maraj 2.10.0 → 2.11.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 CHANGED
@@ -6,13 +6,11 @@
6
6
  /** Dot-paths of an array. */
7
7
  type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? ExtendedArrayIndexes : ExtendedArrayIndexes | `${ExtendedArrayIndexes}.${DotPathsImplementation<TInnerType, TAllowedTypes>}` : `${ExtendedArrayIndexes}.${DotPathsImplementation<TInnerType, TAllowedTypes>}`;
8
8
  /** Dot-paths of an object. */
9
- type ObjectPaths<T, TAllowedTypes> = {
10
- [Key in keyof T & (string | number)]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}` : `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}`;
11
- }[keyof T & (string | number)] & {};
9
+ type ObjectPaths<T, TAllowedTypes> = { [Key in keyof T & (string | number)]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}` : `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}`; }[keyof T & (string | number)];
12
10
  /** Dot-paths of a tuple. */
13
- type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes> & {};
14
- /** Dot-paths implementation */
15
- type DotPathsImplementation<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? T extends Tuple ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T, TAllowedTypes>;
11
+ type TuplePaths<T, TAllowedTypes> = ObjectPaths<Required<Omit<T, keyof any[]>>, TAllowedTypes>;
12
+ /** Dot-paths implementation, Required<T> so optional props count as their type ('nickname?: string' is a string) */
13
+ type DotPathsImplementation<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? T extends Tuple ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<Required<T>, TAllowedTypes>;
16
14
  /** Literal union of all posible dot-paths which satisfy provided types as second argument (default: any). */
17
15
  type DotPaths<T, TAllowedTypes = any> = KeyOf<T, TAllowedTypes> | DotPathsImplementation<T, TAllowedTypes>;
18
16
  /**
@@ -20,15 +18,17 @@ type DotPaths<T, TAllowedTypes = any> = KeyOf<T, TAllowedTypes> | DotPathsImplem
20
18
  * VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
21
19
  * --------------------------------------------
22
20
  */
23
- /** Nested type implementation. */
24
- type ValueInPath<TObject, TPath> = TPath extends keyof TObject ? TObject[TPath] : TPath extends '' ? TObject : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ValueInPath<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? ValueInPath<U, R> : never : never : never;
21
+ /** Tuple without rest items ([number, string], not [string, ...number[]]): an index that isn't a key is out of range. */
22
+ type IsFixedTuple<T> = T extends Tuple ? number extends T['length'] ? false : true : false;
23
+ /** Nested type implementation, goes into each object of a union ({ name } | null -> 'name' is string). */
24
+ type ValueInPath<TObject, TPath> = TObject extends unknown ? TPath extends keyof TObject ? TObject[TPath] : TPath extends '' ? TObject : TPath extends LiteralIndex ? never : TPath extends `${number}` ? IsFixedTuple<TObject> extends true ? never : TObject extends ReadonlyArray<infer U> ? U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ValueInPath<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? IsFixedTuple<TObject> extends true ? never : TObject extends ReadonlyArray<infer U> ? ValueInPath<U, R> : never : never : never : never;
25
25
  /**
26
26
  * --------------------------------------------
27
27
  * RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
28
28
  * --------------------------------------------
29
29
  */
30
- /** Returned nested type implementation. */
31
- type ReturnedValueInPath<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TPath extends number ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : TObject[TPath] : TObject[TPath] : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : never : TPath extends LiteralIndex ? never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ReturnedValueInPath<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (ReturnedValueInPath<U, R> | undefined) : never : never : never;
30
+ /** Returned nested type implementation, null or undefined inside the path -> undefined. */
31
+ type ReturnedValueInPath<TObject, TPath> = TPath extends '' ? TObject : TObject extends null | undefined ? undefined : TPath extends keyof TObject ? TPath extends number ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : TObject[TPath] : TObject[TPath] : TPath extends `${number}` ? IsFixedTuple<TObject> extends true ? never : TObject extends ReadonlyArray<infer U> ? (U | undefined) : never : TPath extends LiteralIndex ? never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ReturnedValueInPath<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? IsFixedTuple<TObject> extends true ? never : TObject extends ReadonlyArray<infer U> ? (ReturnedValueInPath<U, R> | undefined) : never : never : never;
32
32
  /**
33
33
  * --------------------------------------------
34
34
  * UPDATE OBJECT WITH DOT PATH IMPLEMENTATION
@@ -37,9 +37,7 @@ type ReturnedValueInPath<TObject, TPath> = TPath extends '' ? TObject : TPath ex
37
37
  /** Value of a dot-path update object, can be the value or a function. */
38
38
  type UpdateValue<TObject, TPath> = ValueInPath<TObject, TPath> extends never ? never : ValueInPath<TObject, TPath> | ((fieldValue: ValueInPath<TObject, TPath>) => Strict<ValueInPath<TObject, TPath>>);
39
39
  /** Dot-path update object, used to give updates to the updateImmutably function. */
40
- type UpdateObject<TObject, TPaths extends DotPaths<TObject> = DotPaths<TObject>> = {
41
- [Path in TPaths]?: UpdateValue<TObject, Path>;
42
- };
40
+ type UpdateObject<TObject, TPaths extends DotPaths<TObject> = DotPaths<TObject>> = { [Path in TPaths]?: UpdateValue<TObject, Path>; };
43
41
  /**
44
42
  * --------------------------------------------
45
43
  * EXTRACT TYPE FROM DOT-PATH UPDATE OBJECT
@@ -47,78 +45,54 @@ type UpdateObject<TObject, TPaths extends DotPaths<TObject> = DotPaths<TObject>>
47
45
  */
48
46
  /** Merging two objects */
49
47
  /** Keys which arent dot-path keys */
50
- type NonDotPathKeys<T> = {
51
- [Key in keyof T]: Key extends `${infer K}.${string}` ? K : Key;
52
- }[keyof T];
48
+ type NonDotPathKeys<T> = { [Key in keyof T]: Key extends `${infer K}.${string}` ? K : Key; }[keyof T];
53
49
  /** Keys which are an index */
54
- type KeysRepresentingAnIndex<T> = {
55
- [Key in keyof T]: Key extends `${number}.${string}` ? never : Key;
56
- }[keyof T];
50
+ type KeysRepresentingAnIndex<T> = { [Key in keyof T]: Key extends `${number}.${string}` ? never : Key; }[keyof T];
57
51
  /** Keys which start with an index */
58
- type KeyPathsStartingWithAnIndex<T> = {
59
- [Key in keyof T]: Key extends `${number}.${string}` ? Key : never;
60
- }[keyof T];
52
+ type KeyPathsStartingWithAnIndex<T> = { [Key in keyof T]: Key extends `${number}.${string}` ? Key : never; }[keyof T];
61
53
  /** Keys which are paths and its first key-path is removed */
62
- type NestedPathKeys<T, K extends string | number> = {
63
- [Key in keyof T]: Key extends `${K}.${infer U}` ? U : never;
64
- }[keyof T];
54
+ type NestedPathKeys<T, K extends string | number> = { [Key in keyof T]: Key extends `${K}.${infer U}` ? U : never; }[keyof T];
65
55
  /** Extract value if its a function */
66
- type Value<T> = T extends (...args: any) => any ? ReturnType<T> : T;
56
+ type Value<T> = T extends ((...args: any) => any) ? ReturnType<T> : T;
67
57
  /** To check if an empty object is provided */
68
58
  type EmptyObject = Record<PropertyKey, never>;
69
59
  /** Used to extract the type from a dot-path update object. */
70
- 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>]> | {
71
- [Key2 in U]: Value<T[KeyPathsStartingWithAnIndex<T>]>;
72
- }> : {
73
- [Key1 in NonDotPathKeys<T> & string]: Key1 extends keyof T ? Value<T[Key1]> extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<{
74
- [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
75
- }> extends Array<infer W> ? Array<U | W> : Array<U> & ExtractTypeFromDotPathUpdateObject<{
76
- [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
77
- }> : Value<T[Key1]> : ExtractTypeFromDotPathUpdateObject<{
78
- [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
79
- }>;
80
- } & unknown;
60
+ 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>]> | { [Key2 in U]: Value<T[KeyPathsStartingWithAnIndex<T>]>; }> : { [Key1 in NonDotPathKeys<T> & string]: Key1 extends keyof T ? Value<T[Key1]> extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<{ [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>; }> extends Array<infer W> ? Array<U | W> : Array<U> & ExtractTypeFromDotPathUpdateObject<{ [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>; }> : Value<T[Key1]> : ExtractTypeFromDotPathUpdateObject<{ [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>; }>; } & unknown;
81
61
  /** New expanded value after aplying updates including new properties */
82
62
  type ExtendedUpdate<TObject, TDotPathUpdateObject extends Record<PropertyKey, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject>;
83
-
84
63
  /** Index literal type to improve DX at managing updates. */
85
64
  declare const LITERAL_INDEX = "${number}";
86
-
87
65
  /** Primitives and browser native objects. */
88
- type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
66
+ type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File | Blob;
89
67
  /** Tuple type, it has to have at least property "0" */
90
68
  type Tuple = ReadonlyArray<any> & {
91
- "0": any;
69
+ "0": any;
92
70
  };
93
71
  /** Array indexes type. */
94
72
  type LiteralIndex = typeof LITERAL_INDEX;
95
73
  type ExtendedArrayIndexes = number | `${number}` | LiteralIndex;
96
- /** Object Literal */
74
+ /** Object Literal, File and Blob still match it (they have no symbol to reject them by) */
97
75
  type ObjectLiteral = {
98
- [x: string]: any;
99
- [x: number]: any;
100
- [x: symbol]: any;
101
- [Symbol.iterator]?: never;
102
- [Symbol.toPrimitive]?: never;
103
- [Symbol.hasInstance]?: never;
104
- [Symbol.unscopables]?: never;
105
- [Symbol.matchAll]?: never;
76
+ [x: string]: any;
77
+ [x: number]: any;
78
+ [x: symbol]: any;
79
+ [Symbol.iterator]?: never;
80
+ [Symbol.toPrimitive]?: never;
81
+ [Symbol.hasInstance]?: never;
82
+ [Symbol.unscopables]?: never;
83
+ [Symbol.matchAll]?: never;
84
+ [Symbol.toStringTag]?: never;
106
85
  };
107
86
  /** Strict key */
108
87
  declare const strictSymbol: unique symbol;
109
- type Strict<T> = T extends ObjectLiteral ? T & {
110
- [strictSymbol]: DotPaths<T>;
88
+ type Strict<T> = T extends PrimitivesAndNativeObjects ? T : T extends ObjectLiteral ? T & {
89
+ [strictSymbol]: DotPaths<T>;
111
90
  } : T;
112
91
  /** Smart KeyOf (doesnt include the "strict" key) */
113
- type KeyOf<T, TAllowedValuesInKeys = any> = T extends ObjectLiteral ? T extends Strict<infer U> ? KeysOfObject<U, TAllowedValuesInKeys> : KeysOfObject<T, TAllowedValuesInKeys> : T extends Tuple ? KeysOfObject<Omit<T, keyof any[]>, TAllowedValuesInKeys> : T extends ReadonlyArray<infer U> ? U extends TAllowedValuesInKeys ? ExtendedArrayIndexes : never : never;
114
- type KeysOfObject<T, TAllowedValuesInKeys> = {
115
- [Key in keyof T]: T[Key] extends TAllowedValuesInKeys ? Key : never;
116
- }[keyof T];
117
-
92
+ type KeyOf<T, TAllowedValuesInKeys = any> = T extends PrimitivesAndNativeObjects ? never : T extends ObjectLiteral ? T extends Strict<infer U> ? KeysOfObject<U, TAllowedValuesInKeys> : KeysOfObject<T, TAllowedValuesInKeys> : T extends Tuple ? KeysOfObject<Omit<T, keyof any[]>, TAllowedValuesInKeys> : T extends ReadonlyArray<infer U> ? U extends TAllowedValuesInKeys ? ExtendedArrayIndexes : never : never;
93
+ type KeysOfObject<T, TAllowedValuesInKeys> = { [Key in keyof T]-?: Required<T>[Key] extends TAllowedValuesInKeys ? Key : never; }[keyof T];
118
94
  /** Returns only the optional keys of an object. */
119
- type OptionalKeys<T> = {
120
- [K in keyof T]-?: {} extends Pick<T, K> ? K : never;
121
- }[keyof T];
95
+ type OptionalKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? K : never; }[keyof T];
122
96
  /** Removes properties or indexes, order doesn't matter. */
123
97
  type Remove = <T extends ObjectLiteral | any[]>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : OptionalKeys<T> | (OptionalKeys<T>)[]) => T;
124
98
  /**
@@ -127,11 +101,10 @@ type Remove = <T extends ObjectLiteral | any[]>(value: T, keys: T extends any[]
127
101
  * @param keys Keys to remove.
128
102
  * @returns New object/array without the provided keys/indexes.
129
103
  */
130
- declare const remove: Remove;
131
-
104
+ export declare const remove: Remove;
132
105
  type Select = {
133
- <T, TPath extends DotPaths<T>>(state: T, path: TPath): ReturnedValueInPath<T, TPath>;
134
- <T, TPath extends ''>(state: T, path: TPath): T;
106
+ <T, TPath extends DotPaths<T>>(state: T, path: TPath): ReturnedValueInPath<T, TPath>;
107
+ <T, TPath extends ''>(state: T, path: TPath): T;
135
108
  };
136
109
  /**
137
110
  * Returns the value from a given dot path of an object/array or undefined if doesn't exist.
@@ -139,8 +112,7 @@ type Select = {
139
112
  * @param path Path indicating the value to extract.
140
113
  * @returns Value of the provided path or undefined.
141
114
  */
142
- declare const select: Select;
143
-
115
+ export declare const select: Select;
144
116
  type PathWithoutLastKey = string;
145
117
  type LastKey = string;
146
118
  type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastKey, LastKey];
@@ -149,21 +121,19 @@ type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastK
149
121
  * @param dotPath A dot path.
150
122
  * @returns [pathWithoutLastKey, lastKey] or ['', lastKey].
151
123
  */
152
- declare const splitPathAtLastKey: SplitDotPathAtLastKey;
153
-
124
+ export declare const splitPathAtLastKey: SplitDotPathAtLastKey;
154
125
  type Set = <T>(newValue: T) => (v: unknown) => T;
155
126
  /**
156
127
  * Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays.
157
128
  * @param newValue New value.
158
129
  * @returns The same value.
159
130
  */
160
- declare const set: Set;
161
-
131
+ export declare const set: Set;
162
132
  type OptionValues = 'error' | 'skip' | 'add';
163
133
  type Options = {
164
- onNewProps?: OptionValues;
165
- onNewIndexes?: OptionValues;
166
- onPathsIntersectedByPrimitiveValues?: OptionValues;
134
+ onNewProps?: OptionValues;
135
+ onNewIndexes?: OptionValues;
136
+ onPathsIntersectedByPrimitiveValues?: OptionValues;
167
137
  };
168
138
  type ImmutableUpdate = <TObject, TAllowedDotPathKeys extends DotPaths<TObject> = DotPaths<TObject>>(state: TObject, dotPathUpdateObject: UpdateObject<TObject, TAllowedDotPathKeys>, options?: Options) => TObject;
169
139
  /**
@@ -175,7 +145,7 @@ type ImmutableUpdate = <TObject, TAllowedDotPathKeys extends DotPaths<TObject> =
175
145
  * @param options Options to change JS behaviour on non-existent fields.
176
146
  * @returns New Updated Object.
177
147
  */
178
- declare const update: ImmutableUpdate;
148
+ export declare const update: ImmutableUpdate;
179
149
  type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TObject> = UpdateObject<TObject>>(state: TObject, dotPathUpdateObject: TUpdateObject) => ExtendedUpdate<TObject, TUpdateObject>;
180
150
  /**
181
151
  * Creates a new object applying immutable updates using dot-path syntax.
@@ -185,26 +155,21 @@ type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TO
185
155
  * @param dotPathUpdateObject Dot Path Update Object.
186
156
  * @returns New Updated Object.
187
157
  */
188
- declare const experimental_extendableUpdate: ExtendableImmutableUpdate;
189
-
158
+ export declare const experimental_extendableUpdate: ExtendableImmutableUpdate;
190
159
  type ForEachEntry = <T extends ObjectLiteral>(object: T, callbackfn: (key: keyof T, value: T[keyof T]) => void) => void;
191
- declare const forEachEntry: ForEachEntry;
192
-
160
+ export declare const forEachEntry: ForEachEntry;
193
161
  /** Checks if any provided value is an object literal or not */
194
162
  type IsObjectLiteral = (valueToTest: unknown) => valueToTest is ObjectLiteral;
195
- declare const isObjectLiteral: IsObjectLiteral;
196
-
163
+ export declare const isObjectLiteral: IsObjectLiteral;
197
164
  /** Adds strict property to an object literal */
198
165
  type StrictFn = <T>(value: T) => Strict<T>;
199
- declare const strict: StrictFn;
200
- declare const s: StrictFn;
166
+ export declare const strict: StrictFn;
167
+ export declare const s: StrictFn;
201
168
  /** Checks the strictness of a type passed to a function or callback without 3rd party implementation */
202
- type StrictValueWrapper = <T extends U, U>(v: T extends U ? DotPaths<T> extends DotPaths<U> ? T : Exclude<DotPaths<T>, DotPaths<U>> : never) => U;
203
- declare const strictValueWrapper: StrictValueWrapper;
204
-
169
+ type StrictType = <T extends U, U>(v: T extends U ? DotPaths<T> extends DotPaths<U> ? T : Exclude<DotPaths<T>, DotPaths<U>> : never) => U;
170
+ export declare const strictType: StrictType;
205
171
  type ParseStringIndex = (stringKey: string) => number | null;
206
- declare const parseStringIndex: ParseStringIndex;
172
+ export declare const parseStringIndex: ParseStringIndex;
207
173
  type IsStringIndex = (stringKey: string) => stringKey is `${number}`;
208
- declare const isStringIndex: IsStringIndex;
209
-
210
- export { type DotPaths, type ExtendedArrayIndexes, type KeyOf, type LiteralIndex, type ObjectLiteral, type PrimitivesAndNativeObjects, type ReturnedValueInPath, type Strict, type Tuple, type UpdateObject, type UpdateValue, type ValueInPath, experimental_extendableUpdate, forEachEntry, isObjectLiteral, isStringIndex, parseStringIndex, remove, s, select, set, splitPathAtLastKey, strict, strictValueWrapper, update };
174
+ export declare const isStringIndex: IsStringIndex;
175
+ export type { DotPaths, ExtendedArrayIndexes, KeyOf, LiteralIndex, ObjectLiteral, PrimitivesAndNativeObjects, ReturnedValueInPath, Strict, Tuple, UpdateObject, UpdateValue, ValueInPath };
package/dist/index.d.ts CHANGED
@@ -6,13 +6,11 @@
6
6
  /** Dot-paths of an array. */
7
7
  type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? ExtendedArrayIndexes : ExtendedArrayIndexes | `${ExtendedArrayIndexes}.${DotPathsImplementation<TInnerType, TAllowedTypes>}` : `${ExtendedArrayIndexes}.${DotPathsImplementation<TInnerType, TAllowedTypes>}`;
8
8
  /** Dot-paths of an object. */
9
- type ObjectPaths<T, TAllowedTypes> = {
10
- [Key in keyof T & (string | number)]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}` : `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}`;
11
- }[keyof T & (string | number)] & {};
9
+ type ObjectPaths<T, TAllowedTypes> = { [Key in keyof T & (string | number)]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}` : `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}`; }[keyof T & (string | number)];
12
10
  /** Dot-paths of a tuple. */
13
- type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes> & {};
14
- /** Dot-paths implementation */
15
- type DotPathsImplementation<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? T extends Tuple ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T, TAllowedTypes>;
11
+ type TuplePaths<T, TAllowedTypes> = ObjectPaths<Required<Omit<T, keyof any[]>>, TAllowedTypes>;
12
+ /** Dot-paths implementation, Required<T> so optional props count as their type ('nickname?: string' is a string) */
13
+ type DotPathsImplementation<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? T extends Tuple ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<Required<T>, TAllowedTypes>;
16
14
  /** Literal union of all posible dot-paths which satisfy provided types as second argument (default: any). */
17
15
  type DotPaths<T, TAllowedTypes = any> = KeyOf<T, TAllowedTypes> | DotPathsImplementation<T, TAllowedTypes>;
18
16
  /**
@@ -20,15 +18,17 @@ type DotPaths<T, TAllowedTypes = any> = KeyOf<T, TAllowedTypes> | DotPathsImplem
20
18
  * VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
21
19
  * --------------------------------------------
22
20
  */
23
- /** Nested type implementation. */
24
- type ValueInPath<TObject, TPath> = TPath extends keyof TObject ? TObject[TPath] : TPath extends '' ? TObject : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ValueInPath<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? ValueInPath<U, R> : never : never : never;
21
+ /** Tuple without rest items ([number, string], not [string, ...number[]]): an index that isn't a key is out of range. */
22
+ type IsFixedTuple<T> = T extends Tuple ? number extends T['length'] ? false : true : false;
23
+ /** Nested type implementation, goes into each object of a union ({ name } | null -> 'name' is string). */
24
+ type ValueInPath<TObject, TPath> = TObject extends unknown ? TPath extends keyof TObject ? TObject[TPath] : TPath extends '' ? TObject : TPath extends LiteralIndex ? never : TPath extends `${number}` ? IsFixedTuple<TObject> extends true ? never : TObject extends ReadonlyArray<infer U> ? U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ValueInPath<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? IsFixedTuple<TObject> extends true ? never : TObject extends ReadonlyArray<infer U> ? ValueInPath<U, R> : never : never : never : never;
25
25
  /**
26
26
  * --------------------------------------------
27
27
  * RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
28
28
  * --------------------------------------------
29
29
  */
30
- /** Returned nested type implementation. */
31
- type ReturnedValueInPath<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TPath extends number ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : TObject[TPath] : TObject[TPath] : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : never : TPath extends LiteralIndex ? never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ReturnedValueInPath<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (ReturnedValueInPath<U, R> | undefined) : never : never : never;
30
+ /** Returned nested type implementation, null or undefined inside the path -> undefined. */
31
+ type ReturnedValueInPath<TObject, TPath> = TPath extends '' ? TObject : TObject extends null | undefined ? undefined : TPath extends keyof TObject ? TPath extends number ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : TObject[TPath] : TObject[TPath] : TPath extends `${number}` ? IsFixedTuple<TObject> extends true ? never : TObject extends ReadonlyArray<infer U> ? (U | undefined) : never : TPath extends LiteralIndex ? never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ReturnedValueInPath<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? IsFixedTuple<TObject> extends true ? never : TObject extends ReadonlyArray<infer U> ? (ReturnedValueInPath<U, R> | undefined) : never : never : never;
32
32
  /**
33
33
  * --------------------------------------------
34
34
  * UPDATE OBJECT WITH DOT PATH IMPLEMENTATION
@@ -37,9 +37,7 @@ type ReturnedValueInPath<TObject, TPath> = TPath extends '' ? TObject : TPath ex
37
37
  /** Value of a dot-path update object, can be the value or a function. */
38
38
  type UpdateValue<TObject, TPath> = ValueInPath<TObject, TPath> extends never ? never : ValueInPath<TObject, TPath> | ((fieldValue: ValueInPath<TObject, TPath>) => Strict<ValueInPath<TObject, TPath>>);
39
39
  /** Dot-path update object, used to give updates to the updateImmutably function. */
40
- type UpdateObject<TObject, TPaths extends DotPaths<TObject> = DotPaths<TObject>> = {
41
- [Path in TPaths]?: UpdateValue<TObject, Path>;
42
- };
40
+ type UpdateObject<TObject, TPaths extends DotPaths<TObject> = DotPaths<TObject>> = { [Path in TPaths]?: UpdateValue<TObject, Path>; };
43
41
  /**
44
42
  * --------------------------------------------
45
43
  * EXTRACT TYPE FROM DOT-PATH UPDATE OBJECT
@@ -47,78 +45,54 @@ type UpdateObject<TObject, TPaths extends DotPaths<TObject> = DotPaths<TObject>>
47
45
  */
48
46
  /** Merging two objects */
49
47
  /** Keys which arent dot-path keys */
50
- type NonDotPathKeys<T> = {
51
- [Key in keyof T]: Key extends `${infer K}.${string}` ? K : Key;
52
- }[keyof T];
48
+ type NonDotPathKeys<T> = { [Key in keyof T]: Key extends `${infer K}.${string}` ? K : Key; }[keyof T];
53
49
  /** Keys which are an index */
54
- type KeysRepresentingAnIndex<T> = {
55
- [Key in keyof T]: Key extends `${number}.${string}` ? never : Key;
56
- }[keyof T];
50
+ type KeysRepresentingAnIndex<T> = { [Key in keyof T]: Key extends `${number}.${string}` ? never : Key; }[keyof T];
57
51
  /** Keys which start with an index */
58
- type KeyPathsStartingWithAnIndex<T> = {
59
- [Key in keyof T]: Key extends `${number}.${string}` ? Key : never;
60
- }[keyof T];
52
+ type KeyPathsStartingWithAnIndex<T> = { [Key in keyof T]: Key extends `${number}.${string}` ? Key : never; }[keyof T];
61
53
  /** Keys which are paths and its first key-path is removed */
62
- type NestedPathKeys<T, K extends string | number> = {
63
- [Key in keyof T]: Key extends `${K}.${infer U}` ? U : never;
64
- }[keyof T];
54
+ type NestedPathKeys<T, K extends string | number> = { [Key in keyof T]: Key extends `${K}.${infer U}` ? U : never; }[keyof T];
65
55
  /** Extract value if its a function */
66
- type Value<T> = T extends (...args: any) => any ? ReturnType<T> : T;
56
+ type Value<T> = T extends ((...args: any) => any) ? ReturnType<T> : T;
67
57
  /** To check if an empty object is provided */
68
58
  type EmptyObject = Record<PropertyKey, never>;
69
59
  /** Used to extract the type from a dot-path update object. */
70
- 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>]> | {
71
- [Key2 in U]: Value<T[KeyPathsStartingWithAnIndex<T>]>;
72
- }> : {
73
- [Key1 in NonDotPathKeys<T> & string]: Key1 extends keyof T ? Value<T[Key1]> extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<{
74
- [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
75
- }> extends Array<infer W> ? Array<U | W> : Array<U> & ExtractTypeFromDotPathUpdateObject<{
76
- [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
77
- }> : Value<T[Key1]> : ExtractTypeFromDotPathUpdateObject<{
78
- [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>;
79
- }>;
80
- } & unknown;
60
+ 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>]> | { [Key2 in U]: Value<T[KeyPathsStartingWithAnIndex<T>]>; }> : { [Key1 in NonDotPathKeys<T> & string]: Key1 extends keyof T ? Value<T[Key1]> extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<{ [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>; }> extends Array<infer W> ? Array<U | W> : Array<U> & ExtractTypeFromDotPathUpdateObject<{ [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>; }> : Value<T[Key1]> : ExtractTypeFromDotPathUpdateObject<{ [Key2 in NestedPathKeys<T, Key1>]: Value<T[`${Key1}.${Key2}`]>; }>; } & unknown;
81
61
  /** New expanded value after aplying updates including new properties */
82
62
  type ExtendedUpdate<TObject, TDotPathUpdateObject extends Record<PropertyKey, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject>;
83
-
84
63
  /** Index literal type to improve DX at managing updates. */
85
64
  declare const LITERAL_INDEX = "${number}";
86
-
87
65
  /** Primitives and browser native objects. */
88
- type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
66
+ type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File | Blob;
89
67
  /** Tuple type, it has to have at least property "0" */
90
68
  type Tuple = ReadonlyArray<any> & {
91
- "0": any;
69
+ "0": any;
92
70
  };
93
71
  /** Array indexes type. */
94
72
  type LiteralIndex = typeof LITERAL_INDEX;
95
73
  type ExtendedArrayIndexes = number | `${number}` | LiteralIndex;
96
- /** Object Literal */
74
+ /** Object Literal, File and Blob still match it (they have no symbol to reject them by) */
97
75
  type ObjectLiteral = {
98
- [x: string]: any;
99
- [x: number]: any;
100
- [x: symbol]: any;
101
- [Symbol.iterator]?: never;
102
- [Symbol.toPrimitive]?: never;
103
- [Symbol.hasInstance]?: never;
104
- [Symbol.unscopables]?: never;
105
- [Symbol.matchAll]?: never;
76
+ [x: string]: any;
77
+ [x: number]: any;
78
+ [x: symbol]: any;
79
+ [Symbol.iterator]?: never;
80
+ [Symbol.toPrimitive]?: never;
81
+ [Symbol.hasInstance]?: never;
82
+ [Symbol.unscopables]?: never;
83
+ [Symbol.matchAll]?: never;
84
+ [Symbol.toStringTag]?: never;
106
85
  };
107
86
  /** Strict key */
108
87
  declare const strictSymbol: unique symbol;
109
- type Strict<T> = T extends ObjectLiteral ? T & {
110
- [strictSymbol]: DotPaths<T>;
88
+ type Strict<T> = T extends PrimitivesAndNativeObjects ? T : T extends ObjectLiteral ? T & {
89
+ [strictSymbol]: DotPaths<T>;
111
90
  } : T;
112
91
  /** Smart KeyOf (doesnt include the "strict" key) */
113
- type KeyOf<T, TAllowedValuesInKeys = any> = T extends ObjectLiteral ? T extends Strict<infer U> ? KeysOfObject<U, TAllowedValuesInKeys> : KeysOfObject<T, TAllowedValuesInKeys> : T extends Tuple ? KeysOfObject<Omit<T, keyof any[]>, TAllowedValuesInKeys> : T extends ReadonlyArray<infer U> ? U extends TAllowedValuesInKeys ? ExtendedArrayIndexes : never : never;
114
- type KeysOfObject<T, TAllowedValuesInKeys> = {
115
- [Key in keyof T]: T[Key] extends TAllowedValuesInKeys ? Key : never;
116
- }[keyof T];
117
-
92
+ type KeyOf<T, TAllowedValuesInKeys = any> = T extends PrimitivesAndNativeObjects ? never : T extends ObjectLiteral ? T extends Strict<infer U> ? KeysOfObject<U, TAllowedValuesInKeys> : KeysOfObject<T, TAllowedValuesInKeys> : T extends Tuple ? KeysOfObject<Omit<T, keyof any[]>, TAllowedValuesInKeys> : T extends ReadonlyArray<infer U> ? U extends TAllowedValuesInKeys ? ExtendedArrayIndexes : never : never;
93
+ type KeysOfObject<T, TAllowedValuesInKeys> = { [Key in keyof T]-?: Required<T>[Key] extends TAllowedValuesInKeys ? Key : never; }[keyof T];
118
94
  /** Returns only the optional keys of an object. */
119
- type OptionalKeys<T> = {
120
- [K in keyof T]-?: {} extends Pick<T, K> ? K : never;
121
- }[keyof T];
95
+ type OptionalKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? K : never; }[keyof T];
122
96
  /** Removes properties or indexes, order doesn't matter. */
123
97
  type Remove = <T extends ObjectLiteral | any[]>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : OptionalKeys<T> | (OptionalKeys<T>)[]) => T;
124
98
  /**
@@ -127,11 +101,10 @@ type Remove = <T extends ObjectLiteral | any[]>(value: T, keys: T extends any[]
127
101
  * @param keys Keys to remove.
128
102
  * @returns New object/array without the provided keys/indexes.
129
103
  */
130
- declare const remove: Remove;
131
-
104
+ export declare const remove: Remove;
132
105
  type Select = {
133
- <T, TPath extends DotPaths<T>>(state: T, path: TPath): ReturnedValueInPath<T, TPath>;
134
- <T, TPath extends ''>(state: T, path: TPath): T;
106
+ <T, TPath extends DotPaths<T>>(state: T, path: TPath): ReturnedValueInPath<T, TPath>;
107
+ <T, TPath extends ''>(state: T, path: TPath): T;
135
108
  };
136
109
  /**
137
110
  * Returns the value from a given dot path of an object/array or undefined if doesn't exist.
@@ -139,8 +112,7 @@ type Select = {
139
112
  * @param path Path indicating the value to extract.
140
113
  * @returns Value of the provided path or undefined.
141
114
  */
142
- declare const select: Select;
143
-
115
+ export declare const select: Select;
144
116
  type PathWithoutLastKey = string;
145
117
  type LastKey = string;
146
118
  type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastKey, LastKey];
@@ -149,21 +121,19 @@ type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastK
149
121
  * @param dotPath A dot path.
150
122
  * @returns [pathWithoutLastKey, lastKey] or ['', lastKey].
151
123
  */
152
- declare const splitPathAtLastKey: SplitDotPathAtLastKey;
153
-
124
+ export declare const splitPathAtLastKey: SplitDotPathAtLastKey;
154
125
  type Set = <T>(newValue: T) => (v: unknown) => T;
155
126
  /**
156
127
  * Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays.
157
128
  * @param newValue New value.
158
129
  * @returns The same value.
159
130
  */
160
- declare const set: Set;
161
-
131
+ export declare const set: Set;
162
132
  type OptionValues = 'error' | 'skip' | 'add';
163
133
  type Options = {
164
- onNewProps?: OptionValues;
165
- onNewIndexes?: OptionValues;
166
- onPathsIntersectedByPrimitiveValues?: OptionValues;
134
+ onNewProps?: OptionValues;
135
+ onNewIndexes?: OptionValues;
136
+ onPathsIntersectedByPrimitiveValues?: OptionValues;
167
137
  };
168
138
  type ImmutableUpdate = <TObject, TAllowedDotPathKeys extends DotPaths<TObject> = DotPaths<TObject>>(state: TObject, dotPathUpdateObject: UpdateObject<TObject, TAllowedDotPathKeys>, options?: Options) => TObject;
169
139
  /**
@@ -175,7 +145,7 @@ type ImmutableUpdate = <TObject, TAllowedDotPathKeys extends DotPaths<TObject> =
175
145
  * @param options Options to change JS behaviour on non-existent fields.
176
146
  * @returns New Updated Object.
177
147
  */
178
- declare const update: ImmutableUpdate;
148
+ export declare const update: ImmutableUpdate;
179
149
  type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TObject> = UpdateObject<TObject>>(state: TObject, dotPathUpdateObject: TUpdateObject) => ExtendedUpdate<TObject, TUpdateObject>;
180
150
  /**
181
151
  * Creates a new object applying immutable updates using dot-path syntax.
@@ -185,26 +155,21 @@ type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TO
185
155
  * @param dotPathUpdateObject Dot Path Update Object.
186
156
  * @returns New Updated Object.
187
157
  */
188
- declare const experimental_extendableUpdate: ExtendableImmutableUpdate;
189
-
158
+ export declare const experimental_extendableUpdate: ExtendableImmutableUpdate;
190
159
  type ForEachEntry = <T extends ObjectLiteral>(object: T, callbackfn: (key: keyof T, value: T[keyof T]) => void) => void;
191
- declare const forEachEntry: ForEachEntry;
192
-
160
+ export declare const forEachEntry: ForEachEntry;
193
161
  /** Checks if any provided value is an object literal or not */
194
162
  type IsObjectLiteral = (valueToTest: unknown) => valueToTest is ObjectLiteral;
195
- declare const isObjectLiteral: IsObjectLiteral;
196
-
163
+ export declare const isObjectLiteral: IsObjectLiteral;
197
164
  /** Adds strict property to an object literal */
198
165
  type StrictFn = <T>(value: T) => Strict<T>;
199
- declare const strict: StrictFn;
200
- declare const s: StrictFn;
166
+ export declare const strict: StrictFn;
167
+ export declare const s: StrictFn;
201
168
  /** Checks the strictness of a type passed to a function or callback without 3rd party implementation */
202
- type StrictValueWrapper = <T extends U, U>(v: T extends U ? DotPaths<T> extends DotPaths<U> ? T : Exclude<DotPaths<T>, DotPaths<U>> : never) => U;
203
- declare const strictValueWrapper: StrictValueWrapper;
204
-
169
+ type StrictType = <T extends U, U>(v: T extends U ? DotPaths<T> extends DotPaths<U> ? T : Exclude<DotPaths<T>, DotPaths<U>> : never) => U;
170
+ export declare const strictType: StrictType;
205
171
  type ParseStringIndex = (stringKey: string) => number | null;
206
- declare const parseStringIndex: ParseStringIndex;
172
+ export declare const parseStringIndex: ParseStringIndex;
207
173
  type IsStringIndex = (stringKey: string) => stringKey is `${number}`;
208
- declare const isStringIndex: IsStringIndex;
209
-
210
- export { type DotPaths, type ExtendedArrayIndexes, type KeyOf, type LiteralIndex, type ObjectLiteral, type PrimitivesAndNativeObjects, type ReturnedValueInPath, type Strict, type Tuple, type UpdateObject, type UpdateValue, type ValueInPath, experimental_extendableUpdate, forEachEntry, isObjectLiteral, isStringIndex, parseStringIndex, remove, s, select, set, splitPathAtLastKey, strict, strictValueWrapper, update };
174
+ export declare const isStringIndex: IsStringIndex;
175
+ export type { DotPaths, ExtendedArrayIndexes, KeyOf, LiteralIndex, ObjectLiteral, PrimitivesAndNativeObjects, ReturnedValueInPath, Strict, Tuple, UpdateObject, UpdateValue, ValueInPath };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var m=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var W=Object.prototype.hasOwnProperty;var N=(e,t)=>{for(var r in t)m(e,r,{get:t[r],enumerable:!0})},F=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of C(t))!W.call(e,s)&&s!==r&&m(e,s,{get:()=>t[s],enumerable:!(n=R(t,s))||n.enumerable});return e};var B=e=>F(m({},"__esModule",{value:!0}),e);var J={};N(J,{experimental_extendableUpdate:()=>A,forEachEntry:()=>v,isObjectLiteral:()=>l,isStringIndex:()=>y,parseStringIndex:()=>E,remove:()=>L,s:()=>V,select:()=>P,set:()=>w,splitPathAtLastKey:()=>g,strict:()=>T,strictValueWrapper:()=>D,update:()=>S});module.exports=B(J);var l=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype,q=Symbol();var H={[q]:[""]};var L=(e,t)=>{if(!Array.isArray(e)&&!l(e))throw'Wrong type provided in "remove" function';let r=Array.isArray(t)?t:[t];if(Array.isArray(e)){let s=Symbol(),o=[...e];return r.forEach(i=>{o[i]=s}),o.filter(i=>i!==s)}let n={...e};return r.forEach(s=>delete n[s]),n};var P=(e,t)=>{if(typeof t!="string")throw new Error("path is not a string");if(!t)return e;let r=t.split("."),n=e;for(;r.length;){let s=r[0],o=r.join(".");if(Object.hasOwn(n,o))return n[o];if(!Object.hasOwn(n,s))return;n=n[s],r.shift()}return n};var g=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 E=e=>{let t=parseInt(e);return t.toString()===e?t:null},y=e=>parseInt(e).toString()===e;var K=e=>JSON.stringify(e),f=e=>Array.isArray(e),U=(e,t,r={})=>{if(!l(e)&&!f(e))throw new Error(`"${K(e)}" is not an literalObject/array`);if(!l(t))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(e)?[...e]:{...e},s={};for(let[o,x]of Object.entries(t)){if(o==="")continue;let i=o.split("."),d=s,p=n;for(;i.length>1;){let O=i.join(".");if(Object.hasOwn(p,O)){i=[O];break}let a=i[0],b=p[a],h=l(b),j=f(b);if(!h&&!j&&!(b===void 0)){if(r.onPathsIntersectedByPrimitiveValues==="error")throw new Error(`Property "${a}" in "${o}" is a primitive`);if(r.onPathsIntersectedByPrimitiveValues==="skip")continue}if(!d[a]){if(!h&&!j){let k=f(p)&&y(a),$=y(i[1]),I=k?r.onNewIndexes:r.onNewProps;if(I==="error")throw new Error(`${a} doesn't exist in "${o}"`);if(I==="skip")continue;p[a]=$?[]:{}}else p[a]=j?[...b]:{...b};d[a]={}}p=p[a],d=d[a],i.shift()}let c=i[0],u=p;if(!Object.hasOwn(u,c)){let a=f(u)&&y(c)?r.onNewIndexes:r.onNewProps;if(a==="error")throw new Error(`Property/Index "${c}" doesnt exist in the path "${o}". Its value is: "${K(u)}"`);if(a==="skip")continue}u[c]=typeof x=="function"?x(u[c]):x,delete d[c]}return n},S=(e,t,r)=>U(e,t,r),A=(e,t)=>U(e,t);var v=(e,t)=>Object.keys(e).forEach(r=>t(r,e[r]));var T=e=>e,V=T,D=e=>e;0&&(module.exports={experimental_extendableUpdate,forEachEntry,isObjectLiteral,isStringIndex,parseStringIndex,remove,s,select,set,splitPathAtLastKey,strict,strictValueWrapper,update});
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype,t=(t,n)=>{if(!Array.isArray(t)&&!e(t))throw`Wrong type provided in "remove" function`;let r=Array.isArray(n)?n:[n];if(Array.isArray(t)){let e=Symbol(),n=[...t];return r.forEach(t=>{n[t]=e}),n.filter(t=>t!==e)}let i={...t};return r.forEach(e=>delete i[e]),i},n=(e,t)=>{if(typeof t!=`string`)throw Error(`path is not a string`);if(!t)return e;let n=t.split(`.`),r=e;for(;n.length;){if(r==null)return;let e=n[0],t=n.join(`.`);if(Object.hasOwn(r,t))return r[t];if(!Object.hasOwn(r,e))return;r=r[e],n.shift()}return r},r=e=>{if(typeof e!=`string`)throw Error(`Wrong dotPath type: ${e}`);let t=e.lastIndexOf(`.`)+1;return[t?e.slice(0,t-1):``,e.slice(t)]},i=e=>()=>e,a=e=>{let t=parseInt(e);return t.toString()===e?t:null},o=e=>parseInt(e).toString()===e,s=e=>JSON.stringify(e),c=e=>Array.isArray(e),l=(t,n,r={})=>{if(!e(t)&&!c(t))throw Error(`"${s(t)}" is not an literalObject/array`);if(!e(n))throw Error(`wrong "updateObject" type provided`);let i=Array.isArray(t)?[...t]:{...t},a={};for(let[t,l]of Object.entries(n)){if(t===``)continue;let n=t.split(`.`),u=a,d=i;for(;n.length>1;){let i=n.join(`.`);if(Object.hasOwn(d,i)){n=[i];break}let a=n[0],s=d[a],l=e(s),f=c(s);if(!l&&!f&&s!==void 0){if(r.onPathsIntersectedByPrimitiveValues===`error`)throw Error(`Property "${a}" in "${t}" is a primitive`);if(r.onPathsIntersectedByPrimitiveValues===`skip`)continue}if(!u[a]){if(!l&&!f){let e=c(d)&&o(a),i=o(n[1]),s=e?r.onNewIndexes:r.onNewProps;if(s===`error`)throw Error(`${a} doesn't exist in "${t}"`);if(s===`skip`)continue;d[a]=i?[]:{}}else d[a]=f?[...s]:{...s};u[a]={}}d=d[a],u=u[a],n.shift()}let f=n[0],p=d;if(!Object.hasOwn(p,f)){let e=c(p)&&o(f)?r.onNewIndexes:r.onNewProps;if(e===`error`)throw Error(`Property/Index "${f}" doesnt exist in the path "${t}". Its value is: "${s(p)}"`);if(e===`skip`)continue}p[f]=typeof l==`function`?l(p[f]):l,delete u[f]}return i},u=(e,t,n)=>l(e,t,n),d=(e,t)=>l(e,t),f=(e,t)=>Object.keys(e).forEach(n=>t(n,e[n])),p=e=>e,m=p,h=e=>e;exports.experimental_extendableUpdate=d,exports.forEachEntry=f,exports.isObjectLiteral=e,exports.isStringIndex=o,exports.parseStringIndex=a,exports.remove=t,exports.s=m,exports.select=n,exports.set=i,exports.splitPathAtLastKey=r,exports.strict=p,exports.strictType=h,exports.update=u;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var l=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype,w=Symbol();var C={[w]:[""]};var E=(e,t)=>{if(!Array.isArray(e)&&!l(e))throw'Wrong type provided in "remove" function';let r=Array.isArray(t)?t:[t];if(Array.isArray(e)){let i=Symbol(),a=[...e];return r.forEach(o=>{a[o]=i}),a.filter(o=>o!==i)}let n={...e};return r.forEach(i=>delete n[i]),n};var K=(e,t)=>{if(typeof t!="string")throw new Error("path is not a string");if(!t)return e;let r=t.split("."),n=e;for(;r.length;){let i=r[0],a=r.join(".");if(Object.hasOwn(n,a))return n[a];if(!Object.hasOwn(n,i))return;n=n[i],r.shift()}return n};var U=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 S=e=>()=>e;var A=e=>{let t=parseInt(e);return t.toString()===e?t:null},b=e=>parseInt(e).toString()===e;var h=e=>JSON.stringify(e),f=e=>Array.isArray(e),I=(e,t,r={})=>{if(!l(e)&&!f(e))throw new Error(`"${h(e)}" is not an literalObject/array`);if(!l(t))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(e)?[...e]:{...e},i={};for(let[a,x]of Object.entries(t)){if(a==="")continue;let o=a.split("."),y=i,p=n;for(;o.length>1;){let O=o.join(".");if(Object.hasOwn(p,O)){o=[O];break}let s=o[0],u=p[s],m=l(u),j=f(u);if(!m&&!j&&!(u===void 0)){if(r.onPathsIntersectedByPrimitiveValues==="error")throw new Error(`Property "${s}" in "${a}" is a primitive`);if(r.onPathsIntersectedByPrimitiveValues==="skip")continue}if(!y[s]){if(!m&&!j){let P=f(p)&&b(s),g=b(o[1]),T=P?r.onNewIndexes:r.onNewProps;if(T==="error")throw new Error(`${s} doesn't exist in "${a}"`);if(T==="skip")continue;p[s]=g?[]:{}}else p[s]=j?[...u]:{...u};y[s]={}}p=p[s],y=y[s],o.shift()}let c=o[0],d=p;if(!Object.hasOwn(d,c)){let s=f(d)&&b(c)?r.onNewIndexes:r.onNewProps;if(s==="error")throw new Error(`Property/Index "${c}" doesnt exist in the path "${a}". Its value is: "${h(d)}"`);if(s==="skip")continue}d[c]=typeof x=="function"?x(d[c]):x,delete y[c]}return n},v=(e,t,r)=>I(e,t,r),V=(e,t)=>I(e,t);var D=(e,t)=>Object.keys(e).forEach(r=>t(r,e[r]));var L=e=>e,k=L,$=e=>e;export{V as experimental_extendableUpdate,D as forEachEntry,l as isObjectLiteral,b as isStringIndex,A as parseStringIndex,E as remove,k as s,K as select,S as set,U as splitPathAtLastKey,L as strict,$ as strictValueWrapper,v as update};
1
+ const e=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype,t=(t,n)=>{if(!Array.isArray(t)&&!e(t))throw`Wrong type provided in "remove" function`;let r=Array.isArray(n)?n:[n];if(Array.isArray(t)){let e=Symbol(),n=[...t];return r.forEach(t=>{n[t]=e}),n.filter(t=>t!==e)}let i={...t};return r.forEach(e=>delete i[e]),i},n=(e,t)=>{if(typeof t!=`string`)throw Error(`path is not a string`);if(!t)return e;let n=t.split(`.`),r=e;for(;n.length;){if(r==null)return;let e=n[0],t=n.join(`.`);if(Object.hasOwn(r,t))return r[t];if(!Object.hasOwn(r,e))return;r=r[e],n.shift()}return r},r=e=>{if(typeof e!=`string`)throw Error(`Wrong dotPath type: ${e}`);let t=e.lastIndexOf(`.`)+1;return[t?e.slice(0,t-1):``,e.slice(t)]},i=e=>()=>e,a=e=>{let t=parseInt(e);return t.toString()===e?t:null},o=e=>parseInt(e).toString()===e,s=e=>JSON.stringify(e),c=e=>Array.isArray(e),l=(t,n,r={})=>{if(!e(t)&&!c(t))throw Error(`"${s(t)}" is not an literalObject/array`);if(!e(n))throw Error(`wrong "updateObject" type provided`);let i=Array.isArray(t)?[...t]:{...t},a={};for(let[t,l]of Object.entries(n)){if(t===``)continue;let n=t.split(`.`),u=a,d=i;for(;n.length>1;){let i=n.join(`.`);if(Object.hasOwn(d,i)){n=[i];break}let a=n[0],s=d[a],l=e(s),f=c(s);if(!l&&!f&&s!==void 0){if(r.onPathsIntersectedByPrimitiveValues===`error`)throw Error(`Property "${a}" in "${t}" is a primitive`);if(r.onPathsIntersectedByPrimitiveValues===`skip`)continue}if(!u[a]){if(!l&&!f){let e=c(d)&&o(a),i=o(n[1]),s=e?r.onNewIndexes:r.onNewProps;if(s===`error`)throw Error(`${a} doesn't exist in "${t}"`);if(s===`skip`)continue;d[a]=i?[]:{}}else d[a]=f?[...s]:{...s};u[a]={}}d=d[a],u=u[a],n.shift()}let f=n[0],p=d;if(!Object.hasOwn(p,f)){let e=c(p)&&o(f)?r.onNewIndexes:r.onNewProps;if(e===`error`)throw Error(`Property/Index "${f}" doesnt exist in the path "${t}". Its value is: "${s(p)}"`);if(e===`skip`)continue}p[f]=typeof l==`function`?l(p[f]):l,delete u[f]}return i},u=(e,t,n)=>l(e,t,n),d=(e,t)=>l(e,t),f=(e,t)=>Object.keys(e).forEach(n=>t(n,e[n])),p=e=>e,m=p,h=e=>e;export{d as experimental_extendableUpdate,f as forEachEntry,e as isObjectLiteral,o as isStringIndex,a as parseStringIndex,t as remove,m as s,n as select,i as set,r as splitPathAtLastKey,p as strict,h as strictType,u as update};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maraj",
3
- "version": "2.10.0",
3
+ "version": "2.11.0",
4
4
  "description": "A lightweight 600 bytes gzipped alternative for immutable updates while dealing with objects and arrays",
5
5
  "keywords": [
6
6
  "shallow",
@@ -21,11 +21,11 @@
21
21
  "scripts": {
22
22
  "dev": "vitest",
23
23
  "test": "vitest run",
24
- "build": "tsup src/index.ts --minify --format cjs,esm --dts",
24
+ "build": "tsdown",
25
25
  "lint": "tsc",
26
26
  "trace": "tsc --generateTrace trace",
27
27
  "ci": "npm run lint && npm run test && npm run build",
28
- "release": "npm run lint && npm run test && npm run build && changeset publish"
28
+ "release": "node scripts/check-changelog.mjs && npm run build && changeset publish"
29
29
  },
30
30
  "repository": {
31
31
  "type": "git",
@@ -36,11 +36,11 @@
36
36
  },
37
37
  "homepage": "https://github.com/Lukasz17git/maraj#readme",
38
38
  "devDependencies": {
39
- "@changesets/cli": "^2.27.1",
40
- "@microsoft/api-extractor": "^7.38.5",
41
- "@typescript/analyze-trace": "^0.10.1",
42
- "tsup": "^8.0.1",
43
- "typescript": "^5.3.3",
44
- "vitest": "^0.28.4"
39
+ "@changesets/cli": "^3.0.3",
40
+ "@microsoft/api-extractor": "^7.59.4",
41
+ "@typescript/analyze-trace": "^0.11.1",
42
+ "tsdown": "^0.23.1",
43
+ "typescript": "^7.0.2",
44
+ "vitest": "^5.0.5"
45
45
  }
46
46
  }