Browse Source

cowWorkshop => pathWorkshop, uses atPath.

Herby Vojčík 5 years ago
parent
commit
eaaa792fe2
2 changed files with 9 additions and 9 deletions
  1. 6 6
      README.md
  2. 3 3
      index.js

+ 6 - 6
README.md

@@ -205,7 +205,7 @@ using `new Map(obj)`, not using `{...obj}`.
 
 Creates "`obj` as an index in a map".
 
-### `cowWorkshop(keys, fn = x => x)(obj, [options])`
+### `pathWorkshop(keys, fn = x => x)(obj, [options])`
 
 This is multipurpose enumerate-and-act function to manipulate objects
 using `atPath`. The `options` argument can contain these additional fields:
@@ -221,27 +221,27 @@ the result key and value is also put into `diff`.
 It then returns `{result, diff}` object.
 
 ```js
-cowWorkshop(["a", "b.c"])();
+pathWorkshop(["a", "b.c"])();
 // does nothing
 // => {result: undefined, diff: undefined}
   
-cowWorkshop(["a", "b.c"], () => null)();
+pathWorkshop(["a", "b.c"], () => null)();
 // sets a and b.c to null
 // => {result: {a: null, b: {c: null}}, diff: {a: null, b: {c: null}}}
   
 const data = {a: 'foo', b: {c: null}};
-cowWorkshop(["a", "b.c"], JSON.stringify)(data);
+pathWorkshop(["a", "b.c"], JSON.stringify)(data);
 // changes a and b.c to string representation; change to a is noop
 // => {result: {a: 'foo', b: {c: 'null'}}, diff: {b: {c: 'null'}}}
   
 const stored = {ay: 'bar', beecee: 'baz', cee: 'quux'};
 const data = {a: 'foo', b: {c: null}};
-cowWorkshop(["a", "b.c"])(data, {result: stored, resultKeys: ["ay", "beecee"]});
+pathWorkshop(["a", "b.c"])(data, {result: stored, resultKeys: ["ay", "beecee"]});
 // "copies" a and b.c into `stored` under different keys
 // => {result: {ay: 'foo', beecee: null, cee: 'quux'}, diff: {ay: 'foo', beecee: null}}
   
 const data = {a: 'foo', b: {c: 'bar'}, c: 'quux'};
-cowWorkshop(["a", "b.c"], () => null)(data);
+pathWorkshop(["a", "b.c"], () => null)(data);
 // "nulls" a few fields
 // => {result: {a: null, b: {c: null}, c: 'quux'}, diff: {a: null, b: {c: null}}}
 ```

+ 3 - 3
index.js

@@ -74,11 +74,11 @@ const atPathBeginningWith = prefix => (...keyDescriptions) => {
 
 export const atPath = atPathBeginningWith([]);
 
-export const cowWorkshop = (keys, fn = x => x) => (obj, {result = obj, resultKeys = keys, diff} = {}) => {
+export const pathWorkshop = (keys, fn = x => x) => (obj, {result = obj, resultKeys = keys, diff} = {}) => {
     keys.forEach((key, index) => {
-        const value = fn(deget(key)(obj));
+        const value = fn(atPath(key).get(obj));
         if (typeof value === "undefined") return;
-        const modifier = decow(resultKeys[index])(value);
+        const modifier = atPath(resultKeys[index]).put(value);
         const oldResult = result;
         result = modifier(oldResult);
         if (result !== oldResult) {