Browse Source

Treat `null` as fillable (same as `undefined`).

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

+ 13 - 0
README.md

@@ -181,6 +181,19 @@ city(object, "New York") === object;
 // => true
 city2(object, "New York") === object;
 // => true
+  
+city(undefined, "London");
+// => {address: {city: "London"}}
+city(null, "London");
+// => {address: {city: "London"}}
+city({}, "London");
+// => {address: {city: "London"}}
+city({address: null}, "London");
+// => {address: {city: "London"}}
+city({address: {}}, "London");
+// => {address: {city: "London"}}
+city({address: {city: null}}, "London");
+// => {address: {city: "London"}}
 ```
 
 If you put a number in a list of keys to use,

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

@@ -17,10 +17,10 @@ export const cowValueModel = (...keyDescriptions) => {
 
     fillKeys(keyDescriptions);
 
-    function setField (x = {}, index, val) {
+    function setField (x, index, val) {
         if (index >= keys.length) return val;
         const key = keys[index],
-            value = x[key],
+            value = x == null ? undefined : x[key],
             modified = setField(value, index + 1, val);
         return value === modified ? x : copyWith(x, key, modified);
     }