Browse Source

Partial apply for copyWith.

Herby Vojčík 6 năm trước cách đây
mục cha
commit
91dc9ece94
1 tập tin đã thay đổi với 11 bổ sung11 xóa
  1. 11 11
      index.js

+ 11 - 11
index.js

@@ -1,20 +1,20 @@
-function copyArrayWith (obj, key, value) {
+const copyArrayWith = (key, value) => obj => {
     const result = obj == null ? [] : [...obj];
     result[key] = value;
     return result;
-}
+};
 
-function copyMapWith (obj, key, value) {
+const copyMapWith = (key, value) => obj => {
     const result = obj == null ? new Map() : new Map(obj);
     result.set(key, value);
     return result;
-}
+};
 
-function copyObjectWith (obj, key, value) {
+const copyObjectWith = (key, value) => obj => {
     const result = obj == null ? {} : {...obj};
     result[key] = value;
     return result;
-}
+};
 
 const isMapKey = key => typeof key === 'object' && key.isKeyInMap === true;
 
@@ -22,10 +22,10 @@ export const keyInMap = key => ({isKeyInMap: true, key});
 
 export const kim = keyInMap;
 
-const copyWith = (obj, key, value) =>
-    typeof key === 'number' ? copyArrayWith(obj, key, value) :
-        isMapKey(key) ? copyMapWith(obj, key.key, value) :
-            copyObjectWith(obj, key, value);
+const copyWith = (key, value) =>
+    typeof key === 'number' ? copyArrayWith(key, value) :
+        isMapKey(key) ? copyMapWith(key.key, value) :
+            copyObjectWith(key, value);
 
 const constructKeys = function (keyDescriptions) {
     const keys = [];
@@ -63,7 +63,7 @@ const chain = (list, stop, step) => (function worker (index) {
 
 const copyOnModification = (obj, key, value, modifierFn) => {
     const modified = modifierFn(value);
-    return value === modified ? obj : copyWith(obj, key, modified);
+    return value === modified ? obj : copyWith(key, modified)(obj);
 };
 
 export const deepCopyOnWrite = (...keyDescriptions) => {