@eknkc/dux
Advanced tools
Comparing version 2.2.2 to 3.0.0
44
build.js
@@ -7,2 +7,5 @@ 'use strict'; | ||
exports.Immutable = undefined; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
exports.createAction = createAction; | ||
@@ -12,2 +15,4 @@ exports.createActions = createActions; | ||
exports.combineReducers = combineReducers; | ||
exports.bindAction = bindAction; | ||
exports.bindActions = bindActions; | ||
@@ -29,9 +34,13 @@ var _seamlessImmutable = require('seamless-immutable'); | ||
} : arguments[1]; | ||
var metaMapper = arguments.length <= 2 || arguments[2] === undefined ? function (x) { | ||
return x; | ||
} : arguments[2]; | ||
var symbol = Symbol(name); | ||
var action = function action() { | ||
var action = function action(payload, meta) { | ||
return { | ||
type: symbol, | ||
payload: mapper.apply(undefined, arguments) | ||
payload: mapper(payload), | ||
meta: metaMapper(meta) | ||
}; | ||
@@ -65,2 +74,3 @@ }; | ||
var combine = arguments[2]; | ||
var filter = arguments[3]; | ||
@@ -73,2 +83,4 @@ if (combine) combine = combineReducers(combine); | ||
if (filter && !filter(action)) return state; | ||
if (!_seamlessImmutable2.default.isImmutable(state)) state = (0, _seamlessImmutable2.default)(state); | ||
@@ -105,1 +117,29 @@ | ||
} | ||
function bindAction(action, dispatch) { | ||
var meta = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
return function (payload) { | ||
var ameta = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
return dispatch(action(payload, Object.assign({}, meta, ameta))); | ||
}; | ||
} | ||
function bindActions(actions, dispatch, meta) { | ||
if (typeof actions === 'function') return bindAction(actions, dispatch, meta); | ||
if ((typeof actions === 'undefined' ? 'undefined' : _typeof(actions)) !== 'object' || actions === null) throw new Error("Action creators must be a function."); | ||
var keys = Object.keys(actions); | ||
var bound = {}; | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
var action = actions[key]; | ||
if (typeof action === 'function') { | ||
bound[key] = bindAction(action, dispatch, meta); | ||
} else if ((typeof action === 'undefined' ? 'undefined' : _typeof(action)) === 'object') { | ||
bound[key] = bindActions(action, dispatch, meta); | ||
} | ||
} | ||
return bound; | ||
} |
37
index.js
@@ -5,9 +5,10 @@ import Immutable from 'seamless-immutable' | ||
export function createAction(name, mapper = x => x) { | ||
export function createAction(name, mapper = x => x, metaMapper = x => x) { | ||
var symbol = Symbol(name); | ||
var action = function(...data) { | ||
var action = function(payload, meta) { | ||
return { | ||
type: symbol, | ||
payload: mapper(...data) | ||
payload: mapper(payload), | ||
meta: metaMapper(meta) | ||
} | ||
@@ -32,3 +33,3 @@ } | ||
export function createReducer(defState, reducers = {}, combine) { | ||
export function createReducer(defState, reducers = {}, combine, filter) { | ||
if (combine) | ||
@@ -38,2 +39,5 @@ combine = combineReducers(combine); | ||
return function(state = defState, action) { | ||
if (filter && !filter(action)) | ||
return state; | ||
if (!Immutable.isImmutable(state)) | ||
@@ -72,1 +76,26 @@ state = Immutable(state); | ||
} | ||
export function bindAction(action, dispatch, meta = {}) { | ||
return (payload, ameta = {}) => dispatch(action(payload, Object.assign({}, meta, ameta))); | ||
} | ||
export function bindActions(actions, dispatch, meta) { | ||
if (typeof actions === 'function') | ||
return bindAction(actions, dispatch, meta) | ||
if (typeof actions !== 'object' || actions === null) | ||
throw new Error("Action creators must be a function.") | ||
var keys = Object.keys(actions) | ||
var bound = {} | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i] | ||
var action = actions[key] | ||
if (typeof action === 'function') { | ||
bound[key] = bindAction(action, dispatch, meta) | ||
} else if (typeof action === 'object') { | ||
bound[key] = bindActions(action, dispatch, meta) | ||
} | ||
} | ||
return bound | ||
} |
{ | ||
"name": "@eknkc/dux", | ||
"version": "2.2.2", | ||
"version": "3.0.0", | ||
"description": "Redux helpers", | ||
@@ -5,0 +5,0 @@ "main": "build.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8085
177