@ngneat/elf
Advanced tools
Comparing version 1.0.0-beta.4 to 1.0.0-beta.5
import { Reducer } from './store'; | ||
import { EmptyConfig } from './state'; | ||
import { OrArray } from './types'; | ||
export declare function propsArrayFactory<T extends any[], K extends string, Props extends { | ||
@@ -8,10 +9,9 @@ [Key in K]: T; | ||
config?: Config; | ||
idKey?: keyof T[0]; | ||
}): { [P in `with${Capitalize<K>}` | `update${Capitalize<K>}` | `set${Capitalize<K>}` | `reset${Capitalize<K>}` | `select${Capitalize<K>}` | `get${Capitalize<K>}`]: P extends `get${Capitalize<K>}` ? <S extends Props>(state: S) => T : P extends `select${Capitalize<K>}` ? <S_1 extends Props>() => import("rxjs").OperatorFunction<S_1, T> : P extends `reset${Capitalize<K>}` ? <S_2 extends Props>() => Reducer<S_2> : P extends `set${Capitalize<K>}` ? <S_3 extends Props>(value: T | ((state: S_3) => T)) => Reducer<S_3> : P extends `update${Capitalize<K>}` ? <S_4 extends Props>(value: Partial<T> | ((state: S_4) => Partial<T>)) => Reducer<S_4> : P extends `with${Capitalize<K>}` ? (initialValue?: T | undefined) => import("./state").PropsFactory<Props, Config> : any; } & { [P_1 in `remove${Capitalize<K>}` | `add${Capitalize<K>}` | `toggle${Capitalize<K>}`]: P_1 extends `toggle${Capitalize<K>}` ? <S_5 extends Props>(value: T[0]) => Reducer<S_5> : P_1 extends `add${Capitalize<K>}` ? <S_6 extends Props>(value: T[0]) => Reducer<S_6> : P_1 extends `remove${Capitalize<K>}` ? <S_7 extends Props>(value: T[0]) => Reducer<S_7> : never; }; | ||
export declare function arrayAdd<T extends any[]>(arr: T, item: T[0]): T; | ||
export declare function arrayRemove<T extends any[], IdKey extends keyof T[0]>(arr: T, id: T[0] extends Record<any, any> ? T[0][IdKey] : T[0], options?: T[0] extends Record<any, any> ? { | ||
idKey: IdKey; | ||
} : never): T; | ||
export declare function arrayToggle<T extends any[], IdKey extends keyof T[0]>(arr: T, item: T[0], options?: T[0] extends Record<any, any> ? { | ||
idKey: IdKey; | ||
} : never): T; | ||
}): Omit<{ [P in `with${Capitalize<K>}` | `update${Capitalize<K>}` | `set${Capitalize<K>}` | `reset${Capitalize<K>}` | `select${Capitalize<K>}` | `get${Capitalize<K>}`]: P extends `get${Capitalize<K>}` ? <S extends Props>(state: S) => T : P extends `select${Capitalize<K>}` ? <S_1 extends Props>() => import("rxjs").OperatorFunction<S_1, T> : P extends `reset${Capitalize<K>}` ? <S_2 extends Props>() => Reducer<S_2> : P extends `set${Capitalize<K>}` ? <S_3 extends Props>(value: T | ((state: S_3) => T)) => Reducer<S_3> : P extends `update${Capitalize<K>}` ? <S_4 extends Props>(value: Partial<T> | ((state: S_4) => Partial<T>)) => Reducer<S_4> : P extends `with${Capitalize<K>}` ? (initialValue?: T | undefined) => import("./state").PropsFactory<Props, Config> : any; }, `update${Capitalize<K>}`> & { [P_1 in `update${Capitalize<K>}` | `remove${Capitalize<K>}` | `add${Capitalize<K>}` | `in${Capitalize<K>}` | `toggle${Capitalize<K>}`]: P_1 extends `toggle${Capitalize<K>}` ? <S_5 extends Props>(value: OrArray<T[0]>) => Reducer<S_5> : P_1 extends `add${Capitalize<K>}` ? <S_6 extends Props>(value: OrArray<T[0]>) => Reducer<S_6> : P_1 extends `remove${Capitalize<K>}` ? <S_7 extends Props>(value: OrArray<T[0]>) => Reducer<S_7> : P_1 extends `update${Capitalize<K>}` ? <S_8 extends Props>(item: T[0], newItem: T[0]) => Reducer<S_8> : P_1 extends `in${Capitalize<K>}` ? (value: T[0]) => <S_9 extends Props>(state: S_9) => boolean : never; }; | ||
declare type OnlyPrimitive<T extends any[]> = T[0] extends Record<any, any> ? never : T; | ||
export declare function arrayAdd<T extends any[]>(arr: OnlyPrimitive<T>, items: OrArray<T[0]>): T; | ||
export declare function arrayRemove<T extends any[]>(arr: OnlyPrimitive<T>, items: OrArray<T[0]>): T; | ||
export declare function arrayToggle<T extends any[]>(arr: OnlyPrimitive<T>, items: OrArray<T[0]>): T; | ||
export declare function inArray<T extends any[]>(arr: OnlyPrimitive<T>, item: T[0]): boolean; | ||
export declare function arrayUpdate<T extends any[]>(arr: OnlyPrimitive<T>, item: T[0], newItem: T[0]): T; | ||
export {}; |
{ | ||
"name": "@ngneat/elf", | ||
"version": "1.0.0-beta.4", | ||
"version": "1.0.0-beta.5", | ||
"description": "A reactive state management for JS applications", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -38,3 +38,3 @@ <p align="center"> | ||
## 👩🎓 Check out the React Todos [example](https://stackblitz.com/edit/react-ts-jidhej?file=todos/todos.tsx) | ||
## 😋 Check out the Angular Todos [example](https://stackblitz.com/edit/angular-ivy-sky1gb?file=src/app/todos/state/todos.repository.ts) | ||
## 😋 Check out the Angular Todos [example](https://stackblitz.com/edit/angular-ivy-sky1gb?file=src/app/todos/state/todos.repository.ts) or Books [example](https://stackblitz.com/edit/angular-ivy-j9azue?file=src/app/state/books.repository.ts) | ||
@@ -41,0 +41,0 @@ <br > |
@@ -235,18 +235,10 @@ import { Subject, BehaviorSubject, Observable, pipe, asapScheduler } from 'rxjs'; | ||
function propsArrayFactory(key, options) { | ||
const normalizedKey = capitalize(key); // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { | ||
idKey, | ||
...baseOptions | ||
} = options; | ||
const base = propsFactory(key, baseOptions); | ||
const arrayOptions = { | ||
idKey: options?.idKey ?? 'id' | ||
}; | ||
const normalizedKey = capitalize(key); | ||
const base = propsFactory(key, options); | ||
return { ...base, | ||
[`add${normalizedKey}`](value) { | ||
[`add${normalizedKey}`](items) { | ||
return function (state) { | ||
return { ...state, | ||
[key]: arrayAdd(state[key], value) | ||
[key]: arrayAdd(state[key], items) | ||
}; | ||
@@ -256,6 +248,6 @@ }; | ||
[`remove${normalizedKey}`](id) { | ||
[`remove${normalizedKey}`](items) { | ||
return function (state) { | ||
return { ...state, | ||
[key]: arrayRemove(state[key], id, arrayOptions) | ||
[key]: arrayRemove(state[key], items) | ||
}; | ||
@@ -265,8 +257,20 @@ }; | ||
[`toggle${normalizedKey}`](id) { | ||
[`toggle${normalizedKey}`](items) { | ||
return function (state) { | ||
return { ...state, | ||
[key]: arrayToggle(state[key], id, arrayOptions) | ||
[key]: arrayToggle(state[key], items) | ||
}; | ||
}; | ||
}, | ||
[`update${normalizedKey}`](predicateOrIds, obj) { | ||
return function (state) { | ||
return { ...state, | ||
[key]: arrayUpdate(state[key], predicateOrIds, obj) | ||
}; | ||
}; | ||
}, | ||
[`in${normalizedKey}`](item) { | ||
return state => inArray(state[key], item); | ||
} | ||
@@ -276,27 +280,25 @@ | ||
} | ||
function arrayAdd(arr, item) { | ||
return [...arr, item]; | ||
function arrayAdd(arr, items) { | ||
return [...arr, ...coerceArray(items)]; | ||
} | ||
function arrayRemove(arr, id, options) { | ||
const isPrimitive = !isObject(arr[0]); | ||
const idKey = options?.idKey ?? 'id'; | ||
return arr.filter(current => { | ||
if (isPrimitive) { | ||
return id !== current; | ||
} | ||
return current[idKey] !== id; | ||
function arrayRemove(arr, items) { | ||
const toArray = coerceArray(items); | ||
return arr.filter(current => !toArray.includes(current)); | ||
} | ||
function arrayToggle(arr, items) { | ||
const toArray = coerceArray(items); | ||
const result = [...arr]; | ||
toArray.forEach(item => { | ||
const i = result.indexOf(item); | ||
i > -1 ? result.splice(i, 1) : result.push(item); | ||
}); | ||
return result; | ||
} | ||
function arrayToggle(arr, item, options) { | ||
const isPrimitive = !isObject(arr[0]); | ||
const idKey = options?.idKey ?? 'id'; | ||
const isExists = arr.find(current => { | ||
if (isPrimitive) { | ||
return current === item; | ||
} | ||
return item[idKey] === current[idKey]; | ||
function inArray(arr, item) { | ||
return arr.includes(item); | ||
} | ||
function arrayUpdate(arr, item, newItem) { | ||
return arr.map(current => { | ||
return current === item ? newItem : current; | ||
}); | ||
return isExists ? arrayRemove(arr, isPrimitive ? item : item[idKey], options) : arrayAdd(arr, item); | ||
} | ||
@@ -303,0 +305,0 @@ |
@@ -49,14 +49,2 @@ (function (global, factory) { | ||
function __rest(s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
} | ||
function __spreadArray(to, from) { | ||
@@ -329,63 +317,62 @@ for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) | ||
var _b; | ||
var normalizedKey = capitalize(key); | ||
var base = propsFactory(key, options); | ||
return __assign(__assign({}, base), (_a = {}, _a["add" + normalizedKey] = function (items) { | ||
return function (state) { | ||
var _a; | ||
var normalizedKey = capitalize(key); // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
options.idKey; | ||
var baseOptions = __rest(options, ["idKey"]); | ||
var base = propsFactory(key, baseOptions); | ||
var arrayOptions = { | ||
idKey: (_b = options === null || options === void 0 ? void 0 : options.idKey) !== null && _b !== void 0 ? _b : 'id' | ||
}; | ||
return __assign(__assign({}, base), (_a = {}, _a["add" + normalizedKey] = function (value) { | ||
return __assign(__assign({}, state), (_a = {}, _a[key] = arrayAdd(state[key], items), _a)); | ||
}; | ||
}, _a["remove" + normalizedKey] = function (items) { | ||
return function (state) { | ||
var _a; | ||
return __assign(__assign({}, state), (_a = {}, _a[key] = arrayAdd(state[key], value), _a)); | ||
return __assign(__assign({}, state), (_a = {}, _a[key] = arrayRemove(state[key], items), _a)); | ||
}; | ||
}, _a["remove" + normalizedKey] = function (id) { | ||
}, _a["toggle" + normalizedKey] = function (items) { | ||
return function (state) { | ||
var _a; | ||
return __assign(__assign({}, state), (_a = {}, _a[key] = arrayRemove(state[key], id, arrayOptions), _a)); | ||
return __assign(__assign({}, state), (_a = {}, _a[key] = arrayToggle(state[key], items), _a)); | ||
}; | ||
}, _a["toggle" + normalizedKey] = function (id) { | ||
}, _a["update" + normalizedKey] = function (predicateOrIds, obj) { | ||
return function (state) { | ||
var _a; | ||
return __assign(__assign({}, state), (_a = {}, _a[key] = arrayToggle(state[key], id, arrayOptions), _a)); | ||
return __assign(__assign({}, state), (_a = {}, _a[key] = arrayUpdate(state[key], predicateOrIds, obj), _a)); | ||
}; | ||
}, _a["in" + normalizedKey] = function (item) { | ||
return function (state) { | ||
return inArray(state[key], item); | ||
}; | ||
}, _a)); | ||
} | ||
function arrayAdd(arr, item) { | ||
return __spreadArray(__spreadArray([], arr), [item]); | ||
function arrayAdd(arr, items) { | ||
return __spreadArray(__spreadArray([], arr), coerceArray(items)); | ||
} | ||
function arrayRemove(arr, id, options) { | ||
var _a; | ||
var isPrimitive = !isObject(arr[0]); | ||
var idKey = (_a = options === null || options === void 0 ? void 0 : options.idKey) !== null && _a !== void 0 ? _a : 'id'; | ||
function arrayRemove(arr, items) { | ||
var toArray = coerceArray(items); | ||
return arr.filter(function (current) { | ||
if (isPrimitive) { | ||
return id !== current; | ||
} | ||
return current[idKey] !== id; | ||
return !toArray.includes(current); | ||
}); | ||
} | ||
function arrayToggle(arr, item, options) { | ||
var _a; | ||
function arrayToggle(arr, items) { | ||
var toArray = coerceArray(items); | ||
var isPrimitive = !isObject(arr[0]); | ||
var idKey = (_a = options === null || options === void 0 ? void 0 : options.idKey) !== null && _a !== void 0 ? _a : 'id'; | ||
var isExists = arr.find(function (current) { | ||
if (isPrimitive) { | ||
return current === item; | ||
} | ||
var result = __spreadArray([], arr); | ||
return item[idKey] === current[idKey]; | ||
toArray.forEach(function (item) { | ||
var i = result.indexOf(item); | ||
i > -1 ? result.splice(i, 1) : result.push(item); | ||
}); | ||
return isExists ? arrayRemove(arr, isPrimitive ? item : item[idKey], options) : arrayAdd(arr, item); | ||
return result; | ||
} | ||
function inArray(arr, item) { | ||
return arr.includes(item); | ||
} | ||
function arrayUpdate(arr, item, newItem) { | ||
return arr.map(function (current) { | ||
return current === item ? newItem : current; | ||
}); | ||
} | ||
@@ -392,0 +379,0 @@ function withProps(props) { |
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
31528
747