maraj 2.5.1 → 2.6.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.d.mts +72 -31
- package/dist/index.d.ts +72 -31
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,24 +1,47 @@
|
|
|
1
|
+
/** Returns only the optional keys of an object. */
|
|
2
|
+
type OptionalKeys<T> = {
|
|
3
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
4
|
+
}[keyof T];
|
|
5
|
+
/** Removes properties or indexes, order doesn't matter. */
|
|
6
|
+
type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : OptionalKeys<T> | (OptionalKeys<T>)[]) => T;
|
|
7
|
+
/**
|
|
8
|
+
* Removes properties or indexes, order doesn't matter.
|
|
9
|
+
* @param value Object/array from where to remove keys/indexes shallowly.
|
|
10
|
+
* @param keys Keys to remove.
|
|
11
|
+
* @returns New object/array without the provided keys/indexes.
|
|
12
|
+
*/
|
|
13
|
+
declare const remove: Remove;
|
|
14
|
+
|
|
1
15
|
/** Index literal type to improve DX at managing updates. */
|
|
2
16
|
declare const LITERAL_INDEX = "${number}";
|
|
3
17
|
type LiteralIndex = typeof LITERAL_INDEX;
|
|
4
|
-
type
|
|
18
|
+
type ObjectOrArray = Record<string | number | symbol, any>;
|
|
19
|
+
type OptionValues = 'error' | 'skip' | 'add';
|
|
20
|
+
type Options = {
|
|
21
|
+
onNewProps?: OptionValues;
|
|
22
|
+
onNewIndexes?: OptionValues;
|
|
23
|
+
};
|
|
24
|
+
type ImmutableUpdate = <TObject extends ObjectOrArray>(state: TObject, dotPathUpdateObject: DotPathUpdateObject<TObject>, options?: Options) => TObject;
|
|
5
25
|
/**
|
|
6
|
-
* Creates a new object applying
|
|
26
|
+
* Creates a new object applying immutable updates using dot-path syntax.
|
|
27
|
+
* Can add or skip new props or indexes depending on options.
|
|
28
|
+
* TYPESCRIPT: Dot-Path Update Object type must STRICTLY match the type of state.
|
|
7
29
|
* @param state Original State Object.
|
|
8
|
-
* @param
|
|
9
|
-
* @param
|
|
30
|
+
* @param dotPathUpdateObject Dot Path Update Object.
|
|
31
|
+
* @param options Options to change JS behaviour on non-existent fields.
|
|
10
32
|
* @returns New Updated Object.
|
|
11
33
|
*/
|
|
12
|
-
declare const
|
|
13
|
-
type
|
|
34
|
+
declare const update: ImmutableUpdate;
|
|
35
|
+
type ExtendableImmutableUpdate = <TObject extends ObjectOrArray, TUpdateObject extends DotPathUpdateObject<TObject>>(state: TObject, dotPathUpdateObject: TUpdateObject) => ExtendedUpdate<TObject, TUpdateObject>;
|
|
14
36
|
/**
|
|
15
|
-
* Creates a new object applying immutable
|
|
37
|
+
* Creates a new object applying immutable updates using dot-path syntax.
|
|
38
|
+
* It will always add non-existent props or indexes if provided.
|
|
39
|
+
* TYPESCRIPT: Dot-Path Update Object type extends from the type of state and you can add new non-existent properties.
|
|
16
40
|
* @param state Original State Object.
|
|
17
|
-
* @param
|
|
18
|
-
* @param addNonExistentPropsAndIndexes If it should or not create non-existent fields.
|
|
41
|
+
* @param dotPathUpdateObject Dot Path Update Object.
|
|
19
42
|
* @returns New Updated Object.
|
|
20
43
|
*/
|
|
21
|
-
declare const
|
|
44
|
+
declare const extendableUpdate: ExtendableImmutableUpdate;
|
|
22
45
|
|
|
23
46
|
/**
|
|
24
47
|
* --------------------------------------------
|
|
@@ -37,7 +60,7 @@ type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false
|
|
|
37
60
|
* --------------------------------------------
|
|
38
61
|
*/
|
|
39
62
|
/** 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>}`;
|
|
63
|
+
type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? number | `${ArrayIndexes}` : number | `${ArrayIndexes}` | `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}`;
|
|
41
64
|
/** Dot-paths of an object. */
|
|
42
65
|
type ObjectPaths<T, TAllowedTypes> = {
|
|
43
66
|
[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>}`;
|
|
@@ -54,7 +77,7 @@ type DotPaths<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? IsTup
|
|
|
54
77
|
/** Nested type implementation. */
|
|
55
78
|
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
79
|
/** Value of a nested property of a given dot-path. */
|
|
57
|
-
type
|
|
80
|
+
type ExactValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedType<TObject, TPath>;
|
|
58
81
|
/**
|
|
59
82
|
* --------------------------------------------
|
|
60
83
|
* RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
@@ -70,33 +93,51 @@ type ReturnedValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = Ret
|
|
|
70
93
|
* --------------------------------------------
|
|
71
94
|
*/
|
|
72
95
|
/** Value of a dot-path update object, can be the value or a function. */
|
|
73
|
-
type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> =
|
|
96
|
+
type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = ExactValueInDotPath<TObject, TPath> | ((fieldValue: ExactValueInDotPath<TObject, TPath>) => ExactValueInDotPath<TObject, TPath>);
|
|
74
97
|
/** Dot-path update object, used to give updates to the updateImmutably function. */
|
|
75
98
|
type DotPathUpdateObject<TObject> = {
|
|
76
|
-
[TPath in DotPaths<TObject>]?:
|
|
99
|
+
[TPath in DotPaths<TObject>]?: ExactValueInDotPath<TObject, TPath> | ((fieldValue: ExactValueInDotPath<TObject, TPath>) => ExactValueInDotPath<TObject, TPath>);
|
|
77
100
|
};
|
|
78
101
|
/**
|
|
79
102
|
* --------------------------------------------
|
|
80
|
-
*
|
|
103
|
+
* EXTRACT TYPE FROM DOT-PATH UPDATE OBJECT
|
|
81
104
|
* --------------------------------------------
|
|
82
105
|
*/
|
|
83
|
-
/**
|
|
84
|
-
|
|
85
|
-
|
|
106
|
+
/** Merging two objects */
|
|
107
|
+
/** Keys which arent dot-path keys */
|
|
108
|
+
type NonDotPathKeys<T> = {
|
|
109
|
+
[Key in keyof T]: Key extends `${infer K}.${string}` ? K : Key;
|
|
86
110
|
}[keyof T];
|
|
87
|
-
/**
|
|
88
|
-
type
|
|
89
|
-
[
|
|
111
|
+
/** Keys which are an index */
|
|
112
|
+
type KeysRepresentingAnIndex<T> = {
|
|
113
|
+
[Key in keyof T]: Key extends `${number}.${string}` ? never : Key;
|
|
90
114
|
}[keyof T];
|
|
91
|
-
|
|
92
|
-
type
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
115
|
+
/** Keys which start with an index */
|
|
116
|
+
type KeyPathsStartingWithAnIndex<T> = {
|
|
117
|
+
[Key in keyof T]: Key extends `${number}.${string}` ? Key : never;
|
|
118
|
+
}[keyof T];
|
|
119
|
+
/** Keys which are paths and its first key-path is removed */
|
|
120
|
+
type NestedPathKeys<T, K extends string | number> = {
|
|
121
|
+
[Key in keyof T]: Key extends `${K}.${infer U}` ? U : never;
|
|
122
|
+
}[keyof T];
|
|
123
|
+
/** Extract value if its a function */
|
|
124
|
+
type Value<T> = T extends (...args: any) => any ? ReturnType<T> : T;
|
|
125
|
+
/** To check if an empty object is provided */
|
|
126
|
+
type EmptyObject = Record<string | number | symbol, never>;
|
|
127
|
+
/** Used to extract the type from a dot-path update object. */
|
|
128
|
+
type ExtractTypeFromDotPathUpdateObject<T extends Record<string, any>> = T extends EmptyObject ? {} : keyof T extends LiteralIndex | number | `${number}` | `${number}.${infer U}` ? Value<T[KeyPathsStartingWithAnIndex<T>]> extends never ? Array<Value<T[KeysRepresentingAnIndex<T>]>> : Array<Value<T[KeysRepresentingAnIndex<T>]> | {
|
|
129
|
+
[Key2 in U]: Value<T[KeyPathsStartingWithAnIndex<T>]>;
|
|
130
|
+
}> : {
|
|
131
|
+
[Key1 in NonDotPathKeys<T> & string]: Key1 extends keyof T ? Value<T[Key1]> extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<{
|
|
132
|
+
[Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
|
|
133
|
+
}> extends Array<infer W> ? Array<U | W> : Array<U> & ExtractTypeFromDotPathUpdateObject<{
|
|
134
|
+
[Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
|
|
135
|
+
}> : Value<T[Key1]> : ExtractTypeFromDotPathUpdateObject<{
|
|
136
|
+
[Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
|
|
137
|
+
}>;
|
|
138
|
+
} & unknown;
|
|
139
|
+
/** New expanded value after aplying updates including new properties */
|
|
140
|
+
type ExtendedUpdate<TObject extends Record<string | number | symbol, any>, TDotPathUpdateObject extends Record<string | number | symbol, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : ExtractTypeFromDotPathUpdateObject<TObject & TDotPathUpdateObject>;
|
|
100
141
|
|
|
101
142
|
type Select = <TObject, TPath extends DotPaths<TObject> | ''>(state: TObject, path: TPath) => ReturnedValueInDotPath<TObject, TPath>;
|
|
102
143
|
/**
|
|
@@ -131,4 +172,4 @@ declare const isObjectLiteral: (valueToTest: any) => boolean;
|
|
|
131
172
|
declare const parseStringIndex: (v: string) => number | null;
|
|
132
173
|
declare const isStringIndex: (v: string) => boolean;
|
|
133
174
|
|
|
134
|
-
export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type
|
|
175
|
+
export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type ExactValueInDotPath, type LiteralIndex, type ReturnedValueInDotPath, extendableUpdate, isObjectLiteral, isStringIndex, parseStringIndex, remove, select, set, splitPathAtLastKey, update };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,47 @@
|
|
|
1
|
+
/** Returns only the optional keys of an object. */
|
|
2
|
+
type OptionalKeys<T> = {
|
|
3
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
4
|
+
}[keyof T];
|
|
5
|
+
/** Removes properties or indexes, order doesn't matter. */
|
|
6
|
+
type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : OptionalKeys<T> | (OptionalKeys<T>)[]) => T;
|
|
7
|
+
/**
|
|
8
|
+
* Removes properties or indexes, order doesn't matter.
|
|
9
|
+
* @param value Object/array from where to remove keys/indexes shallowly.
|
|
10
|
+
* @param keys Keys to remove.
|
|
11
|
+
* @returns New object/array without the provided keys/indexes.
|
|
12
|
+
*/
|
|
13
|
+
declare const remove: Remove;
|
|
14
|
+
|
|
1
15
|
/** Index literal type to improve DX at managing updates. */
|
|
2
16
|
declare const LITERAL_INDEX = "${number}";
|
|
3
17
|
type LiteralIndex = typeof LITERAL_INDEX;
|
|
4
|
-
type
|
|
18
|
+
type ObjectOrArray = Record<string | number | symbol, any>;
|
|
19
|
+
type OptionValues = 'error' | 'skip' | 'add';
|
|
20
|
+
type Options = {
|
|
21
|
+
onNewProps?: OptionValues;
|
|
22
|
+
onNewIndexes?: OptionValues;
|
|
23
|
+
};
|
|
24
|
+
type ImmutableUpdate = <TObject extends ObjectOrArray>(state: TObject, dotPathUpdateObject: DotPathUpdateObject<TObject>, options?: Options) => TObject;
|
|
5
25
|
/**
|
|
6
|
-
* Creates a new object applying
|
|
26
|
+
* Creates a new object applying immutable updates using dot-path syntax.
|
|
27
|
+
* Can add or skip new props or indexes depending on options.
|
|
28
|
+
* TYPESCRIPT: Dot-Path Update Object type must STRICTLY match the type of state.
|
|
7
29
|
* @param state Original State Object.
|
|
8
|
-
* @param
|
|
9
|
-
* @param
|
|
30
|
+
* @param dotPathUpdateObject Dot Path Update Object.
|
|
31
|
+
* @param options Options to change JS behaviour on non-existent fields.
|
|
10
32
|
* @returns New Updated Object.
|
|
11
33
|
*/
|
|
12
|
-
declare const
|
|
13
|
-
type
|
|
34
|
+
declare const update: ImmutableUpdate;
|
|
35
|
+
type ExtendableImmutableUpdate = <TObject extends ObjectOrArray, TUpdateObject extends DotPathUpdateObject<TObject>>(state: TObject, dotPathUpdateObject: TUpdateObject) => ExtendedUpdate<TObject, TUpdateObject>;
|
|
14
36
|
/**
|
|
15
|
-
* Creates a new object applying immutable
|
|
37
|
+
* Creates a new object applying immutable updates using dot-path syntax.
|
|
38
|
+
* It will always add non-existent props or indexes if provided.
|
|
39
|
+
* TYPESCRIPT: Dot-Path Update Object type extends from the type of state and you can add new non-existent properties.
|
|
16
40
|
* @param state Original State Object.
|
|
17
|
-
* @param
|
|
18
|
-
* @param addNonExistentPropsAndIndexes If it should or not create non-existent fields.
|
|
41
|
+
* @param dotPathUpdateObject Dot Path Update Object.
|
|
19
42
|
* @returns New Updated Object.
|
|
20
43
|
*/
|
|
21
|
-
declare const
|
|
44
|
+
declare const extendableUpdate: ExtendableImmutableUpdate;
|
|
22
45
|
|
|
23
46
|
/**
|
|
24
47
|
* --------------------------------------------
|
|
@@ -37,7 +60,7 @@ type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false
|
|
|
37
60
|
* --------------------------------------------
|
|
38
61
|
*/
|
|
39
62
|
/** 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>}`;
|
|
63
|
+
type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? number | `${ArrayIndexes}` : number | `${ArrayIndexes}` | `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}`;
|
|
41
64
|
/** Dot-paths of an object. */
|
|
42
65
|
type ObjectPaths<T, TAllowedTypes> = {
|
|
43
66
|
[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>}`;
|
|
@@ -54,7 +77,7 @@ type DotPaths<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? IsTup
|
|
|
54
77
|
/** Nested type implementation. */
|
|
55
78
|
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
79
|
/** Value of a nested property of a given dot-path. */
|
|
57
|
-
type
|
|
80
|
+
type ExactValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedType<TObject, TPath>;
|
|
58
81
|
/**
|
|
59
82
|
* --------------------------------------------
|
|
60
83
|
* RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
@@ -70,33 +93,51 @@ type ReturnedValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = Ret
|
|
|
70
93
|
* --------------------------------------------
|
|
71
94
|
*/
|
|
72
95
|
/** Value of a dot-path update object, can be the value or a function. */
|
|
73
|
-
type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> =
|
|
96
|
+
type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = ExactValueInDotPath<TObject, TPath> | ((fieldValue: ExactValueInDotPath<TObject, TPath>) => ExactValueInDotPath<TObject, TPath>);
|
|
74
97
|
/** Dot-path update object, used to give updates to the updateImmutably function. */
|
|
75
98
|
type DotPathUpdateObject<TObject> = {
|
|
76
|
-
[TPath in DotPaths<TObject>]?:
|
|
99
|
+
[TPath in DotPaths<TObject>]?: ExactValueInDotPath<TObject, TPath> | ((fieldValue: ExactValueInDotPath<TObject, TPath>) => ExactValueInDotPath<TObject, TPath>);
|
|
77
100
|
};
|
|
78
101
|
/**
|
|
79
102
|
* --------------------------------------------
|
|
80
|
-
*
|
|
103
|
+
* EXTRACT TYPE FROM DOT-PATH UPDATE OBJECT
|
|
81
104
|
* --------------------------------------------
|
|
82
105
|
*/
|
|
83
|
-
/**
|
|
84
|
-
|
|
85
|
-
|
|
106
|
+
/** Merging two objects */
|
|
107
|
+
/** Keys which arent dot-path keys */
|
|
108
|
+
type NonDotPathKeys<T> = {
|
|
109
|
+
[Key in keyof T]: Key extends `${infer K}.${string}` ? K : Key;
|
|
86
110
|
}[keyof T];
|
|
87
|
-
/**
|
|
88
|
-
type
|
|
89
|
-
[
|
|
111
|
+
/** Keys which are an index */
|
|
112
|
+
type KeysRepresentingAnIndex<T> = {
|
|
113
|
+
[Key in keyof T]: Key extends `${number}.${string}` ? never : Key;
|
|
90
114
|
}[keyof T];
|
|
91
|
-
|
|
92
|
-
type
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
115
|
+
/** Keys which start with an index */
|
|
116
|
+
type KeyPathsStartingWithAnIndex<T> = {
|
|
117
|
+
[Key in keyof T]: Key extends `${number}.${string}` ? Key : never;
|
|
118
|
+
}[keyof T];
|
|
119
|
+
/** Keys which are paths and its first key-path is removed */
|
|
120
|
+
type NestedPathKeys<T, K extends string | number> = {
|
|
121
|
+
[Key in keyof T]: Key extends `${K}.${infer U}` ? U : never;
|
|
122
|
+
}[keyof T];
|
|
123
|
+
/** Extract value if its a function */
|
|
124
|
+
type Value<T> = T extends (...args: any) => any ? ReturnType<T> : T;
|
|
125
|
+
/** To check if an empty object is provided */
|
|
126
|
+
type EmptyObject = Record<string | number | symbol, never>;
|
|
127
|
+
/** Used to extract the type from a dot-path update object. */
|
|
128
|
+
type ExtractTypeFromDotPathUpdateObject<T extends Record<string, any>> = T extends EmptyObject ? {} : keyof T extends LiteralIndex | number | `${number}` | `${number}.${infer U}` ? Value<T[KeyPathsStartingWithAnIndex<T>]> extends never ? Array<Value<T[KeysRepresentingAnIndex<T>]>> : Array<Value<T[KeysRepresentingAnIndex<T>]> | {
|
|
129
|
+
[Key2 in U]: Value<T[KeyPathsStartingWithAnIndex<T>]>;
|
|
130
|
+
}> : {
|
|
131
|
+
[Key1 in NonDotPathKeys<T> & string]: Key1 extends keyof T ? Value<T[Key1]> extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<{
|
|
132
|
+
[Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
|
|
133
|
+
}> extends Array<infer W> ? Array<U | W> : Array<U> & ExtractTypeFromDotPathUpdateObject<{
|
|
134
|
+
[Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
|
|
135
|
+
}> : Value<T[Key1]> : ExtractTypeFromDotPathUpdateObject<{
|
|
136
|
+
[Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
|
|
137
|
+
}>;
|
|
138
|
+
} & unknown;
|
|
139
|
+
/** New expanded value after aplying updates including new properties */
|
|
140
|
+
type ExtendedUpdate<TObject extends Record<string | number | symbol, any>, TDotPathUpdateObject extends Record<string | number | symbol, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : ExtractTypeFromDotPathUpdateObject<TObject & TDotPathUpdateObject>;
|
|
100
141
|
|
|
101
142
|
type Select = <TObject, TPath extends DotPaths<TObject> | ''>(state: TObject, path: TPath) => ReturnedValueInDotPath<TObject, TPath>;
|
|
102
143
|
/**
|
|
@@ -131,4 +172,4 @@ declare const isObjectLiteral: (valueToTest: any) => boolean;
|
|
|
131
172
|
declare const parseStringIndex: (v: string) => number | null;
|
|
132
173
|
declare const isStringIndex: (v: string) => boolean;
|
|
133
174
|
|
|
134
|
-
export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type
|
|
175
|
+
export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type ExactValueInDotPath, type LiteralIndex, type ReturnedValueInDotPath, extendableUpdate, isObjectLiteral, isStringIndex, parseStringIndex, remove, select, set, splitPathAtLastKey, update };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var m=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var v=(e,t)=>{for(var r in t)m(e,r,{get:t[r],enumerable:!0})},C=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of R(t))!k.call(e,a)&&a!==r&&m(e,a,{get:()=>t[a],enumerable:!(n=$(t,a))||n.enumerable});return e};var W=e=>C(m({},"__esModule",{value:!0}),e);var q={};v(q,{extendableUpdate:()=>L,isObjectLiteral:()=>d,isStringIndex:()=>y,parseStringIndex:()=>K,remove:()=>j,select:()=>P,set:()=>w,splitPathAtLastKey:()=>A,update:()=>D});module.exports=W(q);var j=(e,t)=>{if(!Array.isArray(e)&&e?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(t)?t:[t];if(Array.isArray(e)){let a=Symbol(),l=[...e];return r.forEach(s=>{l[s]=a}),l.filter(s=>s!==a)}let n={...e};return r.forEach(a=>delete n[a]),n};var P=(e,t)=>!t||typeof t!="string"?e:t.split(".").reduce((r,n)=>{if(r?.hasOwnProperty(n))return r[n]},e);var A=e=>{if(typeof e!="string")throw new Error(`Wrong dotPath type: ${e}`);let t=e.lastIndexOf(".")+1,r=t?e.slice(0,t-1):"",n=e.slice(t);return[r,n]};var w=e=>()=>e;var d=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype;var K=e=>{let t=parseInt(e);return t.toString()===e?t:null},y=e=>parseInt(e).toString()===e;var U=e=>JSON.stringify(e),O=(e,t)=>e.slice(0,t).join("."),g=e=>Array.isArray(e),E=(e,t,r={})=>{if(r={onNewProps:"add",onNewIndexes:"error",...r},!d(e)&&!Array.isArray(e))throw new Error(`"${U(e)}" is not an literalObject/array`);if(!d(t))throw new Error('wrong "updates" type provided');let n=Array.isArray(e)?[...e]:{...e},a={};for(let[l,f]of Object.entries(t)){if(!l)continue;let s=l.split("."),p=s.pop(),u=a,i=n;for(let c=0;c<s.length;c++){let o=s[c],x=i[o],S=d(x),h=g(x),T=x===void 0;if(!S&&!h&&!T)throw new Error(`Property "${o}" in "${O(s,c)}" is a primitive`);if(!u[o]){if(T){let V=g(i)&&y(o),N=y(s[c+1]??p),I=V?r.onNewIndexes:r.onNewProps;if(I==="error")throw new Error(`${o} doesn't exist in "${O(s,c)}"`);if(I==="skip")continue;i[o]=N?[]:{}}else i[o]=h?[...x]:{...x};u[o]={}}i=i[o],u=u[o]}let b=i;if(!b.hasOwnProperty(p)){let o=Array.isArray(b)&&y(p)?r.onNewIndexes:r.onNewProps;if(o==="error")throw new Error(`Property/Index "${p}" doesnt exist in the path "${O([...s,p])}". Its value is: "${U(b)}"`);if(o==="skip")continue}b[p]=typeof f=="function"?f(b[p]):f,delete u[p]}return n},D=(e,t,r)=>E(e,t,r),L=(e,t)=>E(e,t);0&&(module.exports={extendableUpdate,isObjectLiteral,isStringIndex,parseStringIndex,remove,select,set,splitPathAtLastKey,update});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var U=(e,t)=>{if(!Array.isArray(e)&&e?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(t)?t:[t];if(Array.isArray(e)){let c=Symbol(),d=[...e];return r.forEach(o=>{d[o]=c}),d.filter(o=>o!==c)}let a={...e};return r.forEach(c=>delete a[c]),a};var g=(e,t)=>!t||typeof t!="string"?e:t.split(".").reduce((r,a)=>{if(r?.hasOwnProperty(a))return r[a]},e);var E=e=>{if(typeof e!="string")throw new Error(`Wrong dotPath type: ${e}`);let t=e.lastIndexOf(".")+1,r=t?e.slice(0,t-1):"",a=e.slice(t);return[r,a]};var D=e=>()=>e;var b=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype;var L=e=>{let t=parseInt(e);return t.toString()===e?t:null},x=e=>parseInt(e).toString()===e;var I=e=>JSON.stringify(e),m=(e,t)=>e.slice(0,t).join("."),j=e=>Array.isArray(e),P=(e,t,r={})=>{if(r={onNewProps:"add",onNewIndexes:"error",...r},!b(e)&&!Array.isArray(e))throw new Error(`"${I(e)}" is not an literalObject/array`);if(!b(t))throw new Error('wrong "updates" type provided');let a=Array.isArray(e)?[...e]:{...e},c={};for(let[d,f]of Object.entries(t)){if(!d)continue;let o=d.split("."),s=o.pop(),y=c,p=a;for(let i=0;i<o.length;i++){let n=o[i],u=p[n],A=b(u),O=j(u),h=u===void 0;if(!A&&!O&&!h)throw new Error(`Property "${n}" in "${m(o,i)}" is a primitive`);if(!y[n]){if(h){let w=j(p)&&x(n),K=x(o[i+1]??s),T=w?r.onNewIndexes:r.onNewProps;if(T==="error")throw new Error(`${n} doesn't exist in "${m(o,i)}"`);if(T==="skip")continue;p[n]=K?[]:{}}else p[n]=O?[...u]:{...u};y[n]={}}p=p[n],y=y[n]}let l=p;if(!l.hasOwnProperty(s)){let n=Array.isArray(l)&&x(s)?r.onNewIndexes:r.onNewProps;if(n==="error")throw new Error(`Property/Index "${s}" doesnt exist in the path "${m([...o,s])}". Its value is: "${I(l)}"`);if(n==="skip")continue}l[s]=typeof f=="function"?f(l[s]):f,delete y[s]}return a},S=(e,t,r)=>P(e,t,r),V=(e,t)=>P(e,t);export{V as extendableUpdate,b as isObjectLiteral,x as isStringIndex,L as parseStringIndex,U as remove,g as select,D as set,E as splitPathAtLastKey,S as update};
|