redux-state-branch
Advanced tools
Comparing version 0.0.24 to 0.0.26
@@ -1,12 +0,17 @@ | ||
export declare const makeType: (prefix: string, suffix?: string | undefined) => string; | ||
export declare const ensureArray: (items: any) => any[]; | ||
export interface MappedT<T = any> { | ||
[key: string]: T; | ||
interface AnyAction<T = any> { | ||
type: T; | ||
[extraProps: string]: any; | ||
} | ||
export declare type ItemT<T> = T & { | ||
export interface AnyItem { | ||
id: string; | ||
}; | ||
[extraProps: string]: any; | ||
} | ||
export declare type ItemsT<T> = Partial<T> | Array<Partial<T>>; | ||
export declare type ID = string; | ||
export declare type ConstantsT<C> = { | ||
export interface IAction<T> extends AnyAction { | ||
items: ItemsT<T>; | ||
meta: { | ||
[key: string]: any; | ||
}; | ||
} | ||
export interface Constants { | ||
CREATE: string; | ||
@@ -18,43 +23,51 @@ REPLACE: string; | ||
SET_META: string; | ||
} & C; | ||
export interface IAction<T> { | ||
type: string; | ||
items: ItemsT<T>; | ||
meta: MappedT; | ||
} | ||
export interface IState { | ||
export interface State<Item> { | ||
items: { | ||
[key: string]: Item; | ||
}; | ||
[key: string]: any; | ||
} | ||
export declare const generateId: () => string; | ||
export declare class Selectors<ItemType, BranchType> { | ||
/** | ||
* Action Creators | ||
*/ | ||
/** An action creator to reset all branches */ | ||
export declare const resetAllBranches: () => AnyAction<any>; | ||
/** | ||
* Selectors | ||
*/ | ||
export declare class Selectors<ItemT, BranchType> { | ||
protected name: string; | ||
constructor(name: string); | ||
byId<StateT>(state: StateT, id?: ID): ItemType | undefined; | ||
all<StateT>(state: StateT): ItemType[]; | ||
where<StateT>(state: StateT, condition: (item: ItemT<ItemType>) => boolean): ItemType[]; | ||
byId<StateT>(state: StateT, id?: string): ItemT | undefined; | ||
all<StateT>(state: StateT): ItemT[]; | ||
where<StateT>(state: StateT, condition: (item: ItemT) => boolean): ItemT[]; | ||
meta<StateT>(state: StateT): BranchType; | ||
} | ||
export declare class Actions<T> { | ||
protected constant: ConstantsT<any>; | ||
protected defaultItem: any; | ||
constructor(constants: ConstantsT<any>, defaultItem: any); | ||
replace(items: ItemsT<T>, devToolsSuffix?: string): { | ||
export declare class Actions<ItemT extends AnyItem, BranchStateT extends State<ItemT> = State<ItemT>> { | ||
protected constant: Constants; | ||
protected defaultItem: ItemT; | ||
protected generateId: () => string; | ||
constructor(constants: Constants, defaultItem: ItemT, generateId: () => string); | ||
replace(items: ItemsT<ItemT>, devToolsSuffix?: string): { | ||
type: string; | ||
items: any[]; | ||
items: ItemsT<ItemT>[]; | ||
}; | ||
delete(items: ItemsT<T> | ID | ID[], devToolsSuffix?: string): { | ||
delete(items: ItemsT<ItemT> | string | string[], devToolsSuffix?: string): { | ||
type: string; | ||
items: any[]; | ||
items: ((string | Partial<ItemT>)[] | { | ||
id: string; | ||
}[])[]; | ||
}; | ||
create(items?: ItemsT<T>, devToolsSuffix?: string): { | ||
create(items?: ItemsT<ItemT>, devToolsSuffix?: string): { | ||
type: string; | ||
items: any[]; | ||
}; | ||
update(items: ItemsT<T>, devToolsSuffix?: string): { | ||
update(items: ItemsT<ItemT>, devToolsSuffix?: string): { | ||
type: string; | ||
items: any[]; | ||
items: ItemsT<ItemT>[]; | ||
}; | ||
setMeta(meta: MappedT, devToolsSuffix?: string): { | ||
setMeta(meta: Partial<BranchStateT>, devToolsSuffix?: string): { | ||
type: string; | ||
meta: MappedT<any>; | ||
meta: Partial<BranchStateT>; | ||
}; | ||
@@ -65,24 +78,32 @@ reset(devToolsSuffix?: string): { | ||
} | ||
interface IStateBranchOpts<BranchStateType, ItemType, ActionsType = Actions<ItemType>, SelectorsType = Selectors<ItemType, BranchStateType>, ConstantsType = {}, UtilsType = {}> { | ||
export declare class StateBranch<ItemT extends AnyItem, BranchStateT extends State<ItemT> = State<ItemT>, ActionsT extends Actions<ItemT, BranchStateT> = Actions<ItemT, BranchStateT>, SelectorsT extends Selectors<ItemT, BranchStateT> = Selectors<ItemT, BranchStateT>, ConstantsT extends { | ||
[key: string]: string; | ||
} = { | ||
[key: string]: string; | ||
}, UtilsT extends { | ||
[key: string]: any; | ||
} = { | ||
[key: string]: any; | ||
}> { | ||
name: string; | ||
actions?: new (constants: ConstantsT<ConstantsType>, defaultItem: MappedT) => ActionsType; | ||
selectors?: new (name: string) => SelectorsType; | ||
constants?: ConstantsType; | ||
utils?: UtilsType; | ||
defaultItem?: MappedT; | ||
defaultState?: MappedT; | ||
reducer?: (state: IState, action: IAction<ItemType>) => IState; | ||
constant: ConstantsT & Constants; | ||
util: UtilsT; | ||
action: ActionsT; | ||
select: SelectorsT; | ||
defaultItem: ItemT; | ||
defaultState: BranchStateT; | ||
protected extendedReducer: any; | ||
constructor({ name, actions: ActionsConstructor, selectors: SelectorsConstructor, constants, utils, defaultItem, defaultState, reducer, generateId: customGenerateIdFunc }: { | ||
name: string; | ||
actions?: new (constants: Constants, defaultItem: ItemT, generateId: () => string) => ActionsT; | ||
selectors?: new (name: string) => SelectorsT; | ||
constants?: ConstantsT; | ||
utils?: UtilsT; | ||
defaultItem?: ItemT; | ||
defaultState?: BranchStateT; | ||
reducer?: (state: State<ItemT>, action: IAction<ItemT>) => State<ItemT>; | ||
generateId?: () => string; | ||
}); | ||
reducer(state: BranchStateT | undefined, _action: AnyAction): BranchStateT; | ||
} | ||
export declare class StateBranch<ItemType, BranchStateType, ActionsType = Actions<ItemType>, SelectorsType = Selectors<ItemType, BranchStateType>, ConstantsType = {}, UtilsType = {}> { | ||
name: string; | ||
constant: ConstantsT<ConstantsType>; | ||
util: UtilsType; | ||
action: ActionsType; | ||
select: SelectorsType; | ||
defaultItem: MappedT; | ||
defaultState: MappedT; | ||
protected extendedReducer: (state: IState, action: IAction<ItemType>) => IState; | ||
constructor({ name, actions: ActionsConstructor, selectors: SelectorsConstructor, constants, utils, defaultItem, defaultState, reducer }: IStateBranchOpts<BranchStateType, ItemType, ActionsType, SelectorsType, ConstantsType, UtilsType>); | ||
reducer(state: IState | undefined, action: IAction<ItemType>): MappedT<any>; | ||
} | ||
export {}; |
@@ -14,9 +14,16 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.makeType = function (prefix, suffix) { | ||
return "" + prefix + (suffix ? "/" + suffix : ""); | ||
/** | ||
* Constants | ||
*/ | ||
var RESET_ALL = 'RESET_ALL_BRANCHES'; | ||
/** | ||
* Utils | ||
*/ | ||
var makeType = function (prefix, suffix) { | ||
return "" + prefix + (suffix ? '/' + suffix : ''); | ||
}; | ||
exports.ensureArray = function (items) { | ||
var ensureArray = function (items) { | ||
return Array.isArray(items) ? items : [items]; | ||
}; | ||
exports.generateId = function () { | ||
var generateId = function () { | ||
return ([1e7].toString() + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, function (c) { | ||
@@ -27,2 +34,12 @@ return (c ^ | ||
}; | ||
/** | ||
* Action Creators | ||
*/ | ||
/** An action creator to reset all branches */ | ||
exports.resetAllBranches = function () { return ({ | ||
type: RESET_ALL | ||
}); }; | ||
/** | ||
* Selectors | ||
*/ | ||
var Selectors = /** @class */ (function () { | ||
@@ -33,3 +50,3 @@ function Selectors(name) { | ||
Selectors.prototype.byId = function (state, id) { | ||
return state[this.name].items[id || ""]; | ||
return state[this.name].items[id || '']; | ||
}; | ||
@@ -49,10 +66,11 @@ Selectors.prototype.all = function (state) { | ||
var Actions = /** @class */ (function () { | ||
function Actions(constants, defaultItem) { | ||
function Actions(constants, defaultItem, generateId) { | ||
this.constant = constants; | ||
this.defaultItem = defaultItem; | ||
this.generateId = generateId; | ||
} | ||
Actions.prototype.replace = function (items, devToolsSuffix) { | ||
return { | ||
type: exports.makeType(this.constant.REPLACE, devToolsSuffix), | ||
items: exports.ensureArray(items) | ||
type: makeType(this.constant.REPLACE, devToolsSuffix), | ||
items: ensureArray(items) | ||
}; | ||
@@ -63,4 +81,4 @@ }; | ||
return { | ||
type: exports.makeType(this.constant.DELETE, devToolsSuffix), | ||
items: exports.ensureArray(typeof wrappedItems[0] === "string" | ||
type: makeType(this.constant.DELETE, devToolsSuffix), | ||
items: ensureArray(typeof wrappedItems[0] === 'string' | ||
? wrappedItems.map(function (id) { return ({ id: id }); }) | ||
@@ -72,5 +90,6 @@ : wrappedItems) | ||
var _this = this; | ||
var newCreateItems = exports.ensureArray(items || {}).map(function (item) { | ||
var ensureItem = (items || {}); | ||
var newCreateItems = ensureArray(ensureItem).map(function (item) { | ||
if (item.id === undefined) { | ||
item.id = exports.generateId(); | ||
item.id = _this.generateId(); | ||
} | ||
@@ -80,3 +99,3 @@ return __assign({}, _this.defaultItem, item); | ||
return { | ||
type: exports.makeType(this.constant.CREATE, devToolsSuffix), | ||
type: makeType(this.constant.CREATE, devToolsSuffix), | ||
items: newCreateItems | ||
@@ -87,4 +106,4 @@ }; | ||
return { | ||
type: exports.makeType(this.constant.UPDATE, devToolsSuffix), | ||
items: exports.ensureArray(items) | ||
type: makeType(this.constant.UPDATE, devToolsSuffix), | ||
items: ensureArray(items) | ||
}; | ||
@@ -94,3 +113,3 @@ }; | ||
return { | ||
type: exports.makeType(this.constant.SET_META, devToolsSuffix), | ||
type: makeType(this.constant.SET_META, devToolsSuffix), | ||
meta: meta | ||
@@ -101,3 +120,3 @@ }; | ||
return { | ||
type: exports.makeType(this.constant.RESET, devToolsSuffix) | ||
type: makeType(this.constant.RESET, devToolsSuffix) | ||
}; | ||
@@ -122,6 +141,21 @@ }; | ||
// @ts-ignore | ||
constants = _d === void 0 ? {} : _d, utils = _a.utils, _e = _a.defaultItem, defaultItem = _e === void 0 ? {} : _e, _f = _a.defaultState, defaultState = _f === void 0 ? { items: {} } : _f, _g = _a.reducer, reducer = _g === void 0 ? function (state, action) { return state; } : _g; | ||
constants = _d === void 0 ? {} : _d, | ||
// @ts-ignore | ||
_e = _a.utils, | ||
// @ts-ignore | ||
utils = _e === void 0 ? {} : _e, | ||
// @ts-ignore | ||
_f = _a.defaultItem, | ||
// @ts-ignore | ||
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; | ||
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 | ||
var defaultConstants = { | ||
CREATE: name + "/CREATE", | ||
REPLACE: name + "/REPLACE", | ||
UPDATE: name + "/UPDATE", | ||
DELETE: name + "/DELETE", | ||
SET_META: name + "/SET_META", | ||
RESET: name + "/RESET" | ||
}; | ||
this.constant = __assign({}, constants, defaultConstants); | ||
this.util = utils; | ||
@@ -132,9 +166,10 @@ this.defaultItem = defaultItem; | ||
this.extendedReducer = reducer; | ||
this.action = new ActionsConstructor(this.constant, this.defaultItem); | ||
this.action = new ActionsConstructor(this.constant, this.defaultItem, customGenerateIdFunc); | ||
this.select = new SelectorsConstructor(this.name); | ||
} | ||
StateBranch.prototype.reducer = function (state, action) { | ||
StateBranch.prototype.reducer = function (state, _action) { | ||
if (state === void 0) { state = this.defaultState; } | ||
var items = exports.ensureArray(action.items); | ||
var type = action.type.split("/", 2).join("/"); | ||
var action = _action; | ||
var items = ensureArray(action.items); | ||
var type = action.type.split('/', 2).join('/'); | ||
switch (type) { | ||
@@ -168,2 +203,3 @@ case this.constant.CREATE: | ||
case this.constant.RESET: | ||
case RESET_ALL: | ||
return this.defaultState; | ||
@@ -170,0 +206,0 @@ default: |
{ | ||
"name": "redux-state-branch", | ||
"version": "0.0.24", | ||
"version": "0.0.26", | ||
"private": false, | ||
@@ -16,2 +16,3 @@ "description": "A redux wrapper for sane people.", | ||
"test": "react-app-rewired test --scripts-version react-scripts-ts --env=jsdom", | ||
"preversion": "export CI=true && npm run test", | ||
"version": "npm run build", | ||
@@ -28,3 +29,2 @@ "eject": "react-scripts-ts eject" | ||
"@types/react-test-renderer": "^16.0.2", | ||
"@types/redux": "^3.6.0", | ||
"@types/redux-thunk": "^2.1.0", | ||
@@ -31,0 +31,0 @@ "flowgen": "^1.2.2", |
{ | ||
"extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"], | ||
"extends": [], | ||
"linterOptions": { | ||
@@ -4,0 +4,0 @@ "exclude": [ |
Sorry, the diff of this file is not supported yet
42750
21
18
764