redux-state-branch
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -21,135 +21,147 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
export var makeType = function (prefix, suffix) { | ||
return "" + prefix + (suffix ? "/" + suffix : ""); | ||
}; | ||
export var ensureArray = function (items) { | ||
return Array.isArray(items) ? items : [items]; | ||
}; | ||
var Selectors = /** @class */ (function () { | ||
function Selectors(name) { | ||
this.name = name; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
Selectors.prototype.byId = function (state, id) { | ||
return state[this.name].items[id || ""]; | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.makeType = function (prefix, suffix) { | ||
return "" + prefix + (suffix ? "/" + suffix : ""); | ||
}; | ||
Selectors.prototype.all = function (state) { | ||
return Object.values(state[this.name].items); | ||
exports.ensureArray = function (items) { | ||
return Array.isArray(items) ? items : [items]; | ||
}; | ||
Selectors.prototype.where = function (state, condition) { | ||
return this.all(state).filter(condition); | ||
}; | ||
Selectors.prototype.meta = function (state) { | ||
var _a = state[this.name], items = _a.items, meta = __rest(_a, ["items"]); | ||
return meta; | ||
}; | ||
return Selectors; | ||
}()); | ||
export { Selectors }; | ||
var Actions = /** @class */ (function () { | ||
function Actions(constants) { | ||
this.constant = constants; | ||
} | ||
Actions.prototype.replace = function (items, devToolsSuffix) { | ||
return { | ||
type: makeType(this.constant.REPLACE, devToolsSuffix), | ||
items: ensureArray(items) | ||
var Selectors = /** @class */ (function () { | ||
function Selectors(name) { | ||
this.name = name; | ||
} | ||
Selectors.prototype.byId = function (state, id) { | ||
return state[this.name].items[id || ""]; | ||
}; | ||
}; | ||
Actions.prototype.delete = function (items, devToolsSuffix) { | ||
var wrappedItems = !Array.isArray(items) ? [items] : items; | ||
return { | ||
type: makeType(this.constant.DELETE, devToolsSuffix), | ||
items: ensureArray(typeof wrappedItems[0] === "string" | ||
? wrappedItems.map(function (id) { return ({ id: id }); }) | ||
: wrappedItems) | ||
Selectors.prototype.all = function (state) { | ||
return Object.values(state[this.name].items); | ||
}; | ||
}; | ||
Actions.prototype.create = function (items, devToolsSuffix) { | ||
return { | ||
type: makeType(this.constant.CREATE, devToolsSuffix), | ||
items: ensureArray(items || {}) | ||
Selectors.prototype.where = function (state, condition) { | ||
return this.all(state).filter(condition); | ||
}; | ||
}; | ||
Actions.prototype.update = function (items, devToolsSuffix) { | ||
return { | ||
type: makeType(this.constant.UPDATE, devToolsSuffix), | ||
items: ensureArray(items) | ||
Selectors.prototype.meta = function (state) { | ||
var _a = state[this.name], items = _a.items, meta = __rest(_a, ["items"]); | ||
return meta; | ||
}; | ||
}; | ||
Actions.prototype.setMeta = function (meta, devToolsSuffix) { | ||
return { | ||
type: makeType(this.constant.SET_META, devToolsSuffix), | ||
meta: meta | ||
return Selectors; | ||
}()); | ||
exports.Selectors = Selectors; | ||
var Actions = /** @class */ (function () { | ||
function Actions(constants) { | ||
this.constant = constants; | ||
} | ||
Actions.prototype.replace = function (items, devToolsSuffix) { | ||
return { | ||
type: exports.makeType(this.constant.REPLACE, devToolsSuffix), | ||
items: exports.ensureArray(items) | ||
}; | ||
}; | ||
}; | ||
Actions.prototype.reset = function (devToolsSuffix) { | ||
return { | ||
type: makeType(this.constant.RESET, devToolsSuffix) | ||
Actions.prototype.delete = function (items, devToolsSuffix) { | ||
var wrappedItems = !Array.isArray(items) ? [items] : items; | ||
return { | ||
type: exports.makeType(this.constant.DELETE, devToolsSuffix), | ||
items: exports.ensureArray(typeof wrappedItems[0] === "string" | ||
? wrappedItems.map(function (id) { return ({ id: id }); }) | ||
: wrappedItems) | ||
}; | ||
}; | ||
}; | ||
return Actions; | ||
}()); | ||
export { Actions }; | ||
var StateBranch = /** @class */ (function () { | ||
function StateBranch(_a) { | ||
var name = _a.name, _b = _a.actions, ActionsConstructor = _b === void 0 ? Actions : _b, _c = _a.selectors, SelectorsConstructor = _c === void 0 ? Selectors : _c, constants = _a.constants, utils = _a.utils, _d = _a.defaultItem, defaultItem = _d === void 0 ? {} : _d, _e = _a.defaultState, defaultState = _e === void 0 ? { items: {} } : _e, _f = _a.reducer, reducer = _f === void 0 ? function (state, action) { return state; } : _f; | ||
this.name = name; | ||
this.constant = __assign({}, constants, { CREATE: name + "/CREATE", REPLACE: name + "/REPLACE", UPDATE: name + "/UPDATE", DELETE: name + "/DELETE", SET_META: name + "/SET_META", RESET: name + "/RESET" }); | ||
// @ts-ignore | ||
this.util = utils; | ||
this.action = new ActionsConstructor(this.constant); | ||
this.select = new SelectorsConstructor(this.name); | ||
this.reducer = this.reducer.bind(this); | ||
this.defaultItem = defaultItem; | ||
this.defaultState = defaultState; | ||
this.extendedReducer = reducer; | ||
} | ||
StateBranch.prototype.reducer = function (state, action) { | ||
var _this = this; | ||
if (state === void 0) { state = this.defaultState; } | ||
var items = ensureArray(action.items); | ||
var type = action.type.split("/", 2).join("/"); | ||
switch (type) { | ||
case this.constant.CREATE: | ||
var newCreateItems = items.reduce(function (acc, item) { | ||
if (item.id === undefined) { | ||
if (action.type === _this.constant.CREATE) { | ||
item.id = "-" + Math.random() | ||
.toString(16) | ||
.slice(2); | ||
Actions.prototype.create = function (items, devToolsSuffix) { | ||
return { | ||
type: exports.makeType(this.constant.CREATE, devToolsSuffix), | ||
items: exports.ensureArray(items || {}) | ||
}; | ||
}; | ||
Actions.prototype.update = function (items, devToolsSuffix) { | ||
return { | ||
type: exports.makeType(this.constant.UPDATE, devToolsSuffix), | ||
items: exports.ensureArray(items) | ||
}; | ||
}; | ||
Actions.prototype.setMeta = function (meta, devToolsSuffix) { | ||
return { | ||
type: exports.makeType(this.constant.SET_META, devToolsSuffix), | ||
meta: meta | ||
}; | ||
}; | ||
Actions.prototype.reset = function (devToolsSuffix) { | ||
return { | ||
type: exports.makeType(this.constant.RESET, devToolsSuffix) | ||
}; | ||
}; | ||
return Actions; | ||
}()); | ||
exports.Actions = Actions; | ||
var StateBranch = /** @class */ (function () { | ||
function StateBranch(_a) { | ||
var name = _a.name, _b = _a.actions, ActionsConstructor = _b === void 0 ? Actions : _b, _c = _a.selectors, SelectorsConstructor = _c === void 0 ? Selectors : _c, constants = _a.constants, utils = _a.utils, _d = _a.defaultItem, defaultItem = _d === void 0 ? {} : _d, _e = _a.defaultState, defaultState = _e === void 0 ? { items: {} } : _e, _f = _a.reducer, reducer = _f === void 0 ? function (state, action) { return state; } : _f; | ||
this.name = name; | ||
this.constant = __assign({}, constants, { CREATE: name + "/CREATE", REPLACE: name + "/REPLACE", UPDATE: name + "/UPDATE", DELETE: name + "/DELETE", SET_META: name + "/SET_META", RESET: name + "/RESET" }); | ||
// @ts-ignore | ||
this.util = utils; | ||
this.action = new ActionsConstructor(this.constant); | ||
this.select = new SelectorsConstructor(this.name); | ||
this.reducer = this.reducer.bind(this); | ||
this.defaultItem = defaultItem; | ||
this.defaultState = defaultState; | ||
this.extendedReducer = reducer; | ||
} | ||
StateBranch.prototype.reducer = function (state, action) { | ||
var _this = this; | ||
if (state === void 0) { state = this.defaultState; } | ||
var items = exports.ensureArray(action.items); | ||
var type = action.type.split("/", 2).join("/"); | ||
switch (type) { | ||
case this.constant.CREATE: | ||
var newCreateItems = items.reduce(function (acc, item) { | ||
if (item.id === undefined) { | ||
if (action.type === _this.constant.CREATE) { | ||
item.id = "-" + Math.random() | ||
.toString(16) | ||
.slice(2); | ||
} | ||
} | ||
} | ||
acc[item.id] = __assign({}, _this.defaultItem, item); | ||
return acc; | ||
}, {}); | ||
return __assign({}, state, { items: __assign({}, state.items, newCreateItems) }); | ||
case this.constant.UPDATE: | ||
var newUpdateItems = items.reduce(function (acc, item) { | ||
acc[item.id] = __assign({}, (state.items[item.id] || {}), item); | ||
return acc; | ||
}, {}); | ||
return __assign({}, state, { items: __assign({}, state.items, newUpdateItems) }); | ||
case this.constant.REPLACE: | ||
var newReplaceItems = items.reduce(function (acc, item) { | ||
acc[item.id] = item; | ||
return acc; | ||
}, {}); | ||
return __assign({}, state, { items: __assign({}, state.items, newReplaceItems) }); | ||
case this.constant.DELETE: | ||
var newDeleteItems = items.reduce(function (acc, item) { | ||
delete acc[item.id]; | ||
return acc; | ||
}, __assign({}, state.items)); | ||
return __assign({}, state, { items: newDeleteItems }); | ||
case this.constant.SET_META: | ||
return __assign({}, state, action.meta); | ||
case this.constant.RESET: | ||
return this.defaultState; | ||
default: | ||
return this.extendedReducer(state, action); | ||
} | ||
}; | ||
return StateBranch; | ||
}()); | ||
export { StateBranch }; | ||
acc[item.id] = __assign({}, _this.defaultItem, item); | ||
return acc; | ||
}, {}); | ||
return __assign({}, state, { items: __assign({}, state.items, newCreateItems) }); | ||
case this.constant.UPDATE: | ||
var newUpdateItems = items.reduce(function (acc, item) { | ||
acc[item.id] = __assign({}, (state.items[item.id] || {}), item); | ||
return acc; | ||
}, {}); | ||
return __assign({}, state, { items: __assign({}, state.items, newUpdateItems) }); | ||
case this.constant.REPLACE: | ||
var newReplaceItems = items.reduce(function (acc, item) { | ||
acc[item.id] = item; | ||
return acc; | ||
}, {}); | ||
return __assign({}, state, { items: __assign({}, state.items, newReplaceItems) }); | ||
case this.constant.DELETE: | ||
var newDeleteItems = items.reduce(function (acc, item) { | ||
delete acc[item.id]; | ||
return acc; | ||
}, __assign({}, state.items)); | ||
return __assign({}, state, { items: newDeleteItems }); | ||
case this.constant.SET_META: | ||
return __assign({}, state, action.meta); | ||
case this.constant.RESET: | ||
return this.defaultState; | ||
default: | ||
return this.extendedReducer(state, action); | ||
} | ||
}; | ||
return StateBranch; | ||
}()); | ||
exports.StateBranch = StateBranch; | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "redux-state-branch", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "A redux wrapper for sane people.", |
@@ -8,3 +8,3 @@ { | ||
"declaration": true, | ||
"module": "esnext", | ||
"module": "umd", | ||
"target": "es5", | ||
@@ -11,0 +11,0 @@ "lib": ["es2017", "dom"], |
Sorry, the diff of this file is not supported yet
24067
479