Skip to content

Commit

Permalink
fix: validate props types before clone #152
Browse files Browse the repository at this point in the history
  • Loading branch information
4lessandrodev committed May 2, 2024
1 parent d8a6229 commit 2d81051
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/core/value-object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AutoMapperSerializer, IAdapter, IResult, IValueObject, IVoSettings } from "../types";
import { AutoMapperSerializer, IAdapter, IResult, IValueObject, IVoSettings, UID } from "../types";
import { ReadonlyDeep } from "../types-util";
import { deepFreeze } from "../utils/deep-freeze.util";
import AutoMapper from "./auto-mapper";
Expand All @@ -22,8 +22,17 @@ export class ValueObject<Props> extends BaseGettersAndSetters<Props> implements
* @returns true if props is equal and false if not.
*/
isEqual(other: this): boolean {
const currentProps = Object.assign({}, {}, { ...this?.props});
const providedProps = Object.assign({}, {}, { ...other?.props});
const props = this.props;
if (this.validator.isString(props)) return this.validator.string(props as string).isEqual(other.props as string);
if (this.validator.isDate(props)) return (props as Date).getTime() === (other.props as Date)?.getTime();
if (this.validator.isArray(props) || typeof props === 'function') return JSON.stringify(props) === JSON.stringify(other.props);
if (this.validator.isBoolean(props)) return props === other.props;
if (this.validator.isID(props)) return (props as UID).value() === (other.props as UID)?.value();
if (this.validator.isNumber(props) || typeof props === 'bigint') return this.validator.number(props as number).isEqualTo(other.props as number);
if (this.validator.isUndefined(props) || this.validator.isNull(props)) return props === other.props;
if (this.validator.isSymbol(props)) return (props as Symbol).description === (other.props as Symbol)?.description;
const currentProps = Object.assign({}, {}, { ...this?.props });
const providedProps = Object.assign({}, {}, { ...other?.props });
delete currentProps?.['createdAt'];
delete currentProps?.['updatedAt'];
delete providedProps?.['createdAt'];
Expand All @@ -35,26 +44,28 @@ export class ValueObject<Props> extends BaseGettersAndSetters<Props> implements
* @description Get an instance copy.
* @returns a new instance of value object.
*/
clone(props?: Partial<Props>): ValueObject<Props> {
const _props = props ? { ...this.props, ...props } : { ...this.props };
clone(props?: Props extends object ? Partial<Props> : never): this {
const instance = Reflect.getPrototypeOf(this);
const args = [_props, this.config];
const obj = Reflect.construct(instance!.constructor, args);
return obj;
if (typeof this.props === 'object' && !(this.props instanceof Date) && !(Array.isArray(this.props))) {
const _props = props ? { ...this.props, ...props } : { ...this.props };
const args = [_props, this.config];
return Reflect.construct(instance!.constructor, args);
}
const args = [this.props, this.config];
return Reflect.construct(instance!.constructor, args);
}

/**
* @description Get value from value object.
* @returns value as string, number or any type defined.
*/
toObject<T>(adapter? :IAdapter<this, T>)
toObject<T>(adapter?: IAdapter<this, T>)
: T extends {}
? T
: ReadonlyDeep<AutoMapperSerializer<Props>> {
if (adapter && typeof adapter?.build === 'function') return adapter.build(this).value() as any

const serializedObject = this.autoMapper.valueObjectToObj(this) as ReadonlyDeep<AutoMapperSerializer<Props>>;
const frozenObject = deepFreeze<any>(serializedObject);
const frozenObject = deepFreeze<any>(serializedObject);
return frozenObject;
}

Expand Down

0 comments on commit 2d81051

Please sign in to comment.