Browse Source

Treat `null` as deep-readable (same as `undefined`).

Herbert Vojčík 6 years ago
parent
commit
eeab5b8d1d
2 changed files with 14 additions and 1 deletions
  1. 13 0
      README.md
  2. 1 1
      src/cow-value-model.js

+ 13 - 0
README.md

@@ -182,6 +182,19 @@ city(object, "New York") === object;
 city2(object, "New York") === object;
 // => true
   
+city(undefined);
+// => undefined
+city(null);
+// => undefined
+city({});
+// => undefined
+city({address: null});
+// => undefined
+city({address: {}});
+// => undefined
+city({address: {city: null}});
+// => null
+  
 city(undefined, "London");
 // => {address: {city: "London"}}
 city(null, "London");

+ 1 - 1
src/cow-value-model.js

@@ -29,7 +29,7 @@ export const cowValueModel = (...keyDescriptions) => {
 
     return (obj, val = GET_SENTINEL) =>
         val === GET_SENTINEL ?
-            keys.reduce((x = {}, key) => x[key], obj) :
+            keys.reduce((x, key) => x == null ? undefined : x[key], obj) :
             setField(obj, 0, val);
 };