Herbert Vojčík 7 lat temu
rodzic
commit
470dfee886
5 zmienionych plików z 149 dodań i 1 usunięć
  1. 42 1
      README.md
  2. 39 0
      lib/cow-value-model.js
  3. 33 0
      lib/node.js
  4. 21 0
      src/cow-value-model.js
  5. 14 0
      src/node.js

+ 42 - 1
README.md

@@ -1,3 +1,44 @@
 # redux-sac
 
-Slice and compose redux-type reducers.
+Slice and compose redux-type reducers.
+
+## `subReducer(key, reducer, additionalKey, ...)`
+
+Creates a wrapper reducer that call `reducer`
+on the substate specified by `key`.
+You may use dot notation.
+Rest of the state is left untouched.
+
+```js
+	const r = subReducer("persons", personReducer);
+
+    r({persons: ["John", "Jill"], cars: ["Honda"]}, action);
+    // => {
+    //   persons: personReducer(["John", "Jill"], action), 
+    //   cars: ["Honda"]
+    // }
+```
+
+Respects redux convention that if no change was made,
+the identical object should be returned. So in previous case,
+if `personReducer` would return the identical array,
+`r` would return the state object it was passed in.
+
+If persons were deeper in hierarchy, it could have been created as
+`const r = subReducer("files.persons", personReducer);` for example.
+
+You may pass additional keys (also with possible dot-notation)
+as addition arguments. In that case, additional parts of state
+will be fetched and passed to a sub-reducer:
+
+```js
+	const r = subReducer("persons", personReducer, "assets.cars");
+
+    r({persons: ["John", "Jill"], assets: {cars: ["Honda"]}}, action);
+    // => {
+    //   persons: personReducer(["John", "Jill"], action, ["Honda"]), 
+    //   assets: {cars: ["Honda"]}
+    // }
+```
+
+This technique is mentioned in Redux docs, in "Beyond combineReducers" page.

+ 39 - 0
lib/cow-value-model.js

@@ -0,0 +1,39 @@
+'use strict';
+
+Object.defineProperty(exports, "__esModule", {
+    value: true
+});
+
+var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
+
+function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
+
+var cowValueModel = exports.cowValueModel = function cowValueModel(key) {
+    var keys = key.split('.');
+
+    function setField() {
+        var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
+        var index = arguments[1];
+        var val = arguments[2];
+
+        if (index >= keys.length) return val;
+        var key = keys[index],
+            value = x[key],
+            modified = setField(value, index + 1, val);
+        return value === modified ? x : _extends({}, x, _defineProperty({}, key, modified));
+    }
+
+    return {
+        key: key,
+        get: function get(obj) {
+            return keys.reduce(function () {
+                var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
+                var key = arguments[1];
+                return x[key];
+            }, obj);
+        },
+        set: function set(obj, val) {
+            return setField(obj, 0, val);
+        }
+    };
+};

+ 33 - 0
lib/node.js

@@ -0,0 +1,33 @@
+"use strict";
+
+Object.defineProperty(exports, "__esModule", {
+    value: true
+});
+exports.subReducer = 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); } }
+
+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];
+    }
+
+    var _cowValueModel = (0, _cowValueModel2.cowValueModel)(key),
+        getValue = _cowValueModel.get,
+        setValue = _cowValueModel.set,
+        otherParts = otherKeys.map(function (each) {
+        return (0, _cowValueModel2.cowValueModel)(each).get;
+    });
+
+    return function (state, action) {
+        var newSubState = reducer.apply(undefined, [getValue(state), action].concat(_toConsumableArray(otherParts.map(function (getValue) {
+            return getValue(state);
+        }))));
+        if (typeof newSubState === "undefined") {
+            throw new Error("The '" + key + "' reducer must not return undefined.");
+        }
+        return setValue(state, newSubState);
+    };
+};

+ 21 - 0
src/cow-value-model.js

@@ -0,0 +1,21 @@
+export const cowValueModel = key => {
+    const keys = key.split('.');
+
+    function setField (x = {}, index, val) {
+        if (index >= keys.length) return val;
+        const key = keys[index],
+            value = x[key],
+            modified = setField(value, index + 1, val);
+        return value === modified ? x : {...x, [key]: modified};
+    }
+
+    return {
+        key,
+        get (obj) {
+            return keys.reduce((x = {}, key) => x[key], obj);
+        },
+        set (obj, val) {
+            return setField(obj, 0, val);
+        }
+    };
+};

+ 14 - 0
src/node.js

@@ -0,0 +1,14 @@
+import {cowValueModel} from './cow-value-model';
+
+export const subReducer = (key, reducer, ...otherKeys) => {
+    const {get: getValue, set: setValue} = cowValueModel(key),
+        otherParts = otherKeys.map(each => cowValueModel(each).get);
+
+    return (state, action) => {
+        let newSubState = reducer(getValue(state), action, ...otherParts.map(getValue => getValue(state)));
+        if (typeof newSubState === "undefined") {
+            throw new Error(`The '${key}' reducer must not return undefined.`);
+        }
+        return setValue(state, newSubState);
+    };
+};