Skip to content

Commit 1d6a1c6

Browse files
committed
feat(ValueObject): make it work with primitives
1 parent e54b26d commit 1d6a1c6

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

src/ValueObject.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import DeepCompositeSymbol from './DeepCompositeSymbol';
2+
import { isObject } from './helpers';
23

34
/**
45
* Works somewhat similarly to Record in the Record & Tuple proposal:
56
* https://github.com/tc39/proposal-record-tuple
67
*/
78
// tslint:disable-next-line: variable-name
8-
const ValueObject = <A extends object>(
9-
object: A,
10-
filter?: (entry: [string, any]) => boolean,
11-
): A => {
12-
const key = DeepCompositeSymbol(object, filter);
9+
const ValueObject = <A extends any>(target: A, filter?: (entry: [string, any]) => boolean): A => {
10+
if (!isObject(target)) {
11+
return target;
12+
}
13+
const key = DeepCompositeSymbol(target, filter);
1314
if (cache.has(key)) {
1415
return cache.get(key) as A;
1516
}
16-
cache.set(key, object);
17-
return object;
17+
cache.set(key, target);
18+
return target;
1819
};
1920

2021
const cache = new Map<symbol, object>();

test/ValueObject.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ describe(ValueObject.name, () => {
3434
expect(vO1).not.toBe(vO2);
3535
expect(ValueObject(vO2)).toBe(ValueObject(vO2));
3636
});
37+
38+
it('works with primitives', () => {
39+
expect(() => ValueObject(null)).not.toThrow();
40+
expect(() => ValueObject(undefined)).not.toThrow();
41+
});
3742
});

0 commit comments

Comments
 (0)