@solid-primitives/utils 6.2.3 → 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,213 @@
1
+ import { getOwner, onCleanup, createSignal, untrack, sharedConfig, onMount, DEV, equalFn, } from "solid-js";
2
+ import { isServer } from "solid-js/web";
3
+ export * from "./types.js";
4
+ //
5
+ // GENERAL HELPERS:
6
+ //
7
+ export { isServer };
8
+ export const isClient = !isServer;
9
+ export const isDev = isClient && !!DEV;
10
+ export const isProd = !isDev;
11
+ /** no operation */
12
+ export const noop = (() => void 0);
13
+ export const trueFn = () => true;
14
+ export const falseFn = () => false;
15
+ /** @deprecated use {@link equalFn} from "solid-js" */
16
+ export const defaultEquals = equalFn;
17
+ export const EQUALS_FALSE_OPTIONS = { equals: false };
18
+ export const INTERNAL_OPTIONS = { internal: true };
19
+ /**
20
+ * Check if the value is an instance of ___
21
+ */
22
+ export const ofClass = (v, c) => v instanceof c || (v && v.constructor === c);
23
+ /** Check if value is typeof "object" or "function" */
24
+ export function isObject(value) {
25
+ return value !== null && (typeof value === "object" || typeof value === "function");
26
+ }
27
+ export const isNonNullable = (i) => i != null;
28
+ export const filterNonNullable = (arr) => arr.filter(isNonNullable);
29
+ export const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
30
+ /**
31
+ * Check shallow array equality
32
+ */
33
+ export const arrayEquals = (a, b) => a === b || (a.length === b.length && a.every((e, i) => e === b[i]));
34
+ /**
35
+ * Returns a function that will call all functions in the order they were chained with the same arguments.
36
+ */
37
+ export function chain(callbacks) {
38
+ return (...args) => {
39
+ for (const callback of callbacks)
40
+ callback && callback(...args);
41
+ };
42
+ }
43
+ /**
44
+ * Returns a function that will call all functions in the reversed order with the same arguments.
45
+ */
46
+ export function reverseChain(callbacks) {
47
+ return (...args) => {
48
+ for (let i = callbacks.length - 1; i >= 0; i--) {
49
+ const callback = callbacks[i];
50
+ callback && callback(...args);
51
+ }
52
+ };
53
+ }
54
+ export const clamp = (n, min, max) => Math.min(Math.max(n, min), max);
55
+ /**
56
+ * Accesses the value of a MaybeAccessor
57
+ * @example
58
+ * ```ts
59
+ * access("foo") // => "foo"
60
+ * access(() => "foo") // => "foo"
61
+ * ```
62
+ */
63
+ export const access = (v) => typeof v === "function" && !v.length ? v() : v;
64
+ export const asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
65
+ /**
66
+ * Access an array of MaybeAccessors
67
+ * @example
68
+ * const list = [1, 2, () => 3)] // T: MaybeAccessor<number>[]
69
+ * const newList = accessArray(list) // T: number[]
70
+ */
71
+ export const accessArray = (list) => list.map(v => access(v));
72
+ /**
73
+ * Run the function if the accessed value is not `undefined` nor `null`
74
+ * @param value
75
+ * @param fn
76
+ */
77
+ export const withAccess = (value, fn) => {
78
+ const _value = access(value);
79
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
80
+ typeof _value != null && fn(_value);
81
+ };
82
+ export const asAccessor = (v) => (typeof v === "function" ? v : () => v);
83
+ /** If value is a function – call it with a given arguments – otherwise get the value as is */
84
+ export function accessWith(valueOrFn, ...args) {
85
+ return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
86
+ }
87
+ export function defer(deps, fn, initialValue) {
88
+ const isArray = Array.isArray(deps);
89
+ let prevInput;
90
+ let shouldDefer = true;
91
+ return prevValue => {
92
+ let input;
93
+ if (isArray) {
94
+ input = Array(deps.length);
95
+ for (let i = 0; i < deps.length; i++)
96
+ input[i] = deps[i]();
97
+ }
98
+ else
99
+ input = deps();
100
+ if (shouldDefer) {
101
+ shouldDefer = false;
102
+ prevInput = input;
103
+ return initialValue;
104
+ }
105
+ const result = untrack(() => fn(input, prevInput, prevValue));
106
+ prevInput = input;
107
+ return result;
108
+ };
109
+ }
110
+ /**
111
+ * Get entries of an object
112
+ */
113
+ export const entries = Object.entries;
114
+ /**
115
+ * Get keys of an object
116
+ */
117
+ export const keys = Object.keys;
118
+ /**
119
+ * Solid's `onCleanup` that doesn't warn in development if used outside of a component.
120
+ */
121
+ export const tryOnCleanup = isDev
122
+ ? fn => (getOwner() ? onCleanup(fn) : fn)
123
+ : onCleanup;
124
+ export const createCallbackStack = () => {
125
+ let stack = [];
126
+ const clear = () => (stack = []);
127
+ return {
128
+ push: (...callbacks) => stack.push(...callbacks),
129
+ execute(arg0, arg1, arg2, arg3) {
130
+ stack.forEach(cb => cb(arg0, arg1, arg2, arg3));
131
+ clear();
132
+ },
133
+ clear,
134
+ };
135
+ };
136
+ /**
137
+ * Group synchronous function calls.
138
+ * @param fn
139
+ * @returns `fn`
140
+ */
141
+ export function createMicrotask(fn) {
142
+ let calls = 0;
143
+ let args;
144
+ onCleanup(() => (calls = 0));
145
+ return (...a) => {
146
+ (args = a), calls++;
147
+ queueMicrotask(() => --calls === 0 && fn(...args));
148
+ };
149
+ }
150
+ /**
151
+ * A hydratable version of the {@link createSignal}. It will use the serverValue on the server and the update function on the client. If initialized during hydration it will use serverValue as the initial value and update it once hydration is complete.
152
+ *
153
+ * @param serverValue initial value of the state on the server
154
+ * @param update called once on the client or on hydration to initialize the value
155
+ * @param options {@link SignalOptions}
156
+ * @returns
157
+ * ```ts
158
+ * [state: Accessor<T>, setState: Setter<T>]
159
+ * ```
160
+ * @see {@link createSignal}
161
+ */
162
+ export function createHydratableSignal(serverValue, update, options) {
163
+ if (isServer) {
164
+ return createSignal(serverValue, options);
165
+ }
166
+ if (sharedConfig.context) {
167
+ const [state, setState] = createSignal(serverValue, options);
168
+ onMount(() => setState(() => update()));
169
+ return [state, setState];
170
+ }
171
+ return createSignal(update(), options);
172
+ }
173
+ /** @deprecated use {@link createHydratableSignal} instead */
174
+ export const createHydrateSignal = createHydratableSignal;
175
+ /**
176
+ * Handle items removed and added to the array by diffing it by refference.
177
+ *
178
+ * @param current new array instance
179
+ * @param prev previous array copy
180
+ * @param handleAdded called once for every added item to array
181
+ * @param handleRemoved called once for every removed from array
182
+ */
183
+ export function handleDiffArray(current, prev, handleAdded, handleRemoved) {
184
+ const currLength = current.length;
185
+ const prevLength = prev.length;
186
+ let i = 0;
187
+ if (!prevLength) {
188
+ for (; i < currLength; i++)
189
+ handleAdded(current[i]);
190
+ return;
191
+ }
192
+ if (!currLength) {
193
+ for (; i < prevLength; i++)
194
+ handleRemoved(prev[i]);
195
+ return;
196
+ }
197
+ for (; i < prevLength; i++) {
198
+ if (prev[i] !== current[i])
199
+ break;
200
+ }
201
+ let prevEl;
202
+ let currEl;
203
+ prev = prev.slice(i);
204
+ current = current.slice(i);
205
+ for (prevEl of prev) {
206
+ if (!current.includes(prevEl))
207
+ handleRemoved(prevEl);
208
+ }
209
+ for (currEl of current) {
210
+ if (!prev.includes(currEl))
211
+ handleAdded(currEl);
212
+ }
213
+ }
@@ -0,0 +1,89 @@
1
+ import type { Accessor, Setter } from "solid-js";
2
+ export type { EffectOptions, OnOptions } from "solid-js";
3
+ export type { ResolvedJSXElement, ResolvedChildren } from "solid-js/types/reactive/signal.js";
4
+ /**
5
+ * Can be single or in an array
6
+ */
7
+ export type Many<T> = T | T[];
8
+ export type Values<O extends Object> = O[keyof O];
9
+ export type Noop = (...a: any[]) => void;
10
+ export type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
11
+ /**
12
+ * Infers the type of the array elements
13
+ */
14
+ export type ItemsOf<T> = T extends (infer E)[] ? E : never;
15
+ export type ItemsOfMany<T> = T extends any[] ? ItemsOf<T> : T;
16
+ export type SetterParam<T> = Parameters<Setter<T>>[0];
17
+ /**
18
+ * T or a reactive/non-reactive function returning T
19
+ */
20
+ export type MaybeAccessor<T> = T | Accessor<T>;
21
+ /**
22
+ * Accessed value of a MaybeAccessor
23
+ * @example
24
+ * ```ts
25
+ * MaybeAccessorValue<MaybeAccessor<string>>
26
+ * // => string
27
+ * MaybeAccessorValue<MaybeAccessor<() => string>>
28
+ * // => string | (() => string)
29
+ * MaybeAccessorValue<MaybeAccessor<string> | Function>
30
+ * // => string | void
31
+ * ```
32
+ */
33
+ export type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends () => any ? ReturnType<T> : T;
34
+ export type OnAccessEffectFunction<S, Prev, Next extends Prev = Prev> = (input: AccessReturnTypes<S>, prevInput: AccessReturnTypes<S>, v: Prev) => Next;
35
+ export type AccessReturnTypes<S> = S extends MaybeAccessor<any>[] ? {
36
+ [I in keyof S]: AccessReturnTypes<S[I]>;
37
+ } : MaybeAccessorValue<S>;
38
+ /** Allows to make shallow overwrites to an interface */
39
+ export type Modify<T, R> = Omit<T, keyof R> & R;
40
+ /** Allows to make nested overwrites to an interface */
41
+ export type ModifyDeep<A extends AnyObject, B extends DeepPartialAny<A>> = {
42
+ [K in keyof A]: B[K] extends never ? A[K] : B[K] extends AnyObject ? ModifyDeep<A[K], B[K]> : B[K];
43
+ } & (A extends AnyObject ? Omit<B, keyof A> : A);
44
+ /** Makes each property optional and turns each leaf property into any, allowing for type overrides by narrowing any. */
45
+ export type DeepPartialAny<T> = {
46
+ [P in keyof T]?: T[P] extends AnyObject ? DeepPartialAny<T[P]> : any;
47
+ };
48
+ /** Removes the `[...list]` functionality */
49
+ export type NonIterable<T> = T & {
50
+ [Symbol.iterator]: never;
51
+ };
52
+ /** Get the required keys of an object */
53
+ export type RequiredKeys<T> = keyof {
54
+ [K in keyof T as T extends {
55
+ [_ in K]: unknown;
56
+ } ? K : never]: 0;
57
+ };
58
+ /** Remove the first item of a tuple [1, 2, 3, 4] => [2, 3, 4] */
59
+ export type Tail<T extends any[]> = ((...t: T) => void) extends (x: any, ...u: infer U) => void ? U : never;
60
+ /** `A | B => A & B` */
61
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
62
+ export type ExtractIfPossible<T, U> = Extract<T, U> extends never ? U : Extract<T, U>;
63
+ export type AnyObject = Record<PropertyKey, any>;
64
+ export type AnyStatic = [] | any[] | AnyObject;
65
+ export type AnyFunction = (...args: any[]) => any;
66
+ export type AnyClass = abstract new (...args: any) => any;
67
+ export type PrimitiveValue = PropertyKey | boolean | bigint | null | undefined;
68
+ export type FalsyValue = false | 0 | "" | null | undefined;
69
+ export type Truthy<T> = Exclude<T, FalsyValue>;
70
+ export type Falsy<T> = Extract<T, FalsyValue>;
71
+ export type Position = {
72
+ x: number;
73
+ y: number;
74
+ };
75
+ export type Size = {
76
+ width: number;
77
+ height: number;
78
+ };
79
+ /** Unwraps the type definition of an object, making it more readable */
80
+ export type Simplify<T> = T extends object ? {
81
+ [K in keyof T]: T[K];
82
+ } : T;
83
+ /** Unboxes type definition, making it more readable */
84
+ export type UnboxLazy<T> = T extends () => infer U ? U : T;
85
+ type RawNarrow<T> = (T extends [] ? [] : never) | (T extends string | number | bigint | boolean ? T : never) | {
86
+ [K in keyof T]: T[K] extends Function ? T[K] : RawNarrow<T[K]>;
87
+ };
88
+ export type Narrow<T> = T extends [] ? T : RawNarrow<T>;
89
+ export type NoInfer<T> = [T][T extends any ? 0 : never];
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "6.2.3",
3
+ "version": "6.3.0",
4
4
  "description": "A bunch of reactive utility types and functions, for building primitives with Solid.js",
5
5
  "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>",
6
6
  "contributors": [
@@ -21,29 +21,22 @@
21
21
  "dist"
22
22
  ],
23
23
  "type": "module",
24
- "main": "./dist/index/index.cjs",
25
- "module": "./dist/index/index.js",
26
- "types": "./dist/index/index.d.ts",
24
+ "module": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
27
26
  "browser": {},
28
27
  "exports": {
29
28
  ".": {
29
+ "@solid-primitives/source": "./src/index.ts",
30
30
  "import": {
31
- "types": "./dist/index/index.d.ts",
32
- "default": "./dist/index/index.js"
33
- },
34
- "require": {
35
- "types": "./dist/index/index.d.cts",
36
- "default": "./dist/index/index.cjs"
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
37
33
  }
38
34
  },
39
35
  "./immutable": {
36
+ "@solid-primitives/source": "./src/immutable/index.ts",
40
37
  "import": {
41
38
  "types": "./dist/immutable/index.d.ts",
42
39
  "default": "./dist/immutable/index.js"
43
- },
44
- "require": {
45
- "types": "./dist/immutable/index.d.cts",
46
- "default": "./dist/immutable/index.cjs"
47
40
  }
48
41
  }
49
42
  },
@@ -1,139 +0,0 @@
1
- import { DEV, equalFn, untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount } from 'solid-js';
2
- import { isServer } from 'solid-js/web';
3
- export { isServer } from 'solid-js/web';
4
-
5
- // src/index.ts
6
- var isClient = !isServer;
7
- var isDev = isClient && !!DEV;
8
- var isProd = !isDev;
9
- var noop = () => void 0;
10
- var trueFn = () => true;
11
- var falseFn = () => false;
12
- var defaultEquals = equalFn;
13
- var EQUALS_FALSE_OPTIONS = { equals: false };
14
- var INTERNAL_OPTIONS = { internal: true };
15
- var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
16
- function isObject(value) {
17
- return value !== null && (typeof value === "object" || typeof value === "function");
18
- }
19
- var isNonNullable = (i) => i != null;
20
- var filterNonNullable = (arr) => arr.filter(isNonNullable);
21
- var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
22
- var arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
23
- function chain(callbacks) {
24
- return (...args) => {
25
- for (const callback of callbacks)
26
- callback && callback(...args);
27
- };
28
- }
29
- function reverseChain(callbacks) {
30
- return (...args) => {
31
- for (let i = callbacks.length - 1; i >= 0; i--) {
32
- const callback = callbacks[i];
33
- callback && callback(...args);
34
- }
35
- };
36
- }
37
- var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
38
- var access = (v) => typeof v === "function" && !v.length ? v() : v;
39
- var asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
40
- var accessArray = (list) => list.map((v) => access(v));
41
- var withAccess = (value, fn) => {
42
- const _value = access(value);
43
- typeof _value != null && fn(_value);
44
- };
45
- var asAccessor = (v) => typeof v === "function" ? v : () => v;
46
- function accessWith(valueOrFn, ...args) {
47
- return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
48
- }
49
- function defer(deps, fn, initialValue) {
50
- const isArray = Array.isArray(deps);
51
- let prevInput;
52
- let shouldDefer = true;
53
- return (prevValue) => {
54
- let input;
55
- if (isArray) {
56
- input = Array(deps.length);
57
- for (let i = 0; i < deps.length; i++)
58
- input[i] = deps[i]();
59
- } else
60
- input = deps();
61
- if (shouldDefer) {
62
- shouldDefer = false;
63
- prevInput = input;
64
- return initialValue;
65
- }
66
- const result = untrack(() => fn(input, prevInput, prevValue));
67
- prevInput = input;
68
- return result;
69
- };
70
- }
71
- var entries = Object.entries;
72
- var keys = Object.keys;
73
- var tryOnCleanup = isDev ? (fn) => getOwner() ? onCleanup(fn) : fn : onCleanup;
74
- var createCallbackStack = () => {
75
- let stack = [];
76
- const clear = () => stack = [];
77
- return {
78
- push: (...callbacks) => stack.push(...callbacks),
79
- execute(arg0, arg1, arg2, arg3) {
80
- stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
81
- clear();
82
- },
83
- clear
84
- };
85
- };
86
- function createMicrotask(fn) {
87
- let calls = 0;
88
- let args;
89
- onCleanup(() => calls = 0);
90
- return (...a) => {
91
- args = a, calls++;
92
- queueMicrotask(() => --calls === 0 && fn(...args));
93
- };
94
- }
95
- function createHydratableSignal(serverValue, update, options) {
96
- if (isServer) {
97
- return createSignal(serverValue, options);
98
- }
99
- if (sharedConfig.context) {
100
- const [state, setState] = createSignal(serverValue, options);
101
- onMount(() => setState(() => update()));
102
- return [state, setState];
103
- }
104
- return createSignal(update(), options);
105
- }
106
- var createHydrateSignal = createHydratableSignal;
107
- function handleDiffArray(current, prev, handleAdded, handleRemoved) {
108
- const currLength = current.length;
109
- const prevLength = prev.length;
110
- let i = 0;
111
- if (!prevLength) {
112
- for (; i < currLength; i++)
113
- handleAdded(current[i]);
114
- return;
115
- }
116
- if (!currLength) {
117
- for (; i < prevLength; i++)
118
- handleRemoved(prev[i]);
119
- return;
120
- }
121
- for (; i < prevLength; i++) {
122
- if (prev[i] !== current[i])
123
- break;
124
- }
125
- let prevEl;
126
- let currEl;
127
- prev = prev.slice(i);
128
- current = current.slice(i);
129
- for (prevEl of prev) {
130
- if (!current.includes(prevEl))
131
- handleRemoved(prevEl);
132
- }
133
- for (currEl of current) {
134
- if (!prev.includes(currEl))
135
- handleAdded(currEl);
136
- }
137
- }
138
-
139
- export { EQUALS_FALSE_OPTIONS, INTERNAL_OPTIONS, access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydrateSignal, createMicrotask, defaultEquals, defer, entries, falseFn, filterNonNullable, handleDiffArray, isClient, isDev, isNonNullable, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
@@ -1,161 +0,0 @@
1
- 'use strict';
2
-
3
- require('solid-js');
4
- require('solid-js/web');
5
-
6
- // src/immutable/copy.ts
7
- exports.shallowArrayCopy = (array) => array.slice();
8
- exports.shallowObjectCopy = (object) => Object.assign({}, object);
9
- exports.shallowCopy = (source) => Array.isArray(source) ? exports.shallowArrayCopy(source) : exports.shallowObjectCopy(source);
10
- exports.withArrayCopy = (array, mutator) => {
11
- const copy = exports.shallowArrayCopy(array);
12
- mutator(copy);
13
- return copy;
14
- };
15
- exports.withObjectCopy = (object, mutator) => {
16
- const copy = exports.shallowObjectCopy(object);
17
- mutator(copy);
18
- return copy;
19
- };
20
- exports.withCopy = (source, mutator) => Array.isArray(source) ? exports.withArrayCopy(source, mutator) : exports.withObjectCopy(source, mutator);
21
-
22
- // src/immutable/number.ts
23
- function add(...a) {
24
- let r = 0;
25
- for (const n of a) {
26
- r += n;
27
- }
28
- return r;
29
- }
30
- exports.substract = (a, ...b) => {
31
- for (const n of b) {
32
- a -= n;
33
- }
34
- return a;
35
- };
36
- exports.multiply = (a, ...b) => {
37
- for (const n of b) {
38
- a *= n;
39
- }
40
- return a;
41
- };
42
- exports.divide = (a, ...b) => {
43
- for (const n of b) {
44
- a /= n;
45
- }
46
- return a;
47
- };
48
- exports.power = (a, ...b) => {
49
- for (const n of b) {
50
- a = a ** n;
51
- }
52
- return a;
53
- };
54
- exports.clamp = (n, min, max) => Math.min(max, Math.max(min, n));
55
-
56
- // src/immutable/update.ts
57
- exports.update = (...args) => exports.withCopy(args[0], (obj) => {
58
- if (args.length > 3)
59
- obj[args[1]] = exports.update(obj[args[1]], ...args.slice(2));
60
- else if (typeof args[2] === "function")
61
- obj[args[1]] = args[2](obj[args[1]]);
62
- else
63
- obj[args[1]] = args[2];
64
- });
65
-
66
- // src/immutable/object.ts
67
- exports.omit = (object, ...keys) => exports.withObjectCopy(object, (object2) => keys.forEach((key) => delete object2[key]));
68
- exports.pick = (object, ...keys) => keys.reduce(
69
- (n, k) => {
70
- if (k in object)
71
- n[k] = object[k];
72
- return n;
73
- },
74
- {}
75
- );
76
- function get(obj, ...keys) {
77
- let res = obj;
78
- for (const key of keys) {
79
- res = res[key];
80
- }
81
- return res;
82
- }
83
- function split(object, ...list) {
84
- const _list = typeof list[0] === "string" ? [list] : list;
85
- const copy = exports.shallowObjectCopy(object);
86
- const result = [];
87
- for (let i = 0; i < _list.length; i++) {
88
- const keys = _list[i];
89
- result.push({});
90
- for (const key of keys) {
91
- result[i][key] = copy[key];
92
- delete copy[key];
93
- }
94
- }
95
- return [...result, copy];
96
- }
97
- function merge(...objects) {
98
- const result = {};
99
- for (const obj of objects) {
100
- Object.assign(result, obj);
101
- }
102
- return result;
103
- }
104
- var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
105
- var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
106
-
107
- // src/immutable/array.ts
108
- exports.push = (list, ...items) => exports.withArrayCopy(list, (list2) => list2.push(...items));
109
- exports.drop = (list, n = 1) => list.slice(n);
110
- exports.dropRight = (list, n = 1) => list.slice(0, list.length - n);
111
- exports.filterOut = (list, item) => filter(list, (i) => i !== item);
112
- function filter(list, predicate) {
113
- const newList = list.filter(predicate);
114
- newList.removed = list.length - newList.length;
115
- return newList;
116
- }
117
- exports.sort = (list, compareFn) => list.slice().sort(compareFn);
118
- exports.map = (list, mapFn) => list.map(mapFn);
119
- exports.slice = (list, start, end) => list.slice(start, end);
120
- exports.splice = (list, start, deleteCount = 0, ...items) => exports.withArrayCopy(list, (list2) => list2.splice(start, deleteCount, ...items));
121
- exports.fill = (list, value, start, end) => list.slice().fill(value, start, end);
122
- function concat(...a) {
123
- const result = [];
124
- for (const i in a) {
125
- Array.isArray(a[i]) ? result.push(...a[i]) : result.push(a[i]);
126
- }
127
- return result;
128
- }
129
- exports.remove = (list, item, ...insertItems) => {
130
- const index = list.indexOf(item);
131
- return exports.splice(list, index, 1, ...insertItems);
132
- };
133
- exports.removeItems = (list, ...items) => {
134
- const res = [];
135
- for (let i = 0; i < list.length; i++) {
136
- const item = list[i];
137
- const ii = items.indexOf(item);
138
- if (ii !== -1)
139
- items.splice(ii, 1);
140
- else
141
- res.push(item);
142
- }
143
- return res;
144
- };
145
- exports.flatten = (arr) => arr.reduce((flat, next) => flat.concat(Array.isArray(next) ? exports.flatten(next) : next), []);
146
- exports.sortBy = (arr, ...paths) => exports.flatten(paths).reduce(
147
- (source, path) => exports.sort(
148
- source,
149
- (a, b) => typeof path === "function" ? compare(path(a), path(b)) : compare(get(a, path), get(b, path))
150
- ),
151
- arr
152
- );
153
- exports.filterInstance = (list, ...classes) => classes.length === 1 ? list.filter((item) => ofClass(item, classes[0])) : list.filter((item) => item && classes.some((c) => ofClass(item, c)));
154
- exports.filterOutInstance = (list, ...classes) => classes.length === 1 ? list.filter((item) => item && !ofClass(item, classes[0])) : list.filter((item) => item && !classes.some((c) => ofClass(item, c)));
155
-
156
- exports.add = add;
157
- exports.concat = concat;
158
- exports.filter = filter;
159
- exports.get = get;
160
- exports.merge = merge;
161
- exports.split = split;