diff --git a/src/ValueObject.ts b/src/ValueObject.ts index aaf03f9e..c3cf740f 100644 --- a/src/ValueObject.ts +++ b/src/ValueObject.ts @@ -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 = ( - object: A, - filter?: (entry: [string, any]) => boolean, -): A => { - const key = DeepCompositeSymbol(object, filter); +const ValueObject = (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(); diff --git a/test/ValueObject.test.ts b/test/ValueObject.test.ts index 24c23aea..8af56d2d 100644 --- a/test/ValueObject.test.ts +++ b/test/ValueObject.test.ts @@ -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(); + }); });