maraj 2.4.0 → 2.5.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/README.md +3 -2
- package/dist/index.d.mts +68 -32
- package/dist/index.d.ts +68 -32
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -28,16 +28,17 @@ originalValue.files[1] === updatedValue.files[1] // true
|
|
|
28
28
|
|
|
29
29
|
### Limitations
|
|
30
30
|
|
|
31
|
-
- If "addNonExistentPropsAndIndexes" is enabled, numbers will always create an array if the array/object doesnt exist beforehand.
|
|
31
|
+
- If "addNonExistentPropsAndIndexes" is enabled (3rd parameter in the updateImmutably function), **numbers** will **always** create an **array** if the array/object doesnt exist beforehand.
|
|
32
32
|
|
|
33
33
|
```ts
|
|
34
34
|
const updatedValue = updateImmutably({}, {
|
|
35
35
|
'files.1.name': 'new name',
|
|
36
36
|
}, true)
|
|
37
37
|
// updatedValue = { files: [<empty>, { name: 'new name' }] }
|
|
38
|
+
// the "files" prop is an array.
|
|
38
39
|
```
|
|
39
40
|
|
|
40
|
-
- The type "
|
|
41
|
+
- The type "UpdateDotPathObject" (the type used as second parameter in the updateImmutably function) if used to create new update objects using computed properties { [key]: value } will always throw a ts error even if the typing is correct. [Playground simple explanation](https://www.typescriptlang.org/play?ts=5.3.2#code/LAKALgngDgpgBAGQJYGcxwLxwOQENtwA+cABgCQDeAdgK4C2ARjAE4C+JooksiqYASjADGAe2YATTHApwA2gGk4SKrzQBdAPwAuOItacQoqmjhDmMXGBiDREqQB4AKnBgAPK1XEpVYAHwAKAGsYCB1HAEpMX2lQODijE3MhHWQ0GzFJLApYuNyAejy4AD0wbxZmMR0UZSF4ADcAFgA6AGYmgFYldAB3JAAbPrgwAAsK7rhcFXKxCfQhYeFA5QBzUxE6KBorSWCIb0nJOtw+mhhvGmqqVasNvst4PqQrZmOyupgVJAAzIYWh6BWSm8tiSYByuTkuzUOl24P0IFy5jANGYKiSoFYQA)
|
|
41
42
|
|
|
42
43
|
```ts
|
|
43
44
|
const updateFieldStore = <TPath extends DotPaths<Store>>(
|
package/dist/index.d.mts
CHANGED
|
@@ -1,49 +1,77 @@
|
|
|
1
|
+
/** Index literal type to improve DX at managing updates. */
|
|
2
|
+
declare const LITERAL_INDEX = "${number}";
|
|
3
|
+
type LiteralIndex = typeof LITERAL_INDEX;
|
|
4
|
+
type UpdateImmutably = <T extends object>(state: T, updates: DotPathUpdateObject<T>) => T;
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new object applying strict immutable updates using dot-path syntax. Can't add new props or indexes.
|
|
7
|
+
* @param state Original State Object.
|
|
8
|
+
* @param updates Dot Path Update Object.
|
|
9
|
+
* @param addNonExistentPropsAndIndexes If it should or not create non-existent fields.
|
|
10
|
+
* @returns New Updated Object.
|
|
11
|
+
*/
|
|
12
|
+
declare const updateImmutably: UpdateImmutably;
|
|
13
|
+
type ModifyImmutably = <T extends object>(state: T, updates: DotPathUpdateObject<T> | Record<string, any>) => T;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new object applying immutable modifications using dot-path syntax. It will add non-existent props or indexes if provided.
|
|
16
|
+
* @param state Original State Object.
|
|
17
|
+
* @param updates Dot Path Update Object.
|
|
18
|
+
* @param addNonExistentPropsAndIndexes If it should or not create non-existent fields.
|
|
19
|
+
* @returns New Updated Object.
|
|
20
|
+
*/
|
|
21
|
+
declare const modifyImmutably: ModifyImmutably;
|
|
22
|
+
|
|
1
23
|
/**
|
|
2
24
|
* --------------------------------------------
|
|
3
25
|
* UTILITIES
|
|
4
26
|
* --------------------------------------------
|
|
5
27
|
*/
|
|
28
|
+
/** Primitives and browser native objects. */
|
|
6
29
|
type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
|
|
7
|
-
|
|
8
|
-
type ArrayIndexes =
|
|
30
|
+
/** Array indexes type. */
|
|
31
|
+
type ArrayIndexes = number | LiteralIndex;
|
|
32
|
+
/** To check if a ReadonlyArray is a tuple or not. */
|
|
9
33
|
type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false : true;
|
|
10
34
|
/**
|
|
11
35
|
* --------------------------------------------
|
|
12
36
|
* DOT PATH IMPLEMENTATION
|
|
13
37
|
* --------------------------------------------
|
|
14
38
|
*/
|
|
15
|
-
|
|
39
|
+
/** Dot-paths of an array. */
|
|
40
|
+
type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? `${ArrayIndexes}` : `${ArrayIndexes}` | `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}`;
|
|
41
|
+
/** Dot-paths of an object. */
|
|
16
42
|
type ObjectPaths<T, TAllowedTypes> = {
|
|
17
|
-
[Key in keyof T & string]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${
|
|
43
|
+
[Key in keyof T & string]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPaths<T[Key], TAllowedTypes>}` : `${Key}.${DotPaths<T[Key], TAllowedTypes>}`;
|
|
18
44
|
}[keyof T & string];
|
|
45
|
+
/** Dot-paths of a tuple. */
|
|
19
46
|
type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes>;
|
|
20
|
-
|
|
21
|
-
type DotPaths<T, TAllowedTypes = any> =
|
|
22
|
-
/**
|
|
23
|
-
* --------------------------------------------
|
|
24
|
-
* TO KNOW IF A PATH IS INDEXING ARRAY OR NOT
|
|
25
|
-
* --------------------------------------------
|
|
26
|
-
*/
|
|
47
|
+
/** Literal union of all posible dot-paths which satisfy provided types as second argument (default: any). */
|
|
48
|
+
type DotPaths<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? IsTuple<T> extends true ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T, TAllowedTypes>;
|
|
27
49
|
/**
|
|
28
50
|
* --------------------------------------------
|
|
29
51
|
* VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
30
52
|
* --------------------------------------------
|
|
31
53
|
*/
|
|
54
|
+
/** Nested type implementation. */
|
|
32
55
|
type NestedType<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? NestedType<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? NestedType<U, R> : never : never : never;
|
|
56
|
+
/** Value of a nested property of a given dot-path. */
|
|
33
57
|
type ValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedType<TObject, TPath>;
|
|
34
58
|
/**
|
|
35
59
|
* --------------------------------------------
|
|
36
60
|
* RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
37
61
|
* --------------------------------------------
|
|
38
62
|
*/
|
|
63
|
+
/** Returned nested type implementation. */
|
|
39
64
|
type ReturnedNestedValue<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ReturnedNestedValue<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (ReturnedNestedValue<U, R> | undefined) : never : never : never;
|
|
65
|
+
/** Value of a RETURNED nested property of a given dot-path (so when accessing arrays can be undefined). */
|
|
40
66
|
type ReturnedValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = ReturnedNestedValue<TObject, TPath>;
|
|
41
67
|
/**
|
|
42
68
|
* --------------------------------------------
|
|
43
69
|
* UPDATE OBJECT WITH DOT PATH IMPLEMENTATION
|
|
44
70
|
* --------------------------------------------
|
|
45
71
|
*/
|
|
72
|
+
/** Value of a dot-path update object, can be the value or a function. */
|
|
46
73
|
type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
|
|
74
|
+
/** Dot-path update object, used to give updates to the updateImmutably function. */
|
|
47
75
|
type DotPathUpdateObject<TObject> = {
|
|
48
76
|
[TPath in DotPaths<TObject>]?: ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
|
|
49
77
|
};
|
|
@@ -52,24 +80,30 @@ type DotPathUpdateObject<TObject> = {
|
|
|
52
80
|
* REQUIRED OR OPTIONAL KEYS
|
|
53
81
|
* --------------------------------------------
|
|
54
82
|
*/
|
|
55
|
-
|
|
56
|
-
type
|
|
57
|
-
[K in keyof T
|
|
58
|
-
}[keyof T
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
83
|
+
/** Returns only the optional keys of an object. */
|
|
84
|
+
type OptionalKeys<T> = {
|
|
85
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
86
|
+
}[keyof T];
|
|
87
|
+
/** Returns only the required keys of an object. */
|
|
88
|
+
type RequiredKeys<T> = {
|
|
89
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
|
|
90
|
+
}[keyof T];
|
|
62
91
|
|
|
63
|
-
type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) :
|
|
92
|
+
type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : OptionalKeys<T> | (OptionalKeys<T>)[]) => T;
|
|
64
93
|
/**
|
|
65
94
|
* Removes properties or indexes, order doesn't matter.
|
|
95
|
+
* @param value Object/array from where to remove keys/indexes shallowly.
|
|
96
|
+
* @param keys Keys to remove.
|
|
97
|
+
* @returns New object/array without the provided keys/indexes.
|
|
66
98
|
*/
|
|
67
99
|
declare const remove: Remove;
|
|
68
100
|
|
|
69
101
|
type Select = <TObject, TPath extends DotPaths<TObject> | ''>(state: TObject, path: TPath) => ReturnedValueInDotPath<TObject, TPath>;
|
|
70
102
|
/**
|
|
71
|
-
* Returns the value from a given dot path of an object/array.
|
|
72
|
-
*
|
|
103
|
+
* Returns the value from a given dot path of an object/array or undefined if doesn't exist.
|
|
104
|
+
* @param state State from which retrieve the value.
|
|
105
|
+
* @param path Path indicating the value to extract.
|
|
106
|
+
* @returns Value of the provided path or undefined.
|
|
73
107
|
*/
|
|
74
108
|
declare const select: Select;
|
|
75
109
|
|
|
@@ -77,22 +111,24 @@ type PathWithoutLastKey = string;
|
|
|
77
111
|
type LastKey = string;
|
|
78
112
|
type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastKey, LastKey];
|
|
79
113
|
/**
|
|
80
|
-
* Splits string
|
|
81
|
-
|
|
114
|
+
* Splits a dot path string into its last key and its remaining keys ('' if none).
|
|
115
|
+
* @param dotPath A dot path.
|
|
116
|
+
* @returns [pathWithoutLastKey, lastKey] or ['', lastKey].
|
|
117
|
+
*/
|
|
82
118
|
declare const splitPathAtLastKey: SplitDotPathAtLastKey;
|
|
83
119
|
|
|
84
120
|
type Set = <T>(newValue: T) => (value: T) => T;
|
|
85
121
|
/**
|
|
86
|
-
* Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays
|
|
122
|
+
* Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays.
|
|
123
|
+
* @param newValue New value.
|
|
124
|
+
* @returns The same value.
|
|
87
125
|
*/
|
|
88
126
|
declare const set: Set;
|
|
89
127
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
*/
|
|
96
|
-
declare const updateImmutably: UpdateImmutably;
|
|
128
|
+
/** Checks if any provided value is an object literal or not */
|
|
129
|
+
declare const isObjectLiteral: (valueToTest: any) => boolean;
|
|
130
|
+
|
|
131
|
+
declare const parseStringIndex: (v: string) => number | null;
|
|
132
|
+
declare const isStringIndex: (v: string) => boolean;
|
|
97
133
|
|
|
98
|
-
export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type
|
|
134
|
+
export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type OptionalKeys, type PrimitivesAndNativeObjects, type RequiredKeys, type ReturnedValueInDotPath, type ValueInDotPath, isObjectLiteral, isStringIndex, modifyImmutably, parseStringIndex, remove, select, set, splitPathAtLastKey, updateImmutably };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,49 +1,77 @@
|
|
|
1
|
+
/** Index literal type to improve DX at managing updates. */
|
|
2
|
+
declare const LITERAL_INDEX = "${number}";
|
|
3
|
+
type LiteralIndex = typeof LITERAL_INDEX;
|
|
4
|
+
type UpdateImmutably = <T extends object>(state: T, updates: DotPathUpdateObject<T>) => T;
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new object applying strict immutable updates using dot-path syntax. Can't add new props or indexes.
|
|
7
|
+
* @param state Original State Object.
|
|
8
|
+
* @param updates Dot Path Update Object.
|
|
9
|
+
* @param addNonExistentPropsAndIndexes If it should or not create non-existent fields.
|
|
10
|
+
* @returns New Updated Object.
|
|
11
|
+
*/
|
|
12
|
+
declare const updateImmutably: UpdateImmutably;
|
|
13
|
+
type ModifyImmutably = <T extends object>(state: T, updates: DotPathUpdateObject<T> | Record<string, any>) => T;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new object applying immutable modifications using dot-path syntax. It will add non-existent props or indexes if provided.
|
|
16
|
+
* @param state Original State Object.
|
|
17
|
+
* @param updates Dot Path Update Object.
|
|
18
|
+
* @param addNonExistentPropsAndIndexes If it should or not create non-existent fields.
|
|
19
|
+
* @returns New Updated Object.
|
|
20
|
+
*/
|
|
21
|
+
declare const modifyImmutably: ModifyImmutably;
|
|
22
|
+
|
|
1
23
|
/**
|
|
2
24
|
* --------------------------------------------
|
|
3
25
|
* UTILITIES
|
|
4
26
|
* --------------------------------------------
|
|
5
27
|
*/
|
|
28
|
+
/** Primitives and browser native objects. */
|
|
6
29
|
type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
|
|
7
|
-
|
|
8
|
-
type ArrayIndexes =
|
|
30
|
+
/** Array indexes type. */
|
|
31
|
+
type ArrayIndexes = number | LiteralIndex;
|
|
32
|
+
/** To check if a ReadonlyArray is a tuple or not. */
|
|
9
33
|
type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false : true;
|
|
10
34
|
/**
|
|
11
35
|
* --------------------------------------------
|
|
12
36
|
* DOT PATH IMPLEMENTATION
|
|
13
37
|
* --------------------------------------------
|
|
14
38
|
*/
|
|
15
|
-
|
|
39
|
+
/** Dot-paths of an array. */
|
|
40
|
+
type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? `${ArrayIndexes}` : `${ArrayIndexes}` | `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}`;
|
|
41
|
+
/** Dot-paths of an object. */
|
|
16
42
|
type ObjectPaths<T, TAllowedTypes> = {
|
|
17
|
-
[Key in keyof T & string]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${
|
|
43
|
+
[Key in keyof T & string]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPaths<T[Key], TAllowedTypes>}` : `${Key}.${DotPaths<T[Key], TAllowedTypes>}`;
|
|
18
44
|
}[keyof T & string];
|
|
45
|
+
/** Dot-paths of a tuple. */
|
|
19
46
|
type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes>;
|
|
20
|
-
|
|
21
|
-
type DotPaths<T, TAllowedTypes = any> =
|
|
22
|
-
/**
|
|
23
|
-
* --------------------------------------------
|
|
24
|
-
* TO KNOW IF A PATH IS INDEXING ARRAY OR NOT
|
|
25
|
-
* --------------------------------------------
|
|
26
|
-
*/
|
|
47
|
+
/** Literal union of all posible dot-paths which satisfy provided types as second argument (default: any). */
|
|
48
|
+
type DotPaths<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? IsTuple<T> extends true ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T, TAllowedTypes>;
|
|
27
49
|
/**
|
|
28
50
|
* --------------------------------------------
|
|
29
51
|
* VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
30
52
|
* --------------------------------------------
|
|
31
53
|
*/
|
|
54
|
+
/** Nested type implementation. */
|
|
32
55
|
type NestedType<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? NestedType<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? NestedType<U, R> : never : never : never;
|
|
56
|
+
/** Value of a nested property of a given dot-path. */
|
|
33
57
|
type ValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedType<TObject, TPath>;
|
|
34
58
|
/**
|
|
35
59
|
* --------------------------------------------
|
|
36
60
|
* RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
37
61
|
* --------------------------------------------
|
|
38
62
|
*/
|
|
63
|
+
/** Returned nested type implementation. */
|
|
39
64
|
type ReturnedNestedValue<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ReturnedNestedValue<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (ReturnedNestedValue<U, R> | undefined) : never : never : never;
|
|
65
|
+
/** Value of a RETURNED nested property of a given dot-path (so when accessing arrays can be undefined). */
|
|
40
66
|
type ReturnedValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = ReturnedNestedValue<TObject, TPath>;
|
|
41
67
|
/**
|
|
42
68
|
* --------------------------------------------
|
|
43
69
|
* UPDATE OBJECT WITH DOT PATH IMPLEMENTATION
|
|
44
70
|
* --------------------------------------------
|
|
45
71
|
*/
|
|
72
|
+
/** Value of a dot-path update object, can be the value or a function. */
|
|
46
73
|
type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
|
|
74
|
+
/** Dot-path update object, used to give updates to the updateImmutably function. */
|
|
47
75
|
type DotPathUpdateObject<TObject> = {
|
|
48
76
|
[TPath in DotPaths<TObject>]?: ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
|
|
49
77
|
};
|
|
@@ -52,24 +80,30 @@ type DotPathUpdateObject<TObject> = {
|
|
|
52
80
|
* REQUIRED OR OPTIONAL KEYS
|
|
53
81
|
* --------------------------------------------
|
|
54
82
|
*/
|
|
55
|
-
|
|
56
|
-
type
|
|
57
|
-
[K in keyof T
|
|
58
|
-
}[keyof T
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
83
|
+
/** Returns only the optional keys of an object. */
|
|
84
|
+
type OptionalKeys<T> = {
|
|
85
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
86
|
+
}[keyof T];
|
|
87
|
+
/** Returns only the required keys of an object. */
|
|
88
|
+
type RequiredKeys<T> = {
|
|
89
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
|
|
90
|
+
}[keyof T];
|
|
62
91
|
|
|
63
|
-
type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) :
|
|
92
|
+
type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : OptionalKeys<T> | (OptionalKeys<T>)[]) => T;
|
|
64
93
|
/**
|
|
65
94
|
* Removes properties or indexes, order doesn't matter.
|
|
95
|
+
* @param value Object/array from where to remove keys/indexes shallowly.
|
|
96
|
+
* @param keys Keys to remove.
|
|
97
|
+
* @returns New object/array without the provided keys/indexes.
|
|
66
98
|
*/
|
|
67
99
|
declare const remove: Remove;
|
|
68
100
|
|
|
69
101
|
type Select = <TObject, TPath extends DotPaths<TObject> | ''>(state: TObject, path: TPath) => ReturnedValueInDotPath<TObject, TPath>;
|
|
70
102
|
/**
|
|
71
|
-
* Returns the value from a given dot path of an object/array.
|
|
72
|
-
*
|
|
103
|
+
* Returns the value from a given dot path of an object/array or undefined if doesn't exist.
|
|
104
|
+
* @param state State from which retrieve the value.
|
|
105
|
+
* @param path Path indicating the value to extract.
|
|
106
|
+
* @returns Value of the provided path or undefined.
|
|
73
107
|
*/
|
|
74
108
|
declare const select: Select;
|
|
75
109
|
|
|
@@ -77,22 +111,24 @@ type PathWithoutLastKey = string;
|
|
|
77
111
|
type LastKey = string;
|
|
78
112
|
type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastKey, LastKey];
|
|
79
113
|
/**
|
|
80
|
-
* Splits string
|
|
81
|
-
|
|
114
|
+
* Splits a dot path string into its last key and its remaining keys ('' if none).
|
|
115
|
+
* @param dotPath A dot path.
|
|
116
|
+
* @returns [pathWithoutLastKey, lastKey] or ['', lastKey].
|
|
117
|
+
*/
|
|
82
118
|
declare const splitPathAtLastKey: SplitDotPathAtLastKey;
|
|
83
119
|
|
|
84
120
|
type Set = <T>(newValue: T) => (value: T) => T;
|
|
85
121
|
/**
|
|
86
|
-
* Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays
|
|
122
|
+
* Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays.
|
|
123
|
+
* @param newValue New value.
|
|
124
|
+
* @returns The same value.
|
|
87
125
|
*/
|
|
88
126
|
declare const set: Set;
|
|
89
127
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
*/
|
|
96
|
-
declare const updateImmutably: UpdateImmutably;
|
|
128
|
+
/** Checks if any provided value is an object literal or not */
|
|
129
|
+
declare const isObjectLiteral: (valueToTest: any) => boolean;
|
|
130
|
+
|
|
131
|
+
declare const parseStringIndex: (v: string) => number | null;
|
|
132
|
+
declare const isStringIndex: (v: string) => boolean;
|
|
97
133
|
|
|
98
|
-
export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type
|
|
134
|
+
export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type OptionalKeys, type PrimitivesAndNativeObjects, type RequiredKeys, type ReturnedValueInDotPath, type ValueInDotPath, isObjectLiteral, isStringIndex, modifyImmutably, parseStringIndex, remove, select, set, splitPathAtLastKey, updateImmutably };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var b=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var R=(t,e)=>{for(var r in e)b(t,r,{get:e[r],enumerable:!0})},$=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of w(e))!E.call(t,n)&&n!==r&&b(t,n,{get:()=>e[n],enumerable:!(o=S(e,n))||o.enumerable});return t};var U=t=>$(b({},"__esModule",{value:!0}),t);var V={};R(V,{isObjectLiteral:()=>d,isStringIndex:()=>h,modifyImmutably:()=>D,parseStringIndex:()=>g,remove:()=>I,select:()=>T,set:()=>O,splitPathAtLastKey:()=>P,updateImmutably:()=>K});module.exports=U(V);var I=(t,e)=>{if(!Array.isArray(t)&&t?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(e)?e:[e];if(Array.isArray(t)){let n=Symbol(),p=[...t];return r.forEach(a=>{p[a]=n}),p.filter(a=>a!==n)}let o={...t};return r.forEach(n=>delete o[n]),o};var T=(t,e)=>!e||typeof e!="string"?t:e.split(".").reduce((r,o)=>{if(r?.hasOwnProperty(o))return r[o]},t);var P=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,r=e?t.slice(0,e-1):"",o=t.slice(e);return[r,o]};var O=t=>()=>t;var d=t=>!!t&&Object.getPrototypeOf(t)===Object.prototype;var g=t=>{let e=parseInt(t);return e.toString()===t?e:null},h=t=>parseInt(t).toString()===t;var A="${number}",j=(t,e,r)=>{if(!d(t)&&!Array.isArray(t))throw new Error(`"${t}" is not an literalObject/array`);if(!e)return t;let o=Array.isArray(t)?[...t]:{...t},n={};for(let[p,m]of Object.entries(e)){if(!p)continue;let a=p.split(".");if(a.includes(A))throw new Error(`"${A}" inside "${p}"`);let i=a.pop(),l=n,y=o;for(let u=0;u<a.length;u++){let s=a[u],c=y[s],L=d(c)||Array.isArray(c),x=r&&c===void 0;if(!L&&!x)throw new Error(`"${s}" doesnt exist in "${a.slice(0,u+1).join(".")}"`);l[s]||(x?y[s]=h(a[u+1]??i)?[]:{}:y[s]=Array.isArray(c)?[...c]:{...c},l[s]={}),y=y[s],l=l[s]}let f=y;if(!r&&!f.hasOwnProperty(i))throw new Error(`Property/Index "${i}" doesnt exist in the path "${a.join(".")+"."+i}". Its value is: "${f}"`);f[i]=typeof m=="function"?m(f[i],o):m,delete l[i]}return o},K=(t,e)=>j(t,e,!1),D=(t,e)=>j(t,e,!0);0&&(module.exports={isObjectLiteral,isStringIndex,modifyImmutably,parseStringIndex,remove,select,set,splitPathAtLastKey,updateImmutably});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var P=(t,e)=>{if(!Array.isArray(t)&&t?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(e)?e:[e];if(Array.isArray(t)){let i=Symbol(),p=[...t];return r.forEach(n=>{p[n]=i}),p.filter(n=>n!==i)}let o={...t};return r.forEach(i=>delete o[i]),o};var O=(t,e)=>!e||typeof e!="string"?t:e.split(".").reduce((r,o)=>{if(r?.hasOwnProperty(o))return r[o]},t);var g=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,r=e?t.slice(0,e-1):"",o=t.slice(e);return[r,o]};var A=t=>()=>t;var f=t=>!!t&&Object.getPrototypeOf(t)===Object.prototype;var j=t=>{let e=parseInt(t);return e.toString()===t?e:null},h=t=>parseInt(t).toString()===t;var x="${number}",I=(t,e,r)=>{if(!f(t)&&!Array.isArray(t))throw new Error(`"${t}" is not an literalObject/array`);if(!e)return t;let o=Array.isArray(t)?[...t]:{...t},i={};for(let[p,d]of Object.entries(e)){if(!p)continue;let n=p.split(".");if(n.includes(x))throw new Error(`"${x}" inside "${p}"`);let s=n.pop(),l=i,y=o;for(let u=0;u<n.length;u++){let a=n[u],c=y[a],T=f(c)||Array.isArray(c),b=r&&c===void 0;if(!T&&!b)throw new Error(`"${a}" doesnt exist in "${n.slice(0,u+1).join(".")}"`);l[a]||(b?y[a]=h(n[u+1]??s)?[]:{}:y[a]=Array.isArray(c)?[...c]:{...c},l[a]={}),y=y[a],l=l[a]}let m=y;if(!r&&!m.hasOwnProperty(s))throw new Error(`Property/Index "${s}" doesnt exist in the path "${n.join(".")+"."+s}". Its value is: "${m}"`);m[s]=typeof d=="function"?d(m[s],o):d,delete l[s]}return o},K=(t,e)=>I(t,e,!1),D=(t,e)=>I(t,e,!0);export{f as isObjectLiteral,h as isStringIndex,D as modifyImmutably,j as parseStringIndex,P as remove,O as select,A as set,g as splitPathAtLastKey,K as updateImmutably};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maraj",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "A lightweight 500 bytes gzipped alternative for immutable updates while dealing with objects and arrays",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"shallow",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"test": "vitest run",
|
|
24
24
|
"build": "tsup src/index.ts --minify --format cjs,esm --dts",
|
|
25
25
|
"lint": "tsc",
|
|
26
|
+
"trace": "tsc --generateTrace trace",
|
|
26
27
|
"ci": "npm run lint && npm run test && npm run build",
|
|
27
28
|
"release": "npm run lint && npm run test && npm run build && changeset publish"
|
|
28
29
|
},
|
|
@@ -37,6 +38,7 @@
|
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"@changesets/cli": "^2.27.1",
|
|
39
40
|
"@microsoft/api-extractor": "^7.38.5",
|
|
41
|
+
"@typescript/analyze-trace": "^0.10.1",
|
|
40
42
|
"tsup": "^8.0.1",
|
|
41
43
|
"typescript": "^5.3.3",
|
|
42
44
|
"vitest": "^0.28.4"
|