|
@@ -133,7 +133,14 @@ composeReducers(
|
|
|
|
|
|
Creates an overloaded function allowing
|
|
|
to set or get specified key from any object.
|
|
|
-Get when one arg, set when two args.
|
|
|
+
|
|
|
+It gets when one arg passed, sets when two args passed.
|
|
|
+Setting `undefined` ends up as plain get
|
|
|
+(ES2015 default arguments semantics),
|
|
|
+so props are not created when not present
|
|
|
+if setting `undefined`; other values including `null` are ok
|
|
|
+and create nonpresent props.
|
|
|
+
|
|
|
Specify keys by passing a list of keys to `cowValueModel`.
|
|
|
Key can be either:
|
|
|
- number
|
|
@@ -205,3 +212,44 @@ but it also allows you to use `case answerQuestion.TYPE:` in your reducer,
|
|
|
since `answerQuestion.TYPE === "answer question"`.
|
|
|
IOW, this removes the two-space problem of having `const FOO_BAR_TYPE`
|
|
|
as well as `fooBar` action creator.
|
|
|
+
|
|
|
+### `cowWorkshop(keys, fn = x => x)(obj, [options])`
|
|
|
+
|
|
|
+This is multipurpose enumerate-and-act function to manipulate objects
|
|
|
+using `cowValueModel`. The `options` argument can contain these additional fields:
|
|
|
+ - `result` -- where to put elements (`obj` by default),
|
|
|
+ - `resultKeys` -- what keys to use to put into `result` (`keys` by default)
|
|
|
+ - `diff` -- where to put diffing elements (`undefined` by default)
|
|
|
+
|
|
|
+Function enumerates over keys and performs
|
|
|
+"get key from obj, call fn on value, put transformed value into resultKey in result"
|
|
|
+operations over them, using `cowValueModel` for getting as well as putting.
|
|
|
+Additionally, if putting actually resulted in change,
|
|
|
+the result key and value is also put into `diff`.
|
|
|
+It then returns `{result, diff}` object.
|
|
|
+
|
|
|
+```js
|
|
|
+cowWorkshop(["a", "b.c"])();
|
|
|
+// does nothing
|
|
|
+// => {result: undefined, diff: undefined}
|
|
|
+
|
|
|
+cowWorkshop(["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);
|
|
|
+// 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"]});
|
|
|
+// "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);
|
|
|
+// "nulls" a few fields
|
|
|
+// => {result: {a: null, b: {c: null}, c: 'quux'}, diff: {a: null, b: {c: null}}}
|
|
|
+```
|