Skip to content

Commit

Permalink
feat(HObject): rename some of core value object types (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok authored Sep 29, 2024
1 parent 3cdfbf4 commit dfdcb00
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 79 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- reworked HObjects(no backward compatible changes)
- renamed ValueObjects:
- DateTime -> HDateTime
- UIntValue -> UInt
- UBigInt -> UBigInt64
- StringValue -> HString
- RegexStringValue -> HRegexString


## [0.16.0] - 2024-09-23

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/ValueObject/AccountId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HObjectTypeMeta } from "../../Util";
import { StringValue } from "./StringValue";
import { HString } from "./HString";

export class AccountId extends StringValue<AccountId> {
export class AccountId extends HString<AccountId> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Account', 'ValueObject', 'AccountId', AccountId);
}
4 changes: 2 additions & 2 deletions src/Domain/ValueObject/Email.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { HObjectTypeMeta } from "../../Util";
import { EmailHash } from './EmailHash';
import { RegexStringValue } from './RegexStringValue';
import { HRegexString } from './HRegexString';

const EMAIL_REGEX =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i;

export class Email extends RegexStringValue<Email> {
export class Email extends HRegexString<Email> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'Email', Email);

public static getRegex(): RegExp {
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/ValueObject/EmailHash.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { StringValue } from './StringValue';
import { HString } from './HString';
import { hash } from 'node:crypto';
import { Email } from './Email';
import { HObjectTypeMeta } from "../../Util";

export class EmailHash extends StringValue<EmailHash> {
export class EmailHash extends HString<EmailHash> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'EmailHash', EmailHash);

public static createFromEmail(email: Email): EmailHash {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AbstractValueObject, type AnyValueObject, type ValueObjectType } from './AbstractValueObject';
import { OK, R } from '../../Util/Result';
import { DateTimeFormatter, Duration, Instant, LocalDateTime, Period, ZoneId, ZoneOffset, convert } from '@js-joda/core';
import { HObjectTypeMeta, InvalidStringPlainParseIssue, InvalidTypePlainParseIssue, PlainParseHelper, TooSmallPlainParseIssue, type PlainParseError } from "../../Util";
import { HObjectTypeMeta, InvalidStringPlainParseIssue, InvalidTypePlainParseIssue, PlainParseHelper, TooSmallPlainParseIssue, type PlainParseError } from "../../Util";

export const DEFAULT_DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

Expand All @@ -28,29 +28,29 @@ function createJsJodaFromTimestamp(v: number): LocalDateTime {
/**
* DateTime in UTC zone value object
*/
export class DateTime extends AbstractValueObject<DateTime> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'DateTime', DateTime);
export class HDateTime extends AbstractValueObject<HDateTime> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'HDateTime', HDateTime);

public constructor(private readonly value: LocalDateTime) {
super();
}

public static now(): DateTime {
public static now(): HDateTime {
return new this(LocalDateTime.now(ZoneOffset.UTC).withNano(0));
}

public static nowWithNano(): DateTime {
public static nowWithNano(): HDateTime {
return new this(LocalDateTime.now(ZoneOffset.UTC));
}

public static parse<T extends AnyValueObject>(this: ValueObjectType<T>, plain: unknown): R<T, PlainParseError> {
switch (typeof plain) {
case 'number': return DateTime.fromTimestamp(plain) as any;
case 'number': return HDateTime.fromTimestamp(plain) as any;
case 'string':
try {
return OK(new this(createJsJodaFromString(plain)));
} catch (e) {
return PlainParseHelper.HObjectParseErr(DateTime, [
return PlainParseHelper.HObjectParseErr(HDateTime, [
new InvalidStringPlainParseIssue('datetime', {}, 'Given plain string is not valid datetime')
]) as any;
}
Expand All @@ -60,7 +60,7 @@ export class DateTime extends AbstractValueObject<DateTime> {
return OK(new this(createJsJodaFromDate(plain)));
}

return PlainParseHelper.HObjectParseErr(DateTime, [
return PlainParseHelper.HObjectParseErr(HDateTime, [
new InvalidTypePlainParseIssue(["number", "string", "Date"], typeof plain)
]) as any;
}
Expand All @@ -70,7 +70,7 @@ export class DateTime extends AbstractValueObject<DateTime> {
* @param v
* @returns
*/
public static cs(v: Date | number | string): DateTime {
public static cs(v: Date | number | string): HDateTime {
if (typeof v === 'number') {
return new this(createJsJodaFromTimestamp(v));
}
Expand All @@ -87,14 +87,14 @@ export class DateTime extends AbstractValueObject<DateTime> {
* @param timestamp
* @returns
*/
public static fromTimestamp(timestamp: number): R<DateTime, PlainParseError> {
public static fromTimestamp(timestamp: number): R<HDateTime, PlainParseError> {
timestamp = Math.trunc(timestamp);
if (timestamp < 0) {
return PlainParseHelper.HObjectParseErr(DateTime, [
return PlainParseHelper.HObjectParseErr(HDateTime, [
TooSmallPlainParseIssue.numberGTE(0, timestamp)
]);
}
return OK(new DateTime(createJsJodaFromTimestamp(timestamp)));
return OK(new HDateTime(createJsJodaFromTimestamp(timestamp)));
}

/**
Expand All @@ -116,28 +116,28 @@ export class DateTime extends AbstractValueObject<DateTime> {
* @param amount
* @returns
*/
public plus(amount: Period | Duration): DateTime {
return new DateTime(this.value.plus(amount));
public plus(amount: Period | Duration): HDateTime {
return new HDateTime(this.value.plus(amount));
}

/**
* Returns new instance with an amount subtracted.
* @param amount
* @returns
*/
public minus(amount: Period | Duration): DateTime {
return new DateTime(this.value.minus(amount));
public minus(amount: Period | Duration): HDateTime {
return new HDateTime(this.value.minus(amount));
}

public equals(o: DateTime): boolean {
public equals(o: HDateTime): boolean {
return this.value.equals(o.value);
}

public isAfter(o: DateTime): boolean {
public isAfter(o: HDateTime): boolean {
return this.value.isAfter(o.value);
}

public isBefore(o: DateTime): boolean {
public isBefore(o: HDateTime): boolean {
return this.value.isBefore(o.value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LogicError, OK, StringPlainParseHelper, PlainParseHelper, PlainParseIssue, type PlainParseError, type R } from '../../Util';
import { AbstractValueObject, type AnyValueObject, type ValueObjectType } from "./AbstractValueObject";

export abstract class RegexStringValue<T extends RegexStringValue<any>> extends AbstractValueObject<T> {
export abstract class HRegexString<T extends HRegexString<any>> extends AbstractValueObject<T> {
public constructor(public readonly v: string) {
super();
}
Expand All @@ -24,7 +24,7 @@ export abstract class RegexStringValue<T extends RegexStringValue<any>> extends
* @param v
* @returns
*/
public static cs<T extends RegexStringValue<any>>(this: ValueObjectType<T>, v: string): T {
public static cs<T extends HRegexString<any>>(this: ValueObjectType<T>, v: string): T {
return new (this as any)(v);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HObjectTypeMeta, OK, PlainParseError, PlainParseHelper, StringPlainParseHelper, PlainParseIssue, type R } from "../../Util";
import { AbstractValueObject, type AnyValueObject, type ValueObjectType } from "./AbstractValueObject";

export class StringValue<T extends StringValue<any> = any> extends AbstractValueObject<T> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'String', StringValue);
export class HString<T extends HString<any> = any> extends AbstractValueObject<T> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'HString', HString);


public constructor(public readonly v: string) {
Expand All @@ -23,7 +23,7 @@ export class StringValue<T extends StringValue<any> = any> extends AbstractValue
* @param v
* @returns
*/
public static cs<T extends StringValue>(this: ValueObjectType<T>, v: string): T {
public static cs<T extends HString>(this: ValueObjectType<T>, v: string): T {
return new (this as any)(v);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Domain/ValueObject/RefId.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StringValue } from "./StringValue";
import { HString } from "./HString";
import { customAlphabet } from 'nanoid';
import { HObjectTypeMeta } from "../../Util";

Expand All @@ -7,7 +7,7 @@ const RefIdGenerator = customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcde
/**
* Represents unique random string id. Generates nanoid - 21 characters
*/
export class RefId extends StringValue<RefId> {
export class RefId extends HString<RefId> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'RefId', RefId);

public static gen(): RefId {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HObjectTypeMeta, OK, PlainParseHelper, PlainParseIssue, type PlainParseError, type R } from '../../Util';
import { AbstractValueObject, type AnyValueObject, type ValueObjectType } from './AbstractValueObject';

export class UBigIntValue<T extends UBigIntValue<any> = any> extends AbstractValueObject<T> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'UBigInt', UBigIntValue);
export class UBigInt64<T extends UBigInt64<any> = any> extends AbstractValueObject<T> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'UBigInt64', UBigInt64);

public constructor(public readonly v: bigint) {
super();
Expand All @@ -22,7 +22,7 @@ export class UBigIntValue<T extends UBigIntValue<any> = any> extends AbstractVal
* @param v
* @returns
*/
public static cs<T extends UBigIntValue>(this: ValueObjectType<T>, v: string | number | bigint): T {
public static cs<T extends UBigInt64>(this: ValueObjectType<T>, v: string | number | bigint): T {
return new (this as any)(BigInt(v));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HObjectTypeMeta, IntegerPlainParseHelper, OK, PlainParseHelper, type PlainParseError, type R } from '../../Util';
import { AbstractValueObject, type AnyValueObject, type ValueObjectType } from './AbstractValueObject';

export class UIntValue<T extends UIntValue<any> = any> extends AbstractValueObject<T> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'User', 'ValueObject', 'UInt', UIntValue);
export class UInt<T extends UInt<any> = any> extends AbstractValueObject<T> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'User', 'ValueObject', 'UInt', UInt);

public constructor(public readonly v: number) {
super();
Expand All @@ -18,7 +18,7 @@ export class UIntValue<T extends UIntValue<any> = any> extends AbstractValueObje
* @param v
* @returns
*/
public static cs<T extends UIntValue>(this: ValueObjectType<T>, v: string | number): T {
public static cs<T extends UInt>(this: ValueObjectType<T>, v: string | number): T {
if (typeof v === 'string') {
v = Number.parseInt(v);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/ValueObject/UserId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HObjectTypeMeta } from "../../Util";
import { StringValue } from "./StringValue";
import { HString } from "./HString";

export class UserId extends StringValue<UserId> {
export class UserId extends HString<UserId> {
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'User', 'ValueObject', 'UserId', UserId);
}
10 changes: 5 additions & 5 deletions src/Domain/ValueObject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export * from './Email';
export * from './EmailHash';

// TIME
export * from './DateTime';
export * from './HDateTime';

// PRIMITIVE
export * from './StringValue';
export * from './UBigIntValue';
export * from './UIntValue';
export * from './RegexStringValue';
export * from './HString';
export * from './UBigInt64';
export * from './UInt';
export * from './HRegexString';
export * from './RefId';
10 changes: 5 additions & 5 deletions src/Util/CurrentTime.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { DateTime } from '../Domain';
import { HDateTime } from '../Domain';

export class CurrentTime {
public static i: CurrentTime;

private constNow!: DateTime|null;
private constNow!: HDateTime | null;

public get now(): DateTime {
return this.constNow ?? DateTime.now();
public get now(): HDateTime {
return this.constNow ?? HDateTime.now();
}

public set now(now: DateTime | null) {
public set now(now: HDateTime | null) {
this.constNow = now;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/helper/TestDto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
UIntValue, Dto,
UInt, Dto,
type JsonObjectType,
type R, type PlainParseError,
PlainParseHelper,
Expand All @@ -16,7 +16,7 @@ import {
} from "@";
import type { v } from "@/Util/Plain/types";

export class TestValueObject extends UIntValue { }
export class TestValueObject extends UInt { }

export class OtherTestDto extends Dto<OtherTestDto> {
public primitiveField!: v.int.between<10, 100>;
Expand Down
Loading

0 comments on commit dfdcb00

Please sign in to comment.