Skip to content

Commit

Permalink
feat(ValueObject): make it work with primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
slikts committed Feb 23, 2020
1 parent e54b26d commit 1d6a1c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/ValueObject.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import DeepCompositeSymbol from './DeepCompositeSymbol';
import { isObject } from './helpers';

/**
* Works somewhat similarly to Record in the Record & Tuple proposal:
* https://github.com/tc39/proposal-record-tuple
*/
// tslint:disable-next-line: variable-name
const ValueObject = <A extends object>(
object: A,
filter?: (entry: [string, any]) => boolean,
): A => {
const key = DeepCompositeSymbol(object, filter);
const ValueObject = <A extends any>(target: A, filter?: (entry: [string, any]) => boolean): A => {
if (!isObject(target)) {
return target;
}
const key = DeepCompositeSymbol(target, filter);
if (cache.has(key)) {
return cache.get(key) as A;
}
cache.set(key, object);
return object;
cache.set(key, target);
return target;
};

const cache = new Map<symbol, object>();
Expand Down
5 changes: 5 additions & 0 deletions test/ValueObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ describe(ValueObject.name, () => {
expect(vO1).not.toBe(vO2);
expect(ValueObject(vO2)).toBe(ValueObject(vO2));
});

it('works with primitives', () => {
expect(() => ValueObject(null)).not.toThrow();
expect(() => ValueObject(undefined)).not.toThrow();
});
});

0 comments on commit 1d6a1c6

Please sign in to comment.