-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
184 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
{ | ||
plugins: [ | ||
"transform-es2015-arrow-functions", | ||
["transform-es2015-spread", { "loose": true }], | ||
"transform-es2015-parameters", | ||
"transform-object-rest-spread" | ||
] | ||
"presets": ["es2015","stage-0"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,82 @@ | ||
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; }; | ||
|
||
const getReducerName = function (reducerName) { | ||
return reducerName + '_SET_STATE'; | ||
const removeIn = function (o, target) { | ||
if (target.length === 0) return _extends({}, o); | ||
let cur = target.shift(); | ||
if (target.length === 0) { | ||
const newO = _extends({}, o); | ||
delete newO[cur]; | ||
return newO; | ||
} else { | ||
return _extends({}, o, { [cur]: setIn(o[cur], target) }); | ||
} | ||
}; | ||
|
||
function createReducer(initialState, handlers) { | ||
return function reducer() { | ||
let state = arguments.length <= 0 || arguments[0] === undefined ? initialState : arguments[0]; | ||
let action = arguments[1]; | ||
const updateIn = function (o, target, modifier) { | ||
let cur; | ||
if (typeof target === 'string') cur = target;else { | ||
if (target.length === 0) return _extends({}, o); | ||
cur = target.shift(); | ||
} | ||
|
||
if (handlers.hasOwnProperty(action.type)) { | ||
return handlers[action.type](state, action); | ||
} else { | ||
return state; | ||
} | ||
}; | ||
} | ||
|
||
export const createBasicReducer = function (reducerName, initialState) { | ||
if (target.length === 0 || typeof target === 'string') { | ||
return _extends({}, o, { [cur]: modifier(o(cur)) }); | ||
} else { | ||
return _extends({}, o, { [cur]: updateIn(o[cur], target, modifier) }); | ||
} | ||
}; | ||
|
||
const _initialState = _extends({}, initialState, { | ||
reducerName | ||
export const setIn = function (o, target, val) { | ||
return updateIn(o, target, function () { | ||
return val; | ||
}); | ||
}; | ||
|
||
return createReducer(_initialState, { | ||
[getReducerName(reducerName)]: function (state, action) { | ||
return _extends({}, state, action.state); | ||
} | ||
|
||
}); | ||
export const utils = { | ||
removeIn, | ||
updateIn, | ||
setIn | ||
}; | ||
|
||
export const getReducerState = function (store, reducerName) { | ||
return function () { | ||
const globalState = store.getState(); | ||
const stateReducerName = Object.keys(globalState).find(function (p) { | ||
return globalState[p].reducerName === reducerName; | ||
export const set = function (state, action) { | ||
return function (target) { | ||
return setIn(state, target, action.payload); | ||
}; | ||
}; | ||
export const update = function (state, action) { | ||
return function (target, updateCB) { | ||
return updateIn(state, target, function (oldValue) { | ||
return updateCB(oldValue, action.payload); | ||
}); | ||
return globalState[stateReducerName]; | ||
}; | ||
}; | ||
export const merge = function (state, action) { | ||
return function (target) { | ||
return updateIn(state, target, function (obj) { | ||
return _extends({}, obj, action.payload); | ||
}); | ||
}; | ||
}; | ||
export const remove = function (state) { | ||
return function (target) { | ||
return removeIn(state, target); | ||
}; | ||
}; | ||
|
||
export const createUpdateStateAction = function (reducerName) { | ||
return function (state) { | ||
return { | ||
type: getReducerName(reducerName), | ||
state | ||
}; | ||
//array | ||
export const push = function (state, action) { | ||
return function (target) { | ||
return updateIn(state, target, function (arr) { | ||
return arr.push(action.payload); | ||
}); | ||
}; | ||
}; | ||
export const removeIdx = function (state, action) { | ||
return function (target) { | ||
return updateIn(state, target, function (arr) { | ||
const res = [].concat(arr); | ||
res.splice(action.payload, 1); | ||
return res; | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,56 @@ | ||
const removeIn = (o, target) => { | ||
if(target.length === 0) | ||
return {...o} | ||
let cur = target.shift() | ||
if (target.length === 0) { | ||
const newO = {...o} | ||
delete newO[cur] | ||
return newO; | ||
} | ||
else { | ||
return {...o, [cur]: (setIn(o[cur], target))} | ||
} | ||
} | ||
|
||
const getReducerName = (reducerName) => reducerName+ '_SET_STATE'; | ||
const updateIn = (o, target, modifier) => { | ||
let cur; | ||
if(typeof target === 'string' ) | ||
cur = target | ||
else { | ||
cur = target.shift() | ||
} | ||
|
||
function createReducer(initialState, handlers) { | ||
return function reducer(state = initialState, action) { | ||
if (handlers.hasOwnProperty(action.type)) { | ||
return handlers[action.type](state, action) | ||
if (target.length === 0 || typeof target === 'string') { | ||
return {...o, [cur]: modifier(o[cur])} | ||
} | ||
else { | ||
return state | ||
return {...o, [cur]: (updateIn(o[cur], target, modifier))} | ||
} | ||
} | ||
} | ||
|
||
export const createBasicReducer = (reducerName , initialState) => { | ||
export const setIn = (o, target, val) => updateIn(o, target, () => val) | ||
|
||
const _initialState = { | ||
...initialState, | ||
reducerName | ||
} | ||
|
||
return createReducer( | ||
_initialState, | ||
{ | ||
[getReducerName(reducerName)] : (state, action) => ({ | ||
...state, | ||
...action.state | ||
} | ||
) | ||
export const utils = { | ||
removeIn, | ||
updateIn, | ||
setIn | ||
} | ||
|
||
})} | ||
|
||
export const getReducerState = (store, reducerName) => { | ||
return function(){ | ||
const globalState = store.getState() | ||
const stateReducerName = Object.keys(globalState).find((p) => globalState[p].reducerName === reducerName ) | ||
return globalState[stateReducerName] | ||
} | ||
} | ||
|
||
export const createUpdateStateAction = (reducerName) => { | ||
return (state) => ({ | ||
type:getReducerName(reducerName), | ||
state | ||
}) | ||
} | ||
export const set = (state, action) => target => (setIn(state,target,action.payload )) | ||
export const update = (state, action) => (target, updateCB) => (updateIn(state,target, (oldValue) => updateCB(oldValue, action.payload) )) | ||
export const merge = (state, action) => target => updateIn(state, target, (obj) => ({...obj, ...action.payload}) ) | ||
export const remove = (state) => target => removeIn(state, target) | ||
|
||
//array | ||
export const push = (state, action) => target => updateIn(state, target, (arr) => arr.push(action.payload)) | ||
export const removeIdx = (state, action) => target => updateIn( | ||
state, target, | ||
arr => { | ||
const res = [...arr]; | ||
res.splice(action.payload,1); | ||
return res; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import chai from 'chai'; | ||
import {utils} from '../src/'; | ||
|
||
const {setIn,updateIn} = utils; | ||
|
||
chai.should(); | ||
|
||
describe('Test utils', () => { | ||
const obj = { | ||
a: "aa", | ||
b: { | ||
c:"cc" | ||
} | ||
} | ||
|
||
|
||
it('setIn with target="a"', () => | ||
setIn(obj, 'a', 'aaaa!') | ||
.should.be.deep.equal({ | ||
a: "aaaa!", | ||
b: { | ||
c:"cc" | ||
} | ||
}) | ||
) | ||
|
||
it('setIn with target=["a"]', () => | ||
setIn(obj, ['a'], 'aaaa!') | ||
.should.be.deep.equal({ | ||
a: "aaaa!", | ||
b: { | ||
c:"cc" | ||
} | ||
}) | ||
) | ||
|
||
it("setIn with target=['a','b','c']", () => | ||
setIn(obj, ['b','c'], 'changed!') | ||
.should.be.deep.equal({ | ||
a: "aa", | ||
b: { | ||
c:"changed!" | ||
} | ||
}) | ||
) | ||
|
||
const obj1 = { | ||
a:"a", | ||
b:{ | ||
c:4 | ||
} | ||
} | ||
|
||
it("updateIn updateIn(obj1, ['b','c'], (el) => el*2 )", () => | ||
updateIn(obj1, ['b','c'], (el) => el*2 ) | ||
.should.be.deep.equal({ | ||
a: "a", | ||
b: { | ||
c:8 | ||
} | ||
}) | ||
) | ||
|
||
|
||
}) |