Skip to content

Commit

Permalink
feat(vo): added isEqual method to valueObject
Browse files Browse the repository at this point in the history
  • Loading branch information
4lessandrodev committed Jan 13, 2023
1 parent 13a0241 commit a51d497
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/core/value-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ export class ValueObject<Props> extends GettersAndSetters<Props> implements IVal
this.autoMapper = new AutoMapper();
}

/**
* @description Check if value object instance props is equal another provided instance props.
* @param createdAt is not considered on comparation
* @param updatedAt is not considered on comparation
* @returns true if props is equal and false if not.
*/
isEqual(other: ValueObject<Props>): boolean {
const currentProps = Object.assign({}, {}, { ...this.props});
const providedProps = Object.assign({}, {}, { ...other.props});
delete currentProps?.['createdAt'];
delete currentProps?.['updatedAt'];
delete providedProps?.['createdAt'];
delete providedProps?.['updatedAt'];
return JSON.stringify(currentProps) === JSON.stringify(providedProps);
}

/**
* @description Get an instance copy.
* @returns a new instance of value object.
Expand Down
39 changes: 38 additions & 1 deletion tests/core/value-object.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Class, ID, Result, ValueObject } from "../../lib/core";
import { Class, ID, Ok, Result, ValueObject } from "../../lib/core";
import { ICommand, IPropsValidation, IResult } from "../../lib/types";

describe('value-object', () => {
Expand Down Expand Up @@ -531,5 +531,42 @@ describe('value-object', () => {

expect(result.value().get('value')).toBe('change_value');
})
});

describe('compare', () => {

interface Props {
value: string;
}
class Exam extends ValueObject<Props> {
private constructor(props: Props){
super(props)
}

public static create(props: Props): Result<Exam> {
return Ok(new Exam(props));
}
};

it('should to be equal another instance', () => {
const a = Exam.create({ value : "hello there" }).value();
const b = Exam.create({ value : "hello there" }).value();

expect(a.isEqual(b)).toBeTruthy();
});

it('should to be equal another instance', () => {
const a = Exam.create({ value : "hello there" }).value();
const b = a.clone().value();

expect(a.isEqual(b)).toBeTruthy();
});

it('should not to be equal another instance', () => {
const a = Exam.create({ value : "hello there 1" }).value();
const b = Exam.create({ value : "hello there 2" }).value();

expect(a.isEqual(b)).toBeFalsy();
});
})
});

0 comments on commit a51d497

Please sign in to comment.