|
@@ -129,41 +129,30 @@ composeReducers(
|
|
|
|
|
|
## Redux helpers
|
|
|
|
|
|
-### `cowValueModel(key, ...)`
|
|
|
-
|
|
|
-Creates an overloaded function allowing
|
|
|
-to set or get specified key from any object.
|
|
|
+### `deepGetOrNil(key, ...)`
|
|
|
+### `deget(key, ...)`
|
|
|
|
|
|
-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.
|
|
|
+Creates an accessor function allowing
|
|
|
+to get specified key from any object.
|
|
|
|
|
|
-Specify keys by passing a list of keys to `cowValueModel`.
|
|
|
+Specify keys by passing a list of keys to `deepGetOrNil`.
|
|
|
Key can be either:
|
|
|
- number
|
|
|
- array of Keys
|
|
|
- anything else, which is `toString()`ed and dot-split.
|
|
|
|
|
|
-Set-usage (two-arg) returns a copy with specified (sub-)property changed;
|
|
|
-in case no change actually happens, returns the original object.
|
|
|
-
|
|
|
```js
|
|
|
-const name = cowValueModel("name");
|
|
|
+const name = deget("name");
|
|
|
|
|
|
name({name: "Tom"});
|
|
|
// => "Tom"
|
|
|
-name({name: "Tom"}, "Jerry");
|
|
|
-// => {name: "Jerry"}
|
|
|
|
|
|
-const city = cowValueModel("address.city");
|
|
|
-const city2 = cowValueModel("address", "city");
|
|
|
+const city = deget("address.city");
|
|
|
+const city2 = deget("address", "city");
|
|
|
// and other forms, like:
|
|
|
-// const city3 = cowValueModel(["address", "city"]);
|
|
|
-// const city4 = cowValueModel("address", [[], "city"]);
|
|
|
-// const city5 = cowValueModel([[], "address.city"]);
|
|
|
+// const city3 = deget(["address", "city"]);
|
|
|
+// const city4 = deget("address", [[], "city"]);
|
|
|
+// const city5 = deget([[], "address.city"]);
|
|
|
// etc.
|
|
|
const object = {address: {city: "New York"}};
|
|
|
|
|
@@ -171,16 +160,6 @@ city(object);
|
|
|
// => "New York"
|
|
|
city2(object);
|
|
|
// => "New York"
|
|
|
-city(object, "London");
|
|
|
-// => {address: {city: "London"}}
|
|
|
-city2(object, "London");
|
|
|
-// => {address: {city: "London"}}
|
|
|
-object;
|
|
|
-// => {address: {city: "New York"}}
|
|
|
-city(object, "New York") === object;
|
|
|
-// => true
|
|
|
-city2(object, "New York") === object;
|
|
|
-// => true
|
|
|
|
|
|
city(undefined);
|
|
|
// => undefined
|
|
@@ -194,18 +173,73 @@ city({address: {}});
|
|
|
// => undefined
|
|
|
city({address: {city: null}});
|
|
|
// => null
|
|
|
+```
|
|
|
+
|
|
|
+If you put a number in a list of keys to use,
|
|
|
+an object will be treated as an array.
|
|
|
+
|
|
|
+That way you can create eg. `const c = deget("person", 34, "name")`
|
|
|
+to access `obj.person[34].name` with `c(obj)`.
|
|
|
+
|
|
|
+### `deepCopyOnWrite(key, ...)(val)`
|
|
|
+### `decow(key, ...)(val)`
|
|
|
+
|
|
|
+Creates a modifier function allowing
|
|
|
+to "set" specified key to any object
|
|
|
+in an immutable fashion, eg. creating a modified copy
|
|
|
+when actual write happens.
|
|
|
+
|
|
|
+If properties that are to be parents of a sub-value
|
|
|
+are not present, they are created.
|
|
|
+
|
|
|
+Specify keys by passing a list of keys to `deepCopyOnWrite`.
|
|
|
+Key can be either:
|
|
|
+ - number
|
|
|
+ - array of Keys
|
|
|
+ - anything else, which is `toString()`ed and dot-split.
|
|
|
+
|
|
|
+In case no change actually happens (same value is set which is
|
|
|
+already present), returns the original object.
|
|
|
+
|
|
|
+```js
|
|
|
+const setName = decow("name");
|
|
|
+
|
|
|
+setName("Jerry")({name: "Tom"});
|
|
|
+// => {name: "Jerry"}
|
|
|
+
|
|
|
+const setCity = decow("address.city");
|
|
|
+const setCity2 = decow("address", "city");
|
|
|
+// and other forms, like:
|
|
|
+// const setCity3 = decow(["address", "city"]);
|
|
|
+// const setCity4 = decow("address", [[], "city"]);
|
|
|
+// const setCity5 = decow([[], "address.city"]);
|
|
|
+// etc.
|
|
|
+const object = {address: {city: "New York"}};
|
|
|
+
|
|
|
+setCity("London")(object);
|
|
|
+// => {address: {city: "London"}}
|
|
|
+setCity2("London")(object);
|
|
|
+// => {address: {city: "London"}}
|
|
|
+object;
|
|
|
+// => {address: {city: "New York"}}
|
|
|
+setCity("New York")(object) === object;
|
|
|
+// => true
|
|
|
+setCity("New York")(object) === object;
|
|
|
+// => true
|
|
|
|
|
|
-city(undefined, "London");
|
|
|
+const setCityLondon = setCity("London");
|
|
|
+
|
|
|
+setCityLondon(undefined);
|
|
|
// => {address: {city: "London"}}
|
|
|
-city(null, "London");
|
|
|
+setCityLondon(null);
|
|
|
// => {address: {city: "London"}}
|
|
|
-city({}, "London");
|
|
|
+setCityLondon({});
|
|
|
// => {address: {city: "London"}}
|
|
|
-city({address: null}, "London");
|
|
|
+setCityLondon({address: null});
|
|
|
// => {address: {city: "London"}}
|
|
|
-city({address: {}}, "London");
|
|
|
+setCityLondon({address: {}});
|
|
|
// => {address: {city: "London"}}
|
|
|
-city({address: {city: null}}, "London");
|
|
|
+setCityLondon({address: {city: null}});
|
|
|
// => {address: {city: "London"}}
|
|
|
```
|
|
|
|
|
@@ -214,8 +248,23 @@ an object will be treated as an array (unlike the default string case,
|
|
|
where it is treated as an object), so copy wil be created
|
|
|
using `[...obj]`, not using `{...obj}`.
|
|
|
|
|
|
-That way you can create eg. `const c = cowValueObject("person", 34, "name")`
|
|
|
-to access `obj.person[34].name` with `c(obj)` / `c(obj, val)`.
|
|
|
+That way you can create eg. `const c = decow("person", 34, "name")`
|
|
|
+to "set" `obj.person[34].name` with `c(val)(obj)`.
|
|
|
+
|
|
|
+### `cowValueModel(key, ...)`
|
|
|
+
|
|
|
+Deprecated.
|
|
|
+
|
|
|
+Creates an overloaded function allowing
|
|
|
+to set or get specified key from any object.
|
|
|
+
|
|
|
+It gets when one arg passed (works as `deget(key, ...)`),
|
|
|
+sets when two args (obj, val) passed (works as `decow(key, ...)(val)(obj)`),
|
|
|
+with a caveat: 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.
|
|
|
|
|
|
### `typedAction(type, [fn])`
|
|
|
|