Skip to content

Commit

Permalink
Start transition from set to dotty - tests still failing.
Browse files Browse the repository at this point in the history
  • Loading branch information
julesterrien committed May 1, 2017
1 parent b043516 commit 902dd7a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
npm-debug.*
12 changes: 12 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var resetOrOmit = _utils2.default.resetOrOmit;
var UPDATE = 'UPDATE';
var RESET = 'RESET';

/*
* update
* update the provided state of a given reducer
*/
var update = function update(reducer, tag, state) {
return {
type: UPDATE,
Expand All @@ -29,6 +33,10 @@ var update = function update(reducer, tag, state) {
};
};

/*
* reset
* reset the state of a given reducer, or specific keys of a reducer
*/
var reset = function reset(reducer, tag, state) {
return {
type: RESET,
Expand All @@ -38,6 +46,10 @@ var reset = function reset(reducer, tag, state) {
};
};

/*
* createReducer
* udpate or reset the state of a given reducer
*/
var createReducer = function createReducer(name, initialState) {
return function () {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
Expand Down
39 changes: 14 additions & 25 deletions build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _lodash = require('lodash.set');
var _dotty = require('dotty');

var _lodash2 = _interopRequireDefault(_lodash);
var _dotty2 = _interopRequireDefault(_dotty);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// 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);
};

/*
* resetOrOmit
* initialState: object with the reducer's initial state
* state: object with the reducer's initial state
* keys: array of strings representing keys or paths to keys to reset
* returns an object with the completely or partially resetted state
*/
var resetOrOmit = function resetOrOmit(initialState, state, keys) {
var resetState = state;

Expand All @@ -25,23 +25,12 @@ var resetOrOmit = function resetOrOmit(initialState, state, keys) {
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];
}
var hasInitialState = _dotty2.default.exists(initialState, key);

if (hasInitialState) {
_dotty2.default.put(resetState, key, initialState[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 (typeof value !== 'undefined') {
(0, _lodash2.default)(resetState, key, value);
} else {
throw new Error('Provided path does not exist');
}
_dotty2.default.remove(resetState, key);
}
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "Jules Terrien <[email protected]>",
"license": "MIT",
"dependencies": {
"lodash.set": "^4.3.2"
"dotty": "0.0.2"
},
"devDependencies": {
"babel-cli": "^6.24.1",
Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@ const { resetOrOmit } = utils;
const UPDATE = 'UPDATE';
const RESET = 'RESET';

/*
* update
* update the provided state of a given reducer
*/
const update = (reducer, tag, state) => ({
type: UPDATE,
reducer,
tag,
state,
});

/*
* reset
* reset the state of a given reducer, or specific keys of a reducer
*/
const reset = (reducer, tag, state) => ({
type: RESET,
reducer,
tag,
state,
});

/*
* createReducer
* udpate or reset the state of a given reducer
*/
const createReducer = (name, initialState) => (state = initialState, action) => {
if (typeof name !== 'string') {
return new Error('Expected reducer name to be a string');
Expand Down
37 changes: 13 additions & 24 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import set from 'lodash.set';

// https://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key
const resolve = (path, obj) => {
return path.split('.').reduce((prev, curr) => {
return prev ? prev[curr] : undefined
}, obj || self)
}
import dotty from 'dotty';

/*
* resetOrOmit
* initialState: object with the reducer's initial state
* state: object with the reducer's initial state
* keys: array of strings representing keys or paths to keys to reset
* returns an object with the completely or partially resetted state
*/
const resetOrOmit = (initialState, state, keys) => {
const resetState = state;

Expand All @@ -15,23 +15,12 @@ const resetOrOmit = (initialState, state, keys) => {
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];
}
const hasInitialState = dotty.exists(initialState, key);

if (hasInitialState) {
dotty.put(resetState, key, initialState[key]);
} else {
// key is a path in dot notation
// reset this key to its initial state or return an error
const value = resolve(key, initialState);
if (typeof value !== 'undefined') {
set(resetState, key, value);
} else {
throw new Error('Provided path does not exist');
}
dotty.remove(resetState, key)
}
});

Expand Down
1 change: 0 additions & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ const test = describe('Nova-Redux:', () => {
const resetAction = reset('api', 'reset wrong path', {
reset: ['wrong.path'],
});

const wrapper = () => {
api(nextState, resetAction);
}
Expand Down

0 comments on commit 902dd7a

Please sign in to comment.