Przeglądaj źródła

Partial apply copyOnModification, => modifier.

Herby Vojčík 6 lat temu
rodzic
commit
28f8e2e3cd
1 zmienionych plików z 7 dodań i 7 usunięć
  1. 7 7
      index.js

+ 7 - 7
index.js

@@ -47,21 +47,21 @@ const chain = list => step => stop => (function worker (index) {
     return index >= list.length ? stop : step(list[index], worker(index + 1));
 })(0);
 
-const copyOnModification = (obj, key, value, modifierFn) => {
-    const modified = modifierFn(value);
-    return value === modified ? obj : copyWith(key, modified)(obj);
+const id = x => x;
+
+const modifier = (key, value, valueModifier) => {
+    const modified = valueModifier(value);
+    return value === modified ? id : copyWith(key, modified);
 };
 
 const atPathBeginningWith = prefix => (...keyDescriptions) => {
     const keys = parseKeysInto(keyDescriptions, [...prefix]),
         keyChain = chain(keys),
-        putChain = keyChain((key, next) => x =>
-            copyOnModification(x, key, getKey(x, key), next)
-        ),
+        putChain = keyChain((key, next) => x => modifier(key, getKey(x, key), next)(x)),
         mapChain = keyChain((key, next) => x => {
             const valueWithCheck = getKeyWithCheck(x, key);
             if (valueWithCheck == null) return x;
-            return copyOnModification(x, key, valueWithCheck.value, next);
+            return modifier(key, valueWithCheck.value, next)(x);
         });
 
     return Object.assign(atPathBeginningWith(keys), {