3
0
Herby Vojčík 7 лет назад
Родитель
Сommit
051502e98d
1 измененных файлов с 11 добавлено и 10 удалено
  1. 11 10
      index.js

+ 11 - 10
index.js

@@ -43,7 +43,7 @@ const getKeyWithCheck = (x, key) =>
         isMapKey(key) ? x.has(key.key) ? {value: x.get(key.key)} : null :
             x.hasOwnProperty(key) ? {value: x[key]} : null;
 
-const chain = (list, stop, step) => (function worker (index) {
+const chain = list => step => stop => (function worker (index) {
     return index >= list.length ? stop : step(list[index], worker(index + 1));
 })(0);
 
@@ -53,20 +53,21 @@ const copyOnModification = (obj, key, value, modifierFn) => {
 };
 
 const atPathBeginningWith = prefix => (...keyDescriptions) => {
-    const keys = parseKeysInto(keyDescriptions, [...prefix]);
-
-    return Object.assign(atPathBeginningWith(keys), {
-        get: obj => keys.reduce(getKey, obj),
-
-        put: val => chain(keys, () => val, (key, next) => x =>
+    const keys = parseKeysInto(keyDescriptions, [...prefix]),
+        keyChain = chain(keys),
+        putChain = keyChain((key, next) => x =>
             copyOnModification(x, key, getKey(x, key), next)
         ),
-
-        map: fn => chain(keys, fn, (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 Object.assign(atPathBeginningWith(keys), {
+        get: obj => keys.reduce(getKey, obj),
+        put: val => putChain(() => val),
+        map: mapChain
     });
 };