Skip to content

Commit

Permalink
Add babel and build.
Browse files Browse the repository at this point in the history
  • Loading branch information
julesterrien committed Apr 23, 2017
1 parent c959c6f commit 9fcfe1b
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["es2015"]
"presets": ["es2015", "stage-1"]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
97 changes: 97 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -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;
48 changes: 48 additions & 0 deletions build/utils.js
Original file line number Diff line number Diff line change
@@ -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;
45 changes: 45 additions & 0 deletions npm-debug.log
Original file line number Diff line number Diff line change
@@ -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 [email protected]
3 info using [email protected]
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info lifecycle [email protected]~pretest: [email protected]
6 silly lifecycle [email protected]~pretest: no script for pretest, continuing
7 info lifecycle [email protected]~test: [email protected]
8 verbose lifecycle [email protected]~test: unsafe-perm in lifecycle true
9 verbose lifecycle [email protected]~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 [email protected]~test: CWD: /Users/julesterrien/Desktop/nova-redux
11 silly lifecycle [email protected]~test: Args: [ '-c', 'mocha --opts .mocharc ./tests/index.js' ]
12 silly lifecycle [email protected]~test: Returned: code: 3 signal: null
13 info lifecycle [email protected]~test: Failed to exec test script
14 verbose stack Error: [email protected] test: `mocha --opts .mocharc ./tests/index.js`
14 verbose stack Exit status 3
14 verbose stack at EventEmitter.<anonymous> (/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.<anonymous> (/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 [email protected]
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 [email protected] test: `mocha --opts .mocharc ./tests/index.js`
22 error Exit status 3
23 error Failed at the [email protected] 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 ]
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import utils from 'utils';
import utils from './utils';
const { resetOrOmit } = utils;

const UPDATE = 'UPDATE';
Expand Down
2 changes: 1 addition & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 9fcfe1b

Please sign in to comment.