diff --git a/.babelrc b/.babelrc index c13c5f6..36ef793 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,3 @@ { - "presets": ["es2015"] + "presets": ["es2015", "stage-1"] } diff --git a/README.md b/README.md index 4b5b072..f9e3cfe 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ dispatch(update('user', `${username} ${consent ? 'grants' : 'revokes'} consent`, ``` ### 3. Conclusion -Using the `nova-redux` pattern has allowed us to declare increasingly complicated state updates in simple ways. +Using the `nova-redux` pattern has allowed us to declare increasingly complicated sets of state updates in simple and consistent ways. ```js const toggleConsent = consent => (dispatch, getState) => { diff --git a/build/index.js b/build/index.js new file mode 100644 index 0000000..10d1ae2 --- /dev/null +++ b/build/index.js @@ -0,0 +1,97 @@ +'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; }; + +var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; + +var _utils = require('./utils'); + +var _utils2 = _interopRequireDefault(_utils); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var resetOrOmit = _utils2.default.resetOrOmit; + + +var UPDATE = 'UPDATE'; +var RESET = 'RESET'; + +var update = function update(reducer, tag, state) { + return { + type: UPDATE, + reducer: reducer, + tag: tag, + state: state + }; +}; + +var reset = function reset(reducer, tag, state) { + return { + type: RESET, + reducer: reducer, + tag: tag, + state: state + }; +}; + +var createReducer = function createReducer(name, initialState) { + return function () { + var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; + var action = arguments[1]; + + if (typeof name !== 'string') { + return new Error('Expected reducer name to be a string'); + } + if ((typeof initialState === 'undefined' ? 'undefined' : _typeof(initialState)) !== 'object') { + return new Error('Expected initialState to be an object'); + } + if ((typeof state === 'undefined' ? 'undefined' : _typeof(state)) !== 'object') { + return new Error('Expected state to be an object'); + } + if ((typeof action === 'undefined' ? 'undefined' : _typeof(action)) !== 'object') { + return new Error('Expected action to be an object'); + } + if (action.type === RESET && !action.reset.state.isArray()) { + return new Error('expected reset options to be an array'); + } + + switch (action.type) { + case UPDATE: + if (action.reducer === name) { + var nextState = action.state; + return _extends({}, state, nextState, { + _lastAction: action.tag + }); + } + return state; + + case RESET: + if (action.reducer === name) { + var _nextState = action.reset.state; + if (_nextState.length === 0) { + return initialState; + } + var resetState = resetOrOmit(initialState, state, _nextState); + return _extends({}, resetState, { + _lastAction: action.tag + }); + } + return state; + + default: + return state; + } + }; +}; + +var novaRedux = { + createReducer: createReducer, + update: update, + reset: reset +}; + +exports.default = novaRedux; \ No newline at end of file diff --git a/build/utils.js b/build/utils.js new file mode 100644 index 0000000..98b2a67 --- /dev/null +++ b/build/utils.js @@ -0,0 +1,48 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +// https://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key +var resolve = function resolve(path, obj) { + return path.split('.').reduce(function (prev, curr) { + return prev ? prev[curr] : undefined; + }, obj || self); +}; + +var resetOrOmit = function resetOrOmit(initialState, state, keys) { + var resetState = state; + + keys.forEach(function (key) { + if (typeof key !== 'string') { + return new Error('Expected reset key option to be a string'); + } + + if (!key.includes('.')) { + // key is name, not a dot notation path + // reset this key to its initial state or omit it + if (initialState.hasOwnProperty(key)) { + resetState[key] = initialState[key]; + } else { + delete resetState[key]; + } + } else { + // key is a path in dot notation + // reset this key to its initial state or return an error + var value = resolve(key, initialState); + if (value) { + _.set(resetState, key, value); + } else { + return new Error('Provided path does not exist'); + } + } + }); + + return resetState; +}; + +var utils = { + resetOrOmit: resetOrOmit +}; + +exports.default = utils; \ No newline at end of file diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 0000000..c5b2a42 --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,45 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'test' ] +2 info using npm@4.1.2 +3 info using node@v7.7.3 +4 verbose run-script [ 'pretest', 'test', 'posttest' ] +5 info lifecycle nova-redux@1.0.0~pretest: nova-redux@1.0.0 +6 silly lifecycle nova-redux@1.0.0~pretest: no script for pretest, continuing +7 info lifecycle nova-redux@1.0.0~test: nova-redux@1.0.0 +8 verbose lifecycle nova-redux@1.0.0~test: unsafe-perm in lifecycle true +9 verbose lifecycle nova-redux@1.0.0~test: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/Users/julesterrien/Desktop/nova-redux/node_modules/.bin:/Users/julesterrien/.rvm/gems/ruby-2.4.0/bin:/Users/julesterrien/.rvm/gems/ruby-2.4.0@global/bin:/Users/julesterrien/.rvm/rubies/ruby-2.4.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/julesterrien/.rvm/bin +10 verbose lifecycle nova-redux@1.0.0~test: CWD: /Users/julesterrien/Desktop/nova-redux +11 silly lifecycle nova-redux@1.0.0~test: Args: [ '-c', 'mocha --opts .mocharc ./tests/index.js' ] +12 silly lifecycle nova-redux@1.0.0~test: Returned: code: 3 signal: null +13 info lifecycle nova-redux@1.0.0~test: Failed to exec test script +14 verbose stack Error: nova-redux@1.0.0 test: `mocha --opts .mocharc ./tests/index.js` +14 verbose stack Exit status 3 +14 verbose stack at EventEmitter. (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:279:16) +14 verbose stack at emitTwo (events.js:106:13) +14 verbose stack at EventEmitter.emit (events.js:194:7) +14 verbose stack at ChildProcess. (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:40:14) +14 verbose stack at emitTwo (events.js:106:13) +14 verbose stack at ChildProcess.emit (events.js:194:7) +14 verbose stack at maybeClose (internal/child_process.js:899:16) +14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) +15 verbose pkgid nova-redux@1.0.0 +16 verbose cwd /Users/julesterrien/Desktop/nova-redux +17 error Darwin 16.5.0 +18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test" +19 error node v7.7.3 +20 error npm v4.1.2 +21 error code ELIFECYCLE +22 error nova-redux@1.0.0 test: `mocha --opts .mocharc ./tests/index.js` +22 error Exit status 3 +23 error Failed at the nova-redux@1.0.0 test script 'mocha --opts .mocharc ./tests/index.js'. +23 error Make sure you have the latest version of node.js and npm installed. +23 error If you do, this is most likely a problem with the nova-redux package, +23 error not with npm itself. +23 error Tell the author that this fails on your system: +23 error mocha --opts .mocharc ./tests/index.js +23 error You can get information on how to open an issue for this project with: +23 error npm bugs nova-redux +23 error Or if that isn't available, you can get their info via: +23 error npm owner ls nova-redux +23 error There is likely additional logging output above. +24 verbose exit [ 1, true ] diff --git a/package.json b/package.json index 1202b0d..f3ac3b4 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "repository": "https://github.com/julesterrien/redux-updeep", "main": "src/index.js", "scripts": { + "build": "babel src -d build", "test": "mocha --opts .mocharc ./tests/index.js" }, "keywords": [ @@ -19,6 +20,7 @@ "devDependencies": { "babel-cli": "^6.24.1", "babel-preset-es2015": "^6.24.1", + "babel-preset-stage-1": "^6.24.1", "babel-register": "^6.24.1", "chai": "^3.5.0", "mocha": "^3.2.0" diff --git a/src/index.js b/src/index.js index c4adc9e..7a1ab66 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import utils from 'utils'; +import utils from './utils'; const { resetOrOmit } = utils; const UPDATE = 'UPDATE'; diff --git a/tests/index.js b/tests/index.js index 9c16831..00d644e 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,7 +1,7 @@ /* global describe, it */ import * as chai from 'chai'; -import novaRedux from './src'; +import novaRedux from '../build'; const { assert } = chai; const { update, reset, createReducer } = novaRedux;