Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reducer-composer

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reducer-composer - npm Package Compare versions

Comparing version 0.0.8 to 0.0.9

3

dist/createBatchHandler.d.ts

@@ -1,2 +0,1 @@

import { DeepReadonly } from "./utility";
export declare function createBatchHandler<State, Payload>(handler: (state: DeepReadonly<State>, payload: Payload) => DeepReadonly<State>): (state: DeepReadonly<State>, payload: Payload[]) => DeepReadonly<State>;
export declare function createBatchHandler<State, Payload>(handler: (state: State, payload: Payload) => State): (state: State, payload: Payload[]) => State;
import { Reducer } from "./utility";
/**
* @todo filter unknown actions, cannot be called with unknown actions (cannot be used as is in createCombinedReducer)
*/
export declare function createKeyedReducer<KeyAttribute extends string, State, Action extends {

@@ -6,0 +3,0 @@ type: string;

@@ -23,5 +23,2 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
/**
* @todo filter unknown actions, cannot be called with unknown actions (cannot be used as is in createCombinedReducer)
*/
function createKeyedReducer(keyAttribute, reducer) {

@@ -31,2 +28,5 @@ return function (state, action) {

var _a;
if (!action.payload ||
!Object.prototype.hasOwnProperty.call(action.payload, keyAttribute))
return state;
var type = action.type, _b = action.payload, _c = keyAttribute, key = _b[_c], payload = __rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);

@@ -33,0 +33,0 @@ return __assign({}, state, (_a = {}, _a[key] = reducer(state[key], { type: type, payload: payload }), _a));

@@ -1,3 +0,2 @@

import { DeepReadonly } from "./utility";
export declare function createReducer<State>(): <Handlers extends Record<string, (state: DeepReadonly<State>, payload: any) => DeepReadonly<State>>>(handlers: Handlers) => (state: DeepReadonly<State>, action: { [Type in keyof Handlers]: Parameters<Handlers[Type]>[1] extends undefined ? {
export declare function createReducer<State>(): <Handlers extends Record<string, (state: State, payload: any) => State>>(handlers: Handlers) => (state: State, action: { [Type in keyof Handlers]: Parameters<Handlers[Type]>[1] extends undefined ? {
type: Type;

@@ -8,2 +7,2 @@ payload?: undefined;

payload: Parameters<Handlers[Type]>[1];
}; }[keyof Handlers]) => DeepReadonly<State>;
}; }[keyof Handlers]) => State;

@@ -1,14 +0,14 @@

export declare function createReducerCrudHandlers<Entity, EntityPayload, KeyPayload>(entitySelector: (payload: EntityPayload) => [string, Entity], keySelector: (payload: KeyPayload) => string): {
create: (record: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: EntityPayload) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
update: (record: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: EntityPayload) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
delete: (record: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: KeyPayload) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
upsert: (record: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: EntityPayload) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
discard: (record: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: KeyPayload) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
export declare function createReducerCrudHandlers<Entity, EntityPayload, KeyPayload>(entitySelector: (payload: EntityPayload) => [string | number, Entity], keySelector: (payload: KeyPayload) => string | number): {
create: (record: Record<string, Entity>, payload: EntityPayload) => Record<string, Entity>;
update: (record: Record<string, Entity>, payload: EntityPayload) => Record<string, Entity>;
delete: (record: Record<string, Entity>, payload: KeyPayload) => Record<string, Entity>;
upsert: import("./utility").Description<"add item if not exists, update otherwise"> & ((record: Record<string, Entity>, payload: EntityPayload) => Record<string, Entity>);
discard: import("./utility").Description<"remove item if exists, nothing otherwise"> & ((record: Record<string, Entity>, payload: KeyPayload) => Record<string, Entity>);
batch: {
create: (state: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: EntityPayload[]) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
update: (state: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: EntityPayload[]) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
delete: (state: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: KeyPayload[]) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
upsert: (state: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: EntityPayload[]) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
discard: (state: import("./utility").DeepReadonlyObject<Record<string, Entity>>, payload: EntityPayload[]) => import("./utility").DeepReadonlyObject<Record<string, Entity>>;
create: (state: Record<string, Entity>, payload: EntityPayload[]) => Record<string, Entity>;
update: (state: Record<string, Entity>, payload: EntityPayload[]) => Record<string, Entity>;
delete: (state: Record<string, Entity>, payload: KeyPayload[]) => Record<string, Entity>;
upsert: (state: Record<string, Entity>, payload: EntityPayload[]) => Record<string, Entity>;
discard: (state: Record<string, Entity>, payload: EntityPayload[]) => Record<string, Entity>;
};
};

@@ -23,2 +23,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var utility_1 = require("./utility");
var createBatchHandler_1 = require("./createBatchHandler");

@@ -54,10 +55,10 @@ function createReducerCrudHandlers(entitySelector, keySelector) {

});
var upsert = withEntity(function (state, key, entity) {
var upsert = utility_1.description()(withEntity(function (state, key, entity) {
var _a;
return __assign({}, state, (_a = {}, _a[key] = entity, _a));
});
var discard = withKey(function (state, key) {
}));
var discard = utility_1.description()(withKey(function (state, key) {
var _a = key, removed = state[_a], rest = __rest(state, [typeof _a === "symbol" ? _a : _a + ""]);
return rest;
});
}));
return {

@@ -64,0 +65,0 @@ create: create,

@@ -1,12 +0,11 @@

import { DeepReadonly } from "./utility";
export declare function createReducerOnAction<State, Action extends {
type: string;
payload?: any;
}>(): <Handlers extends { [Type in Action["type"]]: (state: DeepReadonly<State>, payload: Extract<Action, {
}>(): <Handlers extends { [Type in Action["type"]]: (state: State, payload: Extract<Action, {
type: Type;
}>["payload"]) => DeepReadonly<State>; }>(handlers: Handlers) => (state: DeepReadonly<State>, action: { [Type in keyof { [Type in keyof Handlers]: (state: DeepReadonly<State>, payload: Extract<Action, {
}>["payload"]) => State; }>(handlers: Handlers) => (state: State, action: { [Type in keyof { [Type in keyof Handlers]: (state: State, payload: Extract<Action, {
type: Type;
}>["payload"]) => DeepReadonly<State>; }]: Parameters<{ [Type in keyof Handlers]: (state: DeepReadonly<State>, payload: Extract<Action, {
}>["payload"]) => State; }]: Parameters<{ [Type in keyof Handlers]: (state: State, payload: Extract<Action, {
type: Type;
}>["payload"]) => DeepReadonly<State>; }[Type]>[1] extends undefined ? {
}>["payload"]) => State; }[Type]>[1] extends undefined ? {
type: Type;

@@ -16,5 +15,5 @@ payload?: undefined;

type: Type;
payload: Parameters<{ [Type in keyof Handlers]: (state: DeepReadonly<State>, payload: Extract<Action, {
payload: Parameters<{ [Type in keyof Handlers]: (state: State, payload: Extract<Action, {
type: Type;
}>["payload"]) => DeepReadonly<State>; }[Type]>[1];
}; }[keyof Handlers]) => DeepReadonly<State>;
}>["payload"]) => State; }[Type]>[1];
}; }[keyof Handlers]) => State;

@@ -24,1 +24,7 @@ export declare type Values<O> = O[keyof O];

}>(reducer: Reducer<State, Action>, state: State, actions: Action[]): State;
export declare class Description<Desc> {
private description;
private constructor();
}
export declare function description<Desc>(): <Item>(item: Item) => Description<Desc> & Item;
export declare type DescriptionOf<Item extends Description<any>> = Item extends Description<infer Desc> ? Desc : unknown;

@@ -19,1 +19,12 @@ "use strict";

exports.runActionsOnReducer = runActionsOnReducer;
var Description = /** @class */ (function () {
function Description(description) {
this.description = description;
}
return Description;
}());
exports.Description = Description;
function description() {
return function (item) { return item; };
}
exports.description = description;

@@ -11,3 +11,3 @@ {

],
"version": "0.0.8",
"version": "0.0.9",
"license": "MIT",

@@ -14,0 +14,0 @@ "author": "Frederik Batuna",

@@ -93,2 +93,10 @@ # reducer-composer

## Todo
[ ] handle unknown actions on keyed and keyedBy reducer
[ ] documentation
[ ] dtslint tests
[ ] jest tests
[ ] performance tests
## License MIT

@@ -1,8 +0,6 @@

import { DeepReadonly } from "./utility";
export function createBatchHandler<State, Payload>(
handler: (state: DeepReadonly<State>, payload: Payload) => DeepReadonly<State>
handler: (state: State, payload: Payload) => State
) {
return (state: DeepReadonly<State>, payload: Array<Payload>) =>
return (state: State, payload: Array<Payload>) =>
payload.reduce(handler, state);
}
import { Reducer } from "./utility";
/**
* @todo filter unknown actions, cannot be called with unknown actions (cannot be used as is in createCombinedReducer)
*/
export function createKeyedReducer<

@@ -18,2 +15,7 @@ KeyAttribute extends string,

return (state = {}, action) => {
if (
!action.payload ||
!Object.prototype.hasOwnProperty.call(action.payload, keyAttribute)
)
return state;
const {

@@ -20,0 +22,0 @@ type,

@@ -1,9 +0,6 @@

import { Values, DeepReadonly } from "./utility";
import { Values } from "./utility";
export function createReducer<State>() {
return <
Handlers extends Record<
string,
(state: DeepReadonly<State>, payload: any) => DeepReadonly<State>
>
Handlers extends Record<string, (state: State, payload: any) => State>
>(

@@ -13,3 +10,3 @@ handlers: Handlers

return (
state: DeepReadonly<State>,
state: State,
action: Values<

@@ -16,0 +13,0 @@ {

@@ -1,27 +0,24 @@

import { DeepReadonly } from "./utility";
import { description } from "./utility";
import { createBatchHandler } from "./createBatchHandler";
export function createReducerCrudHandlers<Entity, EntityPayload, KeyPayload>(
entitySelector: (payload: EntityPayload) => [string, Entity],
keySelector: (payload: KeyPayload) => string
entitySelector: (payload: EntityPayload) => [string | number, Entity],
keySelector: (payload: KeyPayload) => string | number
) {
const withEntity = (
f: (
record: DeepReadonly<Record<string, Entity>>,
key: string,
entity: DeepReadonly<Entity>
) => DeepReadonly<Record<string, Entity>>
) => (
record: DeepReadonly<Record<string, Entity>>,
payload: EntityPayload
) => {
record: Record<string, Entity>,
key: string | number,
entity: Entity
) => Record<string, Entity>
) => (record: Record<string, Entity>, payload: EntityPayload) => {
const [key, entity] = entitySelector(payload);
return f(record, key, (entity as unknown) as DeepReadonly<Entity>);
return f(record, key, entity);
};
const withKey = (
f: (
record: DeepReadonly<Record<string, Entity>>,
key: string
) => DeepReadonly<Record<string, Entity>>
) => (record: DeepReadonly<Record<string, Entity>>, payload: KeyPayload) =>
record: Record<string, Entity>,
key: string | number
) => Record<string, Entity>
) => (record: Record<string, Entity>, payload: KeyPayload) =>
f(record, keySelector(payload));

@@ -43,13 +40,17 @@ const create = withEntity((state, key, entity) => {

const { [key]: removed, ...rest } = state;
return rest as DeepReadonly<Record<string, Entity>>;
return rest;
}
throw new Error();
});
const upsert = withEntity((state, key, entity) => {
return { ...state, [key]: entity };
});
const discard = withKey((state, key) => {
const { [key]: removed, ...rest } = state;
return rest as DeepReadonly<Record<string, Entity>>;
});
const upsert = description<"add item if not exists, update otherwise">()(
withEntity((state, key, entity) => {
return { ...state, [key]: entity };
})
);
const discard = description<"remove item if exists, nothing otherwise">()(
withKey((state, key) => {
const { [key]: removed, ...rest } = state;
return rest;
})
);
return {

@@ -56,0 +57,0 @@ create,

import { createReducer } from "./createReducer";
import { DeepReadonly } from "./utility";

@@ -11,5 +10,5 @@ export function createReducerOnAction<

[Type in Action["type"]]: (
state: DeepReadonly<State>,
state: State,
payload: Extract<Action, { type: Type }>["payload"]
) => DeepReadonly<State>
) => State
}

@@ -21,6 +20,6 @@ >(

[Type in keyof Handlers]: (
state: DeepReadonly<State>,
state: State,
payload: Extract<Action, { type: Type }>["payload"]
) => DeepReadonly<State>
) => State
});
}

@@ -12,7 +12,5 @@ export type Values<O> = O[keyof O];

export interface DeepReadonlyArray<A> extends ReadonlyArray<DeepReadonly<A>> {}
export type DeepReadonlyObject<A> = {
readonly [K in keyof A]: DeepReadonly<A[K]>
};
export type DeepReadonly<A> = A extends Array<infer B>

@@ -42,1 +40,11 @@ ? DeepReadonlyArray<B>

}
export class Description<Desc> {
private constructor(private description: Desc) {}
}
export function description<Desc>() {
return <Item>(item: Item) => item as Description<Desc> & Item;
}
export type DescriptionOf<
Item extends Description<any>
> = Item extends Description<infer Desc> ? Desc : unknown;
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