File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import 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
2021const cache = new Map < symbol , object > ( ) ;
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments