Socket
Socket
Sign inDemoInstall

redux-state-branch

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-state-branch - npm Package Compare versions

Comparing version 0.0.41 to 0.0.43

173

dist/index.d.ts

@@ -36,25 +36,30 @@ interface AnyAction<T = any> {

*/
export declare const selectorsFactory: <ItemT extends AnyItem, BranchStateT extends State<ItemT> = State<ItemT>>(branchName: string) => {
export declare const selectors: <ItemT extends AnyItem, BranchStateT extends State<ItemT> = State<ItemT>>({ name }: {
name: string;
}) => {
/** Get all items */
all: <StateT>(state: StateT) => ItemT[];
all: <StateT>({ state }: {
/** A global state object */
state: StateT;
}) => ItemT[];
/** Get an item by id */
byId: <StateT>(state: StateT, id?: string | null | undefined) => ItemT | undefined;
byId: <StateT>({ state, id }: {
/** A global state object */
state: StateT;
/** The ID of your item */
id?: string | null | undefined;
}) => ItemT | undefined;
/** Get items that meet a filter condition */
where: <StateT>(state: StateT, condition: (item: ItemT) => boolean) => ItemT[];
where: <StateT>({ state, callback }: {
/** A global state object */
state: StateT;
/** A callback to pass the filter function */
callback: (item: ItemT, index: number) => boolean;
}) => ItemT[];
/** Get the top level meta content */
meta: <StateT>(state: StateT) => BranchStateT;
meta: <StateT>({ state }: {
/** A global state object */
state: StateT;
}) => BranchStateT;
};
export declare class Selectors<ItemT extends AnyItem, BranchStateT extends State<ItemT> = State<ItemT>> {
private methods;
protected branchName: string;
constructor(branchName: string);
/** Get all items */
all<StateT>(state: StateT): ItemT[];
/** Get an item by id */
byId<StateT>(state: StateT, id?: string | null): ItemT | undefined;
/** Get an items that meet a filter condition */
where<StateT>(state: StateT, condition: (item: ItemT) => boolean): ItemT[];
/** Get the top level meta content */
meta<StateT>(state: StateT): State<any>;
}
/**

@@ -65,3 +70,7 @@ * Action Creators

export declare const resetAllBranches: () => AnyAction<any>;
export declare const actionsFactory: <ItemT extends AnyItem, BranchStateT extends State<ItemT> = State<ItemT>>(branchName: string, defaultItem?: Partial<ItemT>, generateIdFunc?: () => string) => {
export declare const actions: <ItemT extends AnyItem, BranchStateT extends State<ItemT> = State<ItemT>>({ name, defaultItem, generateId }: {
name: string;
defaultItem?: Partial<ItemT> | undefined;
generateId?: (() => string) | undefined;
}) => {
/** Create an item */

@@ -97,65 +106,81 @@ create: (items?: Partial<ItemT> | Partial<ItemT>[] | undefined, typeSuffix?: string | undefined) => {

};
declare type ActionClassReturn<ItemT> = {
type: string;
items: PartialWithId<ItemT>[];
};
export declare class Actions<ItemT extends AnyItem, BranchStateT extends State<ItemT> = State<ItemT>> {
protected branchName: string;
protected constant: Constants;
protected defaultItem: Partial<ItemT>;
protected generateId: () => string;
private methods;
constructor(branchName: string, constants: Constants, defaultItem: Partial<ItemT>, generateId: () => string);
/** Create an item */
create(items?: ItemsT<ItemT> | undefined, typeSuffix?: string | undefined): ActionClassReturn<ItemT>;
/** Update an item */
update(items: ItemsT<ItemT>, typeSuffix?: string | undefined): ActionClassReturn<ItemT>;
/** Remove an item */
remove(items: string | ItemsT<ItemT> | string[], typeSuffix?: string | undefined): ActionClassReturn<ItemT>;
/** DEPRECATED, Use remove instead */
delete(items: string | ItemsT<ItemT> | string[], typeSuffix?: string | undefined): ActionClassReturn<ItemT>;
/** Replace an item */
replace(items: ItemsT<ItemT>, typeSuffix?: string | undefined): ActionClassReturn<ItemT>;
/** Set meta content */
setMeta(meta: Partial<BranchStateT>, typeSuffix?: string | undefined): {
type: string;
meta: Partial<BranchStateT>;
};
/** Reset branch to initial state */
reset(typeSuffix?: string | undefined): {
type: string;
};
}
declare type ActionsMap = {
[key: string]: Function;
} & ReturnType<typeof actionsFactory>;
declare type SelectorsMap = {
[key: string]: Function;
} & ReturnType<typeof selectorsFactory>;
export declare class StateBranch<ItemT extends AnyItem, BranchStateT extends State<ItemT>, ActionsT extends Actions<ItemT, BranchStateT> | ActionsMap, SelectorsT extends Selectors<ItemT, BranchStateT> | SelectorsMap, ConstantsT extends {
export declare const stateBranch: <ItemT extends AnyItem, BranchStateT extends State<ItemT>, ActionsT, SelectorsT, ConstantsT extends {
[key: string]: string;
}, UtilsT extends {
[key: string]: any;
}> {
}>({ name, defaultItem, generateId: customGenerateIdFunc, actions: customActions, selectors: customSelectors, constants: customConstants, utils, defaultState, reducer: extendedReducer }: {
name: string;
actions?: ActionsT | undefined;
selectors?: SelectorsT | undefined;
constants?: ConstantsT | undefined;
utils?: UtilsT | undefined;
defaultItem?: Partial<ItemT> | undefined;
defaultState?: BranchStateT | undefined;
reducer?: ((state: BranchStateT, action: any) => BranchStateT) | undefined;
generateId?: (() => string) | undefined;
}) => {
name: string;
constant: ConstantsT & Constants;
util: UtilsT;
action: ActionsT;
select: SelectorsT;
defaultItem: Partial<ItemT>;
reducer: (state: BranchStateT | undefined, _action: AnyAction<any>) => BranchStateT;
defaultState: BranchStateT;
protected extendedReducer: any;
constructor({ name, actions: ActionsArg, selectors: SelectorsArg, constants, utils, defaultItem, defaultState, reducer, generateId: customGenerateIdFunc }: {
name: string;
actions?: ActionsT | (new (...args: any) => ActionsT);
selectors?: SelectorsT | (new (...args: any) => SelectorsT);
constants?: ConstantsT;
utils?: UtilsT;
defaultItem?: Partial<ItemT>;
defaultState?: BranchStateT;
reducer?: (state: BranchStateT, action: any) => BranchStateT;
generateId?: () => string;
});
reducer(state: BranchStateT | undefined, _action: AnyAction): BranchStateT;
}
action: {
/** Create an item */
create: (items?: Partial<ItemT> | Partial<ItemT>[] | undefined, typeSuffix?: string | undefined) => {
type: string;
items: PartialWithId<ItemT>[];
};
/** Update an item */
update: (items: ItemsT<ItemT>, typeSuffix?: string | undefined) => {
type: string;
items: PartialWithId<ItemT>[];
};
/** Remove an item */
remove: (items: string | string[] | Partial<ItemT> | Partial<ItemT>[], typeSuffix?: string | undefined) => {
type: string;
items: PartialWithId<ItemT>[];
};
/** Replace an item */
replace: (items: ItemsT<ItemT>, typeSuffix?: string | undefined) => {
type: string;
items: PartialWithId<ItemT>[];
};
/** Set meta content */
setMeta: (meta: Partial<BranchStateT>, typeSuffix?: string | undefined) => {
type: string;
meta: Partial<BranchStateT>;
};
/** Reset branch to initial state */
reset: (typeSuffix?: string | undefined) => {
type: string;
};
} & ActionsT;
select: {
/** Get all items */
all: <StateT>({ state }: {
/** A global state object */
state: StateT;
}) => ItemT[];
/** Get an item by id */
byId: <StateT>({ state, id }: {
/** A global state object */
state: StateT;
/** The ID of your item */
id?: string | null | undefined;
}) => ItemT | undefined;
/** Get items that meet a filter condition */
where: <StateT>({ state, callback }: {
/** A global state object */
state: StateT;
/** A callback to pass the filter function */
callback: (item: ItemT, index: number) => boolean;
}) => ItemT[];
/** Get the top level meta content */
meta: <StateT>({ state }: {
/** A global state object */
state: StateT;
}) => BranchStateT;
} & SelectorsT;
};
export {};

@@ -27,3 +27,3 @@ "use strict";

};
var generateId = function () {
var defaultGenerateId = function () {
return ([1e7].toString() + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, function (c) {

@@ -34,3 +34,3 @@ return (c ^

};
var constantsFactory = function (name) { return ({
var constants = function (name) { return ({
CREATE: name + "/CREATE",

@@ -46,5 +46,7 @@ REPLACE: name + "/REPLACE",

*/
exports.selectorsFactory = function (branchName) {
var all = function (state) {
return Object.values(state[branchName].items);
exports.selectors = function (_a) {
var name = _a.name;
var all = function (_a) {
var state = _a.state;
return Object.values(state[name].items);
};

@@ -55,40 +57,18 @@ return {

/** Get an item by id */
byId: function (state, id) {
return state[branchName].items[id || ''];
byId: function (_a) {
var state = _a.state, id = _a.id;
return state[name].items[id || ''];
},
/** Get items that meet a filter condition */
where: function (state, condition) {
return all(state).filter(condition);
where: function (_a) {
var state = _a.state, callback = _a.callback;
return all({ state: state }).filter(callback);
},
/** Get the top level meta content */
meta: function (state) {
return state[branchName];
meta: function (_a) {
var state = _a.state;
return state[name];
}
};
};
var Selectors = /** @class */ (function () {
function Selectors(branchName) {
this.branchName = branchName;
this.methods = exports.selectorsFactory(branchName);
}
/** Get all items */
Selectors.prototype.all = function (state) {
return this.methods.all(state);
};
/** Get an item by id */
Selectors.prototype.byId = function (state, id) {
return this.methods.byId(state, id);
};
/** Get an items that meet a filter condition */
Selectors.prototype.where = function (state, condition) {
return this.methods.where(state, condition);
};
/** Get the top level meta content */
Selectors.prototype.meta = function (state) {
// bug: cant cast this output as BranchStateT
return this.methods.meta(state);
};
return Selectors;
}());
exports.Selectors = Selectors;
/**

@@ -101,6 +81,5 @@ * Action Creators

}); };
exports.actionsFactory = function (branchName, defaultItem, generateIdFunc) {
if (defaultItem === void 0) { defaultItem = {}; }
if (generateIdFunc === void 0) { generateIdFunc = generateId; }
var constant = constantsFactory(branchName);
exports.actions = function (_a) {
var name = _a.name, _b = _a.defaultItem, defaultItem = _b === void 0 ? {} : _b, _c = _a.generateId, generateId = _c === void 0 ? defaultGenerateId : _c;
var constant = constants(name);
return {

@@ -112,3 +91,3 @@ /** Create an item */

if (item.id === undefined) {
item.id = generateIdFunc();
item.id = generateId();
}

@@ -159,63 +138,13 @@ return __assign({}, defaultItem, item);

};
var Actions = /** @class */ (function () {
function Actions(branchName, constants, defaultItem, generateId) {
this.branchName = name;
this.constant = constants;
this.defaultItem = defaultItem;
this.generateId = generateId;
this.methods = exports.actionsFactory(branchName, defaultItem, generateId);
}
/** Create an item */
Actions.prototype.create = function (items, typeSuffix) {
return this.methods.create(items, typeSuffix);
};
/** Update an item */
Actions.prototype.update = function (items, typeSuffix) {
return this.methods.update(items, typeSuffix);
};
/** Remove an item */
Actions.prototype.remove = function (items, typeSuffix) {
return this.methods.remove(items, typeSuffix);
};
/** DEPRECATED, Use remove instead */
Actions.prototype.delete = function (items, typeSuffix) {
return this.methods.remove(items, typeSuffix);
};
/** Replace an item */
Actions.prototype.replace = function (items, typeSuffix) {
return this.methods.replace(items, typeSuffix);
};
/** Set meta content */
Actions.prototype.setMeta = function (meta, typeSuffix) {
return this.methods.setMeta(meta, typeSuffix);
};
/** Reset branch to initial state */
Actions.prototype.reset = function (typeSuffix) {
return this.methods.reset(typeSuffix);
};
return Actions;
}());
exports.Actions = Actions;
var StateBranch = /** @class */ (function () {
function StateBranch(_a) {
var name = _a.name, _b = _a.actions, ActionsArg = _b === void 0 ? Actions : _b, _c = _a.selectors, SelectorsArg = _c === void 0 ? Selectors : _c, _d = _a.constants, constants = _d === void 0 ? {} : _d, _e = _a.utils, utils = _e === void 0 ? {} : _e, _f = _a.defaultItem, defaultItem = _f === void 0 ? {} : _f, _g = _a.defaultState, defaultState = _g === void 0 ? { items: {} } : _g, _h = _a.reducer, reducer = _h === void 0 ? function (state, action) { return state; } : _h, _j = _a.generateId, customGenerateIdFunc = _j === void 0 ? generateId : _j;
this.name = name;
var defaultConstants = constantsFactory(name);
this.constant = __assign({}, constants, defaultConstants);
this.util = utils;
this.defaultItem = defaultItem;
this.reducer = this.reducer.bind(this);
this.defaultState = defaultState;
this.extendedReducer = reducer;
this.action =
typeof ActionsArg === 'function'
? new ActionsArg(this.name, this.constant, this.defaultItem, customGenerateIdFunc)
: ActionsArg;
this.select =
typeof SelectorsArg === 'function'
? new SelectorsArg(this.name)
: SelectorsArg;
}
StateBranch.prototype.reducer = function (state, _action) {
if (state === void 0) { state = this.defaultState; }
exports.stateBranch = function (_a) {
var name = _a.name, _b = _a.defaultItem, defaultItem = _b === void 0 ? {} : _b, _c = _a.generateId, customGenerateIdFunc = _c === void 0 ? defaultGenerateId : _c, _d = _a.actions, customActions = _d === void 0 ? {} : _d, _e = _a.selectors, customSelectors = _e === void 0 ? {} : _e, _f = _a.constants, customConstants = _f === void 0 ? {} : _f, _g = _a.utils, utils = _g === void 0 ? {} : _g, _h = _a.defaultState, defaultState = _h === void 0 ? { items: {} } : _h, _j = _a.reducer, extendedReducer = _j === void 0 ? function (state, action) { return state; } : _j;
var constant = __assign({}, customConstants, constants(name));
var defaultActions = exports.actions({
name: name,
defaultItem: defaultItem,
generateId: customGenerateIdFunc
});
var defaultSelectors = exports.selectors({ name: name });
var reducer = function (state, _action) {
if (state === void 0) { state = defaultState; }
var action = _action;

@@ -225,3 +154,3 @@ var items = ensureArray(action.items);

switch (type) {
case this.constant.CREATE:
case constant.CREATE:
var newCreateItems = items.reduce(function (acc, item) {

@@ -232,3 +161,3 @@ acc[item.id] = __assign({}, (state.items[item.id] || {}), item);

return __assign({}, state, { items: __assign({}, state.items, newCreateItems) });
case this.constant.UPDATE:
case constant.UPDATE:
var newUpdateItems = items.reduce(function (acc, item) {

@@ -239,3 +168,3 @@ acc[item.id] = __assign({}, (state.items[item.id] || {}), item);

return __assign({}, state, { items: __assign({}, state.items, newUpdateItems) });
case this.constant.REPLACE:
case constant.REPLACE:
var newReplaceItems = items.reduce(function (acc, item) {

@@ -246,3 +175,3 @@ acc[item.id] = item;

return __assign({}, state, { items: __assign({}, state.items, newReplaceItems) });
case this.constant.REMOVE:
case constant.REMOVE:
var newRemoveItems = items.reduce(function (acc, item) {

@@ -253,14 +182,22 @@ delete acc[item.id];

return __assign({}, state, { items: newRemoveItems });
case this.constant.SET_META:
case constant.SET_META:
return __assign({}, state, action.meta);
case this.constant.RESET:
case constant.RESET:
case RESET_ALL:
return this.defaultState;
return defaultState;
default:
return this.extendedReducer(state, action);
return extendedReducer(state, action);
}
};
return StateBranch;
}());
exports.StateBranch = StateBranch;
return {
name: name,
constant: constant,
util: utils,
defaultItem: defaultItem,
reducer: reducer,
defaultState: defaultState,
action: __assign({}, defaultActions, customActions),
select: __assign({}, defaultSelectors, customSelectors)
};
};
//# sourceMappingURL=index.js.map
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {

@@ -28,3 +15,3 @@ __assign = Object.assign || function(t) {

var _1 = require(".");
var PREFIX = 'testBranch';
var name = 'testBranch';
var DEFAULT_STATE = {

@@ -45,4 +32,4 @@ metaItem: 'test',

});
var branch = new _1.StateBranch({
name: PREFIX,
var branch = _1.stateBranch({
name: name,
constants: {

@@ -63,4 +50,4 @@ CUSTOM: 'CUSTOM_CONSTANT'

describe('ID Generation', function () {
var customIdBranch = new _1.StateBranch({
name: PREFIX,
var customIdBranch = _1.stateBranch({
name: name,
generateId: function () { return 'custom-id'; }

@@ -75,18 +62,18 @@ });

it('CREATE', function () {
expect(branch.constant.CREATE).toBe(PREFIX + "/CREATE");
expect(branch.constant.CREATE).toBe(name + "/CREATE");
});
it('REPLACE', function () {
expect(branch.constant.REPLACE).toBe(PREFIX + "/REPLACE");
expect(branch.constant.REPLACE).toBe(name + "/REPLACE");
});
it('UPDATE', function () {
expect(branch.constant.UPDATE).toBe(PREFIX + "/UPDATE");
expect(branch.constant.UPDATE).toBe(name + "/UPDATE");
});
it('REMOVE', function () {
expect(branch.constant.REMOVE).toBe(PREFIX + "/REMOVE");
expect(branch.constant.REMOVE).toBe(name + "/REMOVE");
});
it('RESET', function () {
expect(branch.constant.RESET).toBe(PREFIX + "/RESET");
expect(branch.constant.RESET).toBe(name + "/RESET");
});
it('SET_META', function () {
expect(branch.constant.SET_META).toBe(PREFIX + "/SET_META");
expect(branch.constant.SET_META).toBe(name + "/SET_META");
});

@@ -172,18 +159,14 @@ it('custom', function () {

describe('Custom Selectors', function () {
var CustomSelectors = /** @class */ (function (_super) {
__extends(CustomSelectors, _super);
function CustomSelectors() {
return _super !== null && _super.apply(this, arguments) || this;
var where = _1.selectors({ name: name }).where;
var customSelectorsBranch = _1.stateBranch({
name: name,
defaultState: DEFAULT_STATE,
selectors: {
customSelector: function (state) {
return where({ state: state, callback: function (u) { return u.id === 'customUserId'; } });
}
}
CustomSelectors.prototype.customSelector = function (state) {
return this.where(state, function (u) { return u.id === 'customUserId'; });
};
return CustomSelectors;
}(_1.Selectors));
var customSelectorsBranch = new _1.StateBranch({
name: 'customSelectors',
defaultState: DEFAULT_STATE,
selectors: CustomSelectors
});
it('customSelector', function () {
var _a;
customSelectorsBranch.action;

@@ -196,20 +179,15 @@ var action = customSelectorsBranch.action.create({

var state = customSelectorsBranch.reducer(undefined, action);
expect(customSelectorsBranch.select.customSelector({ customSelectors: state })).toEqual([item]);
expect(customSelectorsBranch.select.customSelector((_a = {}, _a[name] = state, _a))).toEqual([item]);
});
});
describe('Custom Actions', function () {
var CustomActions = /** @class */ (function (_super) {
__extends(CustomActions, _super);
function CustomActions() {
return _super !== null && _super.apply(this, arguments) || this;
var create = _1.actions({ name: name }).create;
var customActionsBranch = _1.stateBranch({
name: name,
defaultState: DEFAULT_STATE,
actions: {
customAction: function () {
return create({ id: 'customUser', name: 'Custom User' });
}
}
CustomActions.prototype.customAction = function () {
return this.create({ id: 'customUser', name: 'Custom User' });
};
return CustomActions;
}(_1.Actions));
var customActionsBranch = new _1.StateBranch({
name: 'customActions',
defaultState: DEFAULT_STATE,
actions: CustomActions
});

@@ -228,24 +206,25 @@ it('customAction', function () {

it('byId', function () {
expect(branch.select.byId(state, 'testUser')).toBe(DEFAULT_STATE.items.testUser);
expect(branch.select.byId({ state: state, id: 'testUser' })).toBe(DEFAULT_STATE.items.testUser);
});
it('byId undefined', function () {
expect(branch.select.byId(state, 'nonExistant')).toBeUndefined();
expect(branch.select.byId({ state: state, id: 'nonExistant' })).toBeUndefined();
});
it('all', function () {
expect(branch.select.all(state)).toEqual(Object.values(branchState.items));
expect(branch.select.all({ state: state })).toEqual(Object.values(branchState.items));
});
it('where', function () {
expect(branch.select.where(state, function (user) { return user.id === 'testUser'; })).toEqual([
branchState.items.testUser
]);
expect(branch.select.where({ state: state, callback: function (user) { return user.id === 'testUser'; } })).toEqual([branchState.items.testUser]);
});
it('where empty', function () {
expect(branch.select.where(state, function (user) { return user.id === 'nonExistant'; })).toEqual([]);
expect(branch.select.where({
state: state,
callback: function (user) { return user.id === 'nonExistant'; }
})).toEqual([]);
});
it('meta', function () {
expect(branch.select.meta(state)).toEqual(branchState);
expect(branch.select.meta({ state: state })).toEqual(branchState);
});
});
describe('Utils', function () {
var utilsBranch = new _1.StateBranch({
var utilsBranch = _1.stateBranch({
name: 'utilsBranch',

@@ -252,0 +231,0 @@ utils: {

{
"name": "redux-state-branch",
"version": "0.0.41",
"version": "0.0.43",
"private": false,

@@ -5,0 +5,0 @@ "description": "A redux wrapper for sane people.",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc