@solid-primitives/utils 7.0.0-next.1 → 7.0.0-next.3

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.
@@ -1,54 +1,52 @@
1
- import { withObjectCopy, shallowObjectCopy } from "./copy.js";
2
- import {} from "../index.js";
1
+ /* @ts-self-types="./object.d.ts" */
2
+ import { shallowObjectCopy, withObjectCopy } from "./copy.js";
3
+ //#region src/immutable/object.ts
3
4
  /**
4
- * Create a new subset object without the provided keys
5
- *
6
- * @example
7
- * ```ts
8
- * const newObject = omit({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
9
- * newObject // => { c: "baz" }
10
- * ```
11
- */
12
- export const omit = (object, ...keys) => withObjectCopy(object, object => keys.forEach(key => delete object[key]));
5
+ * Create a new subset object without the provided keys
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const newObject = omit({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
10
+ * newObject // => { c: "baz" }
11
+ * ```
12
+ */
13
+ const omit = (object, ...keys) => withObjectCopy(object, (object) => keys.forEach((key) => delete object[key]));
13
14
  /**
14
- * Create a new subset object with only the provided keys
15
- *
16
- * @example
17
- * ```ts
18
- * const newObject = pick({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
19
- * newObject // => { a:"foo", b:"bar" }
20
- * ```
21
- */
22
- export const pick = (object, ...keys) => keys.reduce((n, k) => {
23
- if (k in object)
24
- n[k] = object[k];
25
- return n;
15
+ * Create a new subset object with only the provided keys
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * const newObject = pick({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
20
+ * newObject // => { a:"foo", b:"bar" }
21
+ * ```
22
+ */
23
+ const pick = (object, ...keys) => keys.reduce((n, k) => {
24
+ if (k in object) n[k] = object[k];
25
+ return n;
26
26
  }, {});
27
- export function get(obj, ...keys) {
28
- let res = obj;
29
- for (const key of keys) {
30
- res = res[key];
31
- }
32
- return res;
27
+ function get(obj, ...keys) {
28
+ let res = obj;
29
+ for (const key of keys) res = res[key];
30
+ return res;
33
31
  }
34
- export function split(object, ...list) {
35
- const _list = (typeof list[0] === "string" ? [list] : list);
36
- const copy = shallowObjectCopy(object);
37
- const result = [];
38
- for (let i = 0; i < _list.length; i++) {
39
- const keys = _list[i];
40
- result.push({});
41
- for (const key of keys) {
42
- result[i][key] = copy[key];
43
- delete copy[key];
44
- }
45
- }
46
- return [...result, copy];
32
+ function split(object, ...list) {
33
+ const _list = typeof list[0] === "string" ? [list] : list;
34
+ const copy = shallowObjectCopy(object);
35
+ const result = [];
36
+ for (let i = 0; i < _list.length; i++) {
37
+ const keys = _list[i];
38
+ result.push({});
39
+ for (const key of keys) {
40
+ result[i][key] = copy[key];
41
+ delete copy[key];
42
+ }
43
+ }
44
+ return [...result, copy];
47
45
  }
48
- export function merge(...objects) {
49
- const result = {};
50
- for (const obj of objects) {
51
- Object.assign(result, obj);
52
- }
53
- return result;
46
+ function merge(...objects) {
47
+ const result = {};
48
+ for (const obj of objects) Object.assign(result, obj);
49
+ return result;
54
50
  }
51
+ //#endregion
52
+ export { get, merge, omit, pick, split };
@@ -1,7 +1,9 @@
1
- import { type ItemsOf } from "../index.js";
2
- export type Predicate<T> = (item: T, index: number, array: readonly T[]) => boolean;
3
- export type MappingFn<T, V> = (item: T, index: number, array: readonly T[]) => V;
4
- export type FlattenArray<T> = T extends any[] ? FlattenArray<ItemsOf<T>> : T;
5
- export type ModifyValue<O, K extends keyof O, V> = Omit<O, K> & {
6
- [key in K]: V;
7
- };
1
+ /* @ts-self-types="./types.d.ts" */
2
+ import { ItemsOf } from "../types.js";
3
+ //#region src/immutable/types.d.ts
4
+ type Predicate<T> = (item: T, index: number, array: readonly T[]) => boolean;
5
+ type MappingFn<T, V> = (item: T, index: number, array: readonly T[]) => V;
6
+ type FlattenArray<T> = T extends any[] ? FlattenArray<ItemsOf<T>> : T;
7
+ type ModifyValue<O, K extends keyof O, V> = Omit<O, K> & { [key in K]: V };
8
+ //#endregion
9
+ export { FlattenArray, MappingFn, ModifyValue, Predicate };
@@ -1 +1 @@
1
- import {} from "../index.js";
1
+ /* @ts-self-types="./types.d.ts" */
@@ -1,30 +1,35 @@
1
- import { type ModifyValue } from "./types.js";
2
- export type UpdateSetter<O, K extends keyof O, V> = V | ((prev: O[K]) => V);
3
- export type Update = {
4
- <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], K3 extends keyof O[K0][K1][K2], K4 extends keyof O[K0][K1][K2][K3], V>(object: O, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, setter: UpdateSetter<O[K0][K1][K2][K3], K4, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, ModifyValue<O[K0][K1][K2], K3, ModifyValue<O[K0][K1][K2][K3], K4, V>>>>>;
5
- <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], K3 extends keyof O[K0][K1][K2], V>(object: O, k0: K0, k1: K1, k2: K2, k3: K3, setter: UpdateSetter<O[K0][K1][K2], K3, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, ModifyValue<O[K0][K1][K2], K3, V>>>>;
6
- <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], V>(object: O, k0: K0, k1: K1, k2: K2, setter: UpdateSetter<O[K0][K1], K2, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, V>>>;
7
- <O extends object, K0 extends keyof O, K1 extends keyof O[K0], V>(object: O, k0: K0, k1: K1, setter: UpdateSetter<O[K0], K1, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, V>>;
8
- <O extends object, K extends keyof O, V>(object: O, key: K, setter: UpdateSetter<O, K, V>): ModifyValue<O, K, V>;
1
+ /* @ts-self-types="./update.d.ts" */
2
+ import { ModifyValue } from "./types.js";
3
+
4
+ //#region src/immutable/update.d.ts
5
+ type UpdateSetter<O, K extends keyof O, V> = V | ((prev: O[K]) => V);
6
+ type Update = {
7
+ <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], K3 extends keyof O[K0][K1][K2], K4 extends keyof O[K0][K1][K2][K3], V>(object: O, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, setter: UpdateSetter<O[K0][K1][K2][K3], K4, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, ModifyValue<O[K0][K1][K2], K3, ModifyValue<O[K0][K1][K2][K3], K4, V>>>>>;
8
+ <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], K3 extends keyof O[K0][K1][K2], V>(object: O, k0: K0, k1: K1, k2: K2, k3: K3, setter: UpdateSetter<O[K0][K1][K2], K3, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, ModifyValue<O[K0][K1][K2], K3, V>>>>;
9
+ <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], V>(object: O, k0: K0, k1: K1, k2: K2, setter: UpdateSetter<O[K0][K1], K2, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, V>>>;
10
+ <O extends object, K0 extends keyof O, K1 extends keyof O[K0], V>(object: O, k0: K0, k1: K1, setter: UpdateSetter<O[K0], K1, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, V>>;
11
+ <O extends object, K extends keyof O, V>(object: O, key: K, setter: UpdateSetter<O, K, V>): ModifyValue<O, K, V>;
9
12
  };
10
13
  /**
11
- * Change single value in an object by key. Allows accessign nested objects by passing multiple keys.
12
- *
13
- * Performs a shallow copy of each accessed object.
14
- *
15
- * @param object original source
16
- * @param ...keys keys of sequential accessed objects
17
- * @param value a value to set in place of a previous one, or a setter function.
18
- * ```ts
19
- * V | ((prev: O[K]) => V)
20
- * ```
21
- * a new value doesn't have to have the same type as the original
22
- * @returns changed copy of the original object
23
- *
24
- * @example
25
- * const original = { foo: { bar: { baz: 123 }}};
26
- * const newObj = update(original, "foo", "bar", "baz", prev => prev + 1)
27
- * original // { foo: { bar: { baz: 123 }}}
28
- * newObj // { foo: { bar: { baz: 124 }}}
29
- */
30
- export declare const update: Update;
14
+ * Change single value in an object by key. Allows accessign nested objects by passing multiple keys.
15
+ *
16
+ * Performs a shallow copy of each accessed object.
17
+ *
18
+ * @param object original source
19
+ * @param ...keys keys of sequential accessed objects
20
+ * @param value a value to set in place of a previous one, or a setter function.
21
+ * ```ts
22
+ * V | ((prev: O[K]) => V)
23
+ * ```
24
+ * a new value doesn't have to have the same type as the original
25
+ * @returns changed copy of the original object
26
+ *
27
+ * @example
28
+ * const original = { foo: { bar: { baz: 123 }}};
29
+ * const newObj = update(original, "foo", "bar", "baz", prev => prev + 1)
30
+ * original // { foo: { bar: { baz: 123 }}}
31
+ * newObj // { foo: { bar: { baz: 124 }}}
32
+ */
33
+ declare const update: Update;
34
+ //#endregion
35
+ export { Update, UpdateSetter, update };
@@ -1,30 +1,30 @@
1
+ /* @ts-self-types="./update.d.ts" */
1
2
  import { withCopy } from "./copy.js";
2
- import {} from "./types.js";
3
+ //#region src/immutable/update.ts
3
4
  /**
4
- * Change single value in an object by key. Allows accessign nested objects by passing multiple keys.
5
- *
6
- * Performs a shallow copy of each accessed object.
7
- *
8
- * @param object original source
9
- * @param ...keys keys of sequential accessed objects
10
- * @param value a value to set in place of a previous one, or a setter function.
11
- * ```ts
12
- * V | ((prev: O[K]) => V)
13
- * ```
14
- * a new value doesn't have to have the same type as the original
15
- * @returns changed copy of the original object
16
- *
17
- * @example
18
- * const original = { foo: { bar: { baz: 123 }}};
19
- * const newObj = update(original, "foo", "bar", "baz", prev => prev + 1)
20
- * original // { foo: { bar: { baz: 123 }}}
21
- * newObj // { foo: { bar: { baz: 124 }}}
22
- */
23
- export const update = (...args) => withCopy(args[0], obj => {
24
- if (args.length > 3)
25
- obj[args[1]] = update(obj[args[1]], ...args.slice(2));
26
- else if (typeof args[2] === "function")
27
- obj[args[1]] = args[2](obj[args[1]]);
28
- else
29
- obj[args[1]] = args[2];
5
+ * Change single value in an object by key. Allows accessign nested objects by passing multiple keys.
6
+ *
7
+ * Performs a shallow copy of each accessed object.
8
+ *
9
+ * @param object original source
10
+ * @param ...keys keys of sequential accessed objects
11
+ * @param value a value to set in place of a previous one, or a setter function.
12
+ * ```ts
13
+ * V | ((prev: O[K]) => V)
14
+ * ```
15
+ * a new value doesn't have to have the same type as the original
16
+ * @returns changed copy of the original object
17
+ *
18
+ * @example
19
+ * const original = { foo: { bar: { baz: 123 }}};
20
+ * const newObj = update(original, "foo", "bar", "baz", prev => prev + 1)
21
+ * original // { foo: { bar: { baz: 123 }}}
22
+ * newObj // { foo: { bar: { baz: 124 }}}
23
+ */
24
+ const update = (...args) => withCopy(args[0], (obj) => {
25
+ if (args.length > 3) obj[args[1]] = update(obj[args[1]], ...args.slice(2));
26
+ else if (typeof args[2] === "function") obj[args[1]] = args[2](obj[args[1]]);
27
+ else obj[args[1]] = args[2];
30
28
  });
29
+ //#endregion
30
+ export { update };