Skip to content

Commit

Permalink
reduxerit 0.2 first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgob committed Apr 25, 2016
1 parent 01bd8dd commit 4c6e5cb
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 77 deletions.
7 changes: 1 addition & 6 deletions .babelrc
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"]
}
101 changes: 66 additions & 35 deletions lib/index.js
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;
});
};
};
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"scripts": {
"build:lib": "babel src --out-dir lib",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha --compilers js:babel-core/register"
},
"author": {
"name": "Jurgo Boemo",
Expand All @@ -22,9 +22,15 @@
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.7.5",
"babel-core": "^6.7.7",
"babel-plugin-transform-es2015-arrow-functions": "^6.5.2",
"babel-plugin-transform-es2015-parameters": "^6.7.0",
"babel-plugin-transform-es2015-spread": "^6.6.5",
"babel-plugin-transform-object-rest-spread": "^6.6.5"
"babel-plugin-transform-object-rest-spread": "^6.6.5",
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.7.2",
"chai": "^3.5.0",
"mocha": "^2.4.5"
}
}
78 changes: 44 additions & 34 deletions src/index.js
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;
}
);
65 changes: 65 additions & 0 deletions test/tests.js
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
}
})
)


})

0 comments on commit 4c6e5cb

Please sign in to comment.