Browse Source

Inline deepGet and deepPut.

Herby Vojčík 5 years ago
parent
commit
91d3e68919
1 changed files with 11 additions and 16 deletions
  1. 11 16
      src/cow-value-model.js

+ 11 - 16
src/cow-value-model.js

@@ -45,30 +45,25 @@ const constructKeys = function (keyDescriptions) {
 
 const getKey = (x, key) => x == null ? undefined : isMapKey(key) ? x.get(key.key) : x[key];
 
-const deepGet = (keys, obj) => keys.reduce(getKey, obj);
-
-const deepPut = (keys, obj, val) => {
-    function setVal (x, index) {
-        if (index >= keys.length) return val;
-        const key = keys[index],
-            value = getKey(x, key),
-            modified = setVal(value, index + 1);
-        return value === modified ? x : copyWith(x, key, modified);
-    }
-
-    return setVal(obj, 0);
-};
-
 export const deepGetOrNil = (...keyDescriptions) => {
     const keys = constructKeys(keyDescriptions);
-    return obj => deepGet(keys, obj);
+    return obj => keys.reduce(getKey, obj);
 };
 
 export const deget = deepGetOrNil;
 
 export const deepCopyOnWrite = (...keyDescriptions) => {
     const keys = constructKeys(keyDescriptions);
-    return val => obj => deepPut(keys, obj, val);
+
+    function setVal (x, index, val) {
+        if (index >= keys.length) return val;
+        const key = keys[index],
+            value = getKey(x, key),
+            modified = setVal(value, index + 1, val);
+        return value === modified ? x : copyWith(x, key, modified);
+    }
+
+    return val => obj => setVal(obj, 0, val);
 };
 
 export const decow = deepCopyOnWrite;