Herbert Vojčík 7 лет назад
Родитель
Сommit
a17f719b4b
3 измененных файлов с 34 добавлено и 2 удалено
  1. 31 1
      README.md
  2. 2 1
      lib/node.js
  3. 1 0
      src/node.js

+ 31 - 1
README.md

@@ -12,7 +12,7 @@ Rest of the state is left untouched.
 ```js
 	const r = subReducer("persons", personReducer);
 
-    r({persons: ["John", "Jill"], cars: ["Honda"]}, action);
+    r({persons: ["John", "Jill"], cars: ["Honda"]}, cityon);
     // => {
     //   persons: personReducer(["John", "Jill"], action), 
     //   cars: ["Honda"]
@@ -59,3 +59,33 @@ Useful to "concatenate" a few `subReducer`s. like:
 	    baseReducer
 	)
 ```
+
+## `cowValueModel(key)`
+
+Creates an object with set of functions allowing
+to `set` or `get` specified key from any object.
+You can use dot notation.
+
+Setting return a copy with specified (sub-)property changed;
+in case no change actually happens, returns the original object.
+
+```js
+    const name = cowValueModel("name");
+    
+    name.get({name: "Tom"});
+    // => "Tom"
+    name.set({name: "Tom"}, "Jerry");
+    // => {name: "Jerry"}
+    
+    const city = cowValueModel("address.city");
+    const object = {address: {city: "New York"}};
+    
+    city.get(object);
+    // => "New York"
+    city.set(object, "London");
+    // => {address: {city: "London"}}
+    object;
+    // => {address: {city: "New York"}}
+    city.set(object, "New York") === object;
+    // => true
+```

+ 2 - 1
lib/node.js

@@ -3,12 +3,13 @@
 Object.defineProperty(exports, "__esModule", {
     value: true
 });
-exports.composeReducers = exports.subReducer = undefined;
+exports.composeReducers = exports.subReducer = exports.cowValueModel = undefined;
 
 var _cowValueModel2 = require("./cow-value-model");
 
 function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
 
+exports.cowValueModel = _cowValueModel2.cowValueModel;
 var subReducer = exports.subReducer = function subReducer(key, reducer) {
     for (var _len = arguments.length, otherKeys = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
         otherKeys[_key - 2] = arguments[_key];

+ 1 - 0
src/node.js

@@ -1,4 +1,5 @@
 import {cowValueModel} from './cow-value-model';
+export {cowValueModel};
 
 export const subReducer = (key, reducer, ...otherKeys) => {
     const {get: getValue, set: setValue} = cowValueModel(key),