reslib 1.0.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 +298 -0
- package/build/auth/index.d.ts +2034 -0
- package/build/auth/index.js +5 -0
- package/build/auth/types.d.ts +465 -0
- package/build/auth/types.js +1 -0
- package/build/countries/countries.d.ts +1454 -0
- package/build/countries/countries.js +1 -0
- package/build/countries/index.d.ts +159 -0
- package/build/countries/index.js +5 -0
- package/build/countries/types.d.ts +65 -0
- package/build/countries/types.js +1 -0
- package/build/currency/currencies.d.ts +8 -0
- package/build/currency/currencies.js +1 -0
- package/build/currency/index.d.ts +51 -0
- package/build/currency/index.js +5 -0
- package/build/currency/session.d.ts +23 -0
- package/build/currency/session.js +5 -0
- package/build/currency/types.d.ts +1039 -0
- package/build/currency/types.js +1 -0
- package/build/currency/utils.d.ts +25 -0
- package/build/currency/utils.js +1 -0
- package/build/i18n/index.d.ts +640 -0
- package/build/i18n/index.js +5 -0
- package/build/inputFormatter/index.d.ts +396 -0
- package/build/inputFormatter/index.js +5 -0
- package/build/inputFormatter/types.d.ts +544 -0
- package/build/inputFormatter/types.js +1 -0
- package/build/logger/index.d.ts +235 -0
- package/build/logger/index.js +5 -0
- package/build/observable/index.d.ts +329 -0
- package/build/observable/index.js +1 -0
- package/build/platform/index.d.ts +32 -0
- package/build/platform/index.js +1 -0
- package/build/resources/ResourcePaginationHelper.d.ts +537 -0
- package/build/resources/ResourcePaginationHelper.js +2 -0
- package/build/resources/decorators/create.decorator.d.ts +20 -0
- package/build/resources/decorators/create.decorator.js +1 -0
- package/build/resources/decorators/index.d.ts +41 -0
- package/build/resources/decorators/index.js +1 -0
- package/build/resources/fields/index.d.ts +33 -0
- package/build/resources/fields/index.js +1 -0
- package/build/resources/filters.d.ts +62 -0
- package/build/resources/filters.js +1 -0
- package/build/resources/index.d.ts +854 -0
- package/build/resources/index.js +6 -0
- package/build/resources/types/filters.d.ts +508 -0
- package/build/resources/types/filters.js +1 -0
- package/build/resources/types/index.d.ts +4138 -0
- package/build/resources/types/index.js +1 -0
- package/build/session/index.d.ts +1474 -0
- package/build/session/index.js +1 -0
- package/build/translations/auth.en.d.ts +3 -0
- package/build/translations/auth.en.js +1 -0
- package/build/translations/countries.en.d.ts +6 -0
- package/build/translations/countries.en.js +1 -0
- package/build/translations/currencies.en.d.ts +5 -0
- package/build/translations/currencies.en.js +1 -0
- package/build/translations/date.en.d.ts +19 -0
- package/build/translations/date.en.js +1 -0
- package/build/translations/index.d.ts +1583 -0
- package/build/translations/index.js +5 -0
- package/build/translations/resources.en.d.ts +6 -0
- package/build/translations/resources.en.js +1 -0
- package/build/translations/validator.en.d.ts +104 -0
- package/build/translations/validator.en.js +5 -0
- package/build/types/date.d.ts +44 -0
- package/build/types/date.js +1 -0
- package/build/types/dictionary.d.ts +29 -0
- package/build/types/dictionary.js +1 -0
- package/build/types/i18n.d.ts +121 -0
- package/build/types/i18n.js +1 -0
- package/build/types/index.d.ts +145 -0
- package/build/types/index.js +1 -0
- package/build/utils/areEquals.d.ts +19 -0
- package/build/utils/areEquals.js +1 -0
- package/build/utils/date/dateHelper.d.ts +371 -0
- package/build/utils/date/dateHelper.js +5 -0
- package/build/utils/date/index.d.ts +212 -0
- package/build/utils/date/index.js +5 -0
- package/build/utils/date/isDateObj.d.ts +14 -0
- package/build/utils/date/isDateObj.js +1 -0
- package/build/utils/debounce.d.ts +52 -0
- package/build/utils/debounce.js +1 -0
- package/build/utils/defaultArray.d.ts +18 -0
- package/build/utils/defaultArray.js +1 -0
- package/build/utils/defaultBool.d.ts +14 -0
- package/build/utils/defaultBool.js +1 -0
- package/build/utils/defaultStr.d.ts +17 -0
- package/build/utils/defaultStr.js +1 -0
- package/build/utils/defaultVal.d.ts +18 -0
- package/build/utils/defaultVal.js +1 -0
- package/build/utils/dom/index.d.ts +65 -0
- package/build/utils/dom/index.js +1 -0
- package/build/utils/dom/isDOMElement.d.ts +11 -0
- package/build/utils/dom/isDOMElement.js +1 -0
- package/build/utils/file/index.d.ts +26 -0
- package/build/utils/file/index.js +1 -0
- package/build/utils/global.d.ts +53 -0
- package/build/utils/global.js +1 -0
- package/build/utils/image.d.ts +56 -0
- package/build/utils/image.js +1 -0
- package/build/utils/index.d.ts +39 -0
- package/build/utils/index.js +6 -0
- package/build/utils/interpolate.d.ts +105 -0
- package/build/utils/interpolate.js +1 -0
- package/build/utils/isEmail.d.ts +57 -0
- package/build/utils/isEmail.js +1 -0
- package/build/utils/isEmpty.d.ts +18 -0
- package/build/utils/isEmpty.js +1 -0
- package/build/utils/isNonNullString.d.ts +17 -0
- package/build/utils/isNonNullString.js +1 -0
- package/build/utils/isNullable.d.ts +7 -0
- package/build/utils/isNullable.js +1 -0
- package/build/utils/isNumber.d.ts +36 -0
- package/build/utils/isNumber.js +1 -0
- package/build/utils/isPrimitive.d.ts +16 -0
- package/build/utils/isPrimitive.js +1 -0
- package/build/utils/isPromise.d.ts +14 -0
- package/build/utils/isPromise.js +1 -0
- package/build/utils/isRegex.d.ts +15 -0
- package/build/utils/isRegex.js +1 -0
- package/build/utils/isTime.d.ts +18 -0
- package/build/utils/isTime.js +1 -0
- package/build/utils/json.d.ts +224 -0
- package/build/utils/json.js +1 -0
- package/build/utils/numbers.d.ts +148 -0
- package/build/utils/numbers.js +5 -0
- package/build/utils/object.d.ts +567 -0
- package/build/utils/object.js +1 -0
- package/build/utils/sort.d.ts +67 -0
- package/build/utils/sort.js +1 -0
- package/build/utils/string.d.ts +165 -0
- package/build/utils/string.js +1 -0
- package/build/utils/stringify.d.ts +23 -0
- package/build/utils/stringify.js +1 -0
- package/build/utils/uniqid.d.ts +18 -0
- package/build/utils/uniqid.js +1 -0
- package/build/utils/uri/index.d.ts +333 -0
- package/build/utils/uri/index.js +2 -0
- package/build/validator/index.d.ts +4 -0
- package/build/validator/index.js +6 -0
- package/build/validator/rules/array.d.ts +848 -0
- package/build/validator/rules/array.js +5 -0
- package/build/validator/rules/boolean.d.ts +87 -0
- package/build/validator/rules/boolean.js +5 -0
- package/build/validator/rules/date.d.ts +551 -0
- package/build/validator/rules/date.js +5 -0
- package/build/validator/rules/default.d.ts +367 -0
- package/build/validator/rules/default.js +5 -0
- package/build/validator/rules/enum.d.ts +155 -0
- package/build/validator/rules/enum.js +5 -0
- package/build/validator/rules/file.d.ts +356 -0
- package/build/validator/rules/file.js +5 -0
- package/build/validator/rules/format.d.ts +2825 -0
- package/build/validator/rules/format.js +6 -0
- package/build/validator/rules/index.d.ts +16 -0
- package/build/validator/rules/index.js +6 -0
- package/build/validator/rules/multiRules.d.ts +475 -0
- package/build/validator/rules/multiRules.js +5 -0
- package/build/validator/rules/numeric.d.ts +1135 -0
- package/build/validator/rules/numeric.js +5 -0
- package/build/validator/rules/string.d.ts +504 -0
- package/build/validator/rules/string.js +5 -0
- package/build/validator/rules/target.d.ts +137 -0
- package/build/validator/rules/target.js +5 -0
- package/build/validator/rules/utils.d.ts +1 -0
- package/build/validator/rules/utils.js +1 -0
- package/build/validator/rulesMarkers.d.ts +11 -0
- package/build/validator/rulesMarkers.js +1 -0
- package/build/validator/types.d.ts +2906 -0
- package/build/validator/types.js +1 -0
- package/build/validator/validator.d.ts +3692 -0
- package/build/validator/validator.js +5 -0
- package/lib/cjs/auth.js +1 -0
- package/lib/cjs/countries.js +1 -0
- package/lib/cjs/currency.js +1 -0
- package/lib/cjs/i18n.js +1 -0
- package/lib/cjs/inputFormatter.js +1 -0
- package/lib/cjs/logger.js +1 -0
- package/lib/cjs/observable.js +1 -0
- package/lib/cjs/platform.js +1 -0
- package/lib/cjs/resources.js +1 -0
- package/lib/cjs/session.js +1 -0
- package/lib/cjs/types.js +1 -0
- package/lib/cjs/utils.js +1 -0
- package/lib/cjs/validator.js +1 -0
- package/lib/esm/auth.mjs +1 -0
- package/lib/esm/countries.mjs +1 -0
- package/lib/esm/currency.mjs +1 -0
- package/lib/esm/i18n.mjs +1 -0
- package/lib/esm/inputFormatter.mjs +1 -0
- package/lib/esm/logger.mjs +1 -0
- package/lib/esm/observable.mjs +1 -0
- package/lib/esm/platform.mjs +1 -0
- package/lib/esm/resources.mjs +1 -0
- package/lib/esm/session.mjs +1 -0
- package/lib/esm/types.mjs +1 -0
- package/lib/esm/utils.mjs +1 -0
- package/lib/esm/validator.mjs +1 -0
- package/package.json +244 -0
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
import { Primitive } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Determines whether a value is a plain object (POJO - Plain Old JavaScript Object).
|
|
4
|
+
*
|
|
5
|
+
* A plain object is an object created by the Object constructor or one with a null prototype.
|
|
6
|
+
* This function performs comprehensive checks to distinguish plain objects from other object types
|
|
7
|
+
* like arrays, dates, DOM elements, regular expressions, class instances, and functions.
|
|
8
|
+
* It also handles cross-frame compatibility where objects might be created in different execution contexts.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of the value being checked
|
|
11
|
+
* @param {T} obj - The value to test for being a plain object
|
|
12
|
+
*
|
|
13
|
+
* @returns {obj is (T extends (Record<any, any> | object) ? T : T extends string | undefined | null | boolean | Array<any> ? never : any)}
|
|
14
|
+
* Type predicate that narrows the type to a plain object if the check passes, or never for non-object types
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Basic plain object detection
|
|
19
|
+
* const plainObj = { name: "John", age: 30 };
|
|
20
|
+
* console.log(isObj(plainObj)); // true
|
|
21
|
+
*
|
|
22
|
+
* // Object created with Object.create(null)
|
|
23
|
+
* const nullProtoObj = Object.create(null);
|
|
24
|
+
* nullProtoObj.prop = "value";
|
|
25
|
+
* console.log(isObj(nullProtoObj)); // true
|
|
26
|
+
*
|
|
27
|
+
* // Arrays are not plain objects
|
|
28
|
+
* const array = [1, 2, 3];
|
|
29
|
+
* console.log(isObj(array)); // false
|
|
30
|
+
*
|
|
31
|
+
* // Dates are not plain objects
|
|
32
|
+
* const date = new Date();
|
|
33
|
+
* console.log(isObj(date)); // false
|
|
34
|
+
*
|
|
35
|
+
* // Regular expressions are not plain objects
|
|
36
|
+
* const regex = /pattern/;
|
|
37
|
+
* console.log(isObj(regex)); // false
|
|
38
|
+
*
|
|
39
|
+
* // Class instances are not plain objects
|
|
40
|
+
* class MyClass {
|
|
41
|
+
* constructor(public value: string) {}
|
|
42
|
+
* }
|
|
43
|
+
* const instance = new MyClass("test");
|
|
44
|
+
* console.log(isObj(instance)); // false
|
|
45
|
+
*
|
|
46
|
+
* // Functions are not plain objects
|
|
47
|
+
* const func = () => {};
|
|
48
|
+
* console.log(isObj(func)); // false
|
|
49
|
+
*
|
|
50
|
+
* // Primitives are not plain objects
|
|
51
|
+
* console.log(isObj("string")); // false
|
|
52
|
+
* console.log(isObj(42)); // false
|
|
53
|
+
* console.log(isObj(true)); // false
|
|
54
|
+
* console.log(isObj(null)); // false
|
|
55
|
+
* console.log(isObj(undefined)); // false
|
|
56
|
+
*
|
|
57
|
+
* // DOM elements are not plain objects (in browser environment)
|
|
58
|
+
* const element = document.createElement('div');
|
|
59
|
+
* console.log(isObj(element)); // false
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // Type narrowing with TypeScript
|
|
65
|
+
* function processValue<T>(value: T): void {
|
|
66
|
+
* if (isObj(value)) {
|
|
67
|
+
* // TypeScript now knows 'value' is a plain object
|
|
68
|
+
* // Safe to access object properties
|
|
69
|
+
* Object.keys(value).forEach(key => {
|
|
70
|
+
* console.log(`${key}: ${value[key]}`);
|
|
71
|
+
* });
|
|
72
|
+
* } else {
|
|
73
|
+
* // Handle non-object values
|
|
74
|
+
* console.log("Not a plain object:", value);
|
|
75
|
+
* }
|
|
76
|
+
* }
|
|
77
|
+
*
|
|
78
|
+
* // Cross-frame compatibility example
|
|
79
|
+
* // Works even when objects are created in different iframes
|
|
80
|
+
* const iframe = document.createElement('iframe');
|
|
81
|
+
* document.body.appendChild(iframe);
|
|
82
|
+
* const iframeWindow = iframe.contentWindow;
|
|
83
|
+
* const crossFrameObj = new iframeWindow.Object();
|
|
84
|
+
* crossFrameObj.prop = "value";
|
|
85
|
+
* console.log(isObj(crossFrameObj)); // true (handles cross-frame objects)
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @remarks
|
|
89
|
+
* This function is particularly useful for:
|
|
90
|
+
* - Object serialization/deserialization logic
|
|
91
|
+
* - Deep cloning or merging operations where you need to distinguish plain objects
|
|
92
|
+
* - API response validation where you expect plain data objects
|
|
93
|
+
* - Library functions that need to handle various object types differently
|
|
94
|
+
* - Cross-frame scenarios in browser environments
|
|
95
|
+
*
|
|
96
|
+
* The function performs several layers of validation:
|
|
97
|
+
* 1. **Early rejection**: Filters out null, primitives, arrays, dates, regex, and DOM elements
|
|
98
|
+
* 2. **Null prototype check**: Accepts objects created with `Object.create(null)`
|
|
99
|
+
* 3. **Constructor validation**: Verifies the object's constructor is a function
|
|
100
|
+
* 4. **Standard Object check**: Accepts objects with `Object` as constructor
|
|
101
|
+
* 5. **Prototype chain validation**: Ensures the prototype chain is standard
|
|
102
|
+
* 6. **Cross-frame compatibility**: Handles objects from different execution contexts
|
|
103
|
+
*
|
|
104
|
+
*
|
|
105
|
+
* @category Type Guards
|
|
106
|
+
* @see {@link cloneObject} - For cloning plain objects
|
|
107
|
+
* @see {@link extendObj} - For merging plain objects
|
|
108
|
+
* @see {@link defaultObj} - For providing default plain objects
|
|
109
|
+
*/
|
|
110
|
+
export declare function isObj<T = any>(obj: T): obj is T extends Record<any, any> | object ? T : T extends string | undefined | null | boolean | Array<any> ? never : any;
|
|
111
|
+
/**
|
|
112
|
+
* Clones a source object by returning a non-reference copy of the object.
|
|
113
|
+
*
|
|
114
|
+
* This function creates a deep copy of the provided object.
|
|
115
|
+
* Any nested objects or arrays within the source will also be cloned, ensuring that the
|
|
116
|
+
* returned object does not reference the original object.
|
|
117
|
+
*
|
|
118
|
+
* @template T - The type of the object to clone. This can be any type, including arrays and nested objects.
|
|
119
|
+
* @param {T} source - The object to clone. This can be any type, including arrays and nested objects.
|
|
120
|
+
*
|
|
121
|
+
* @returns {T} A deep cloned copy of the source object. The return type can be
|
|
122
|
+
* either an object or an array, depending on the input.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* // Example with a simple object
|
|
127
|
+
* const original = { a: 1, b: { c: 2 } };
|
|
128
|
+
* const cloned = cloneObject(original);
|
|
129
|
+
* console.log(cloned); // Outputs: { a: 1, b: { c: 2 } }
|
|
130
|
+
*
|
|
131
|
+
* // Modifying the cloned object does not affect the original
|
|
132
|
+
* cloned.b.c = 3;
|
|
133
|
+
* console.log(original.b.c); // Outputs: 2 (original remains unchanged)
|
|
134
|
+
*
|
|
135
|
+
* // Example with an array
|
|
136
|
+
* const originalArray = [1, 2, { a: 3 }];
|
|
137
|
+
* const clonedArray = cloneObject(originalArray);
|
|
138
|
+
* console.log(clonedArray); // Outputs: [1, 2, { a: 3 }]
|
|
139
|
+
*
|
|
140
|
+
* // Modifying the cloned array does not affect the original
|
|
141
|
+
* clonedArray[2].a = 4;
|
|
142
|
+
* console.log(originalArray[2].a); // Outputs: 3 (original remains unchanged)
|
|
143
|
+
*
|
|
144
|
+
* // Example with a nested structure
|
|
145
|
+
* const complexObject = { a: 1, b: [2, { c: 3 }] };
|
|
146
|
+
* const clonedComplex = cloneObject(complexObject);
|
|
147
|
+
* console.log(clonedComplex); // Outputs: { a: 1, b: [2, { c: 3 }] }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export declare function cloneObject<T = any>(source: T): T;
|
|
151
|
+
/**
|
|
152
|
+
* Calculates the size of an object or array.
|
|
153
|
+
*
|
|
154
|
+
* This function returns the number of own properties of an object or the length of an array.
|
|
155
|
+
* If the input is null or not an object, it returns 0.
|
|
156
|
+
*
|
|
157
|
+
* @param {any} obj - The object or array whose size is to be calculated.
|
|
158
|
+
* @param {boolean} [breakOnFirstElementFound=false] - Optional flag to determine if the function should
|
|
159
|
+
* return the size immediately upon finding the first property or element.
|
|
160
|
+
*
|
|
161
|
+
* @returns {number} The size of the object or array. Returns 0 if the input is null or not an object.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* // Example with an object
|
|
165
|
+
* const exampleObject = { a: 1, b: 2, c: 3 };
|
|
166
|
+
* const size = objectSize(exampleObject);
|
|
167
|
+
* console.log(size); // Outputs: 3
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* // Example with an array
|
|
171
|
+
* const exampleArray = [1, 2, 3, 4];
|
|
172
|
+
* const arraySize = objectSize(exampleArray);
|
|
173
|
+
* console.log(arraySize); // Outputs: 4
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* // Example with breakOnFirstElementFound
|
|
177
|
+
* const earlyExitSize = objectSize(exampleObject, true);
|
|
178
|
+
* console.log(earlyExitSize); // Outputs: 1, as it returns after the first property
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* // Example with a null input
|
|
182
|
+
* const nullSize = objectSize(null);
|
|
183
|
+
* console.log(nullSize); // Outputs: 0
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* // Example with a non-object input
|
|
187
|
+
* const nonObjectSize = objectSize(42);
|
|
188
|
+
* console.log(nonObjectSize); // Outputs: 0
|
|
189
|
+
*/
|
|
190
|
+
export declare const objectSize: (obj: any, breakOnFirstElementFound?: boolean) => number;
|
|
191
|
+
/**
|
|
192
|
+
* Returns a default object based on the provided arguments.
|
|
193
|
+
*
|
|
194
|
+
* This function takes multiple arguments and returns the first non-empty object found.
|
|
195
|
+
* If only one argument is provided, it returns that argument if it is an object; otherwise, it returns an empty object.
|
|
196
|
+
* If no valid object is found among the arguments, it returns an empty object.
|
|
197
|
+
*
|
|
198
|
+
* @param {...any[]} args - The arguments to check for objects.
|
|
199
|
+
* @template T - The type of the object to return.
|
|
200
|
+
*
|
|
201
|
+
* @returns {object} The first non-empty object found among the arguments, or an empty object if none is found.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```ts
|
|
205
|
+
* // Example with one valid object
|
|
206
|
+
* const result1 = defaultObj({ a: 1 });
|
|
207
|
+
* console.log(result1); // Outputs: { a: 1 }
|
|
208
|
+
*
|
|
209
|
+
* // Example with one invalid argument
|
|
210
|
+
* const result2 = defaultObj("not an object");
|
|
211
|
+
* console.log(result2); // Outputs: {}
|
|
212
|
+
*
|
|
213
|
+
* // Example with multiple arguments, returning the first non-empty object
|
|
214
|
+
* const result3 = defaultObj({}, { b: 2 }, { c: 3 });
|
|
215
|
+
* console.log(result3); // Outputs: { b: 2 }
|
|
216
|
+
*
|
|
217
|
+
* // Example with multiple arguments, returning the last valid object
|
|
218
|
+
* const result4 = defaultObj({}, {}, { d: 4 });
|
|
219
|
+
* console.log(result4); // Outputs: { d: 4 }
|
|
220
|
+
*
|
|
221
|
+
* // Example with no valid objects
|
|
222
|
+
* const result5 = defaultObj(null, undefined, "string");
|
|
223
|
+
* console.log(result5); // Outputs: {}
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
export declare function defaultObj<T extends object = any>(...args: any[]): T;
|
|
227
|
+
/**
|
|
228
|
+
* Declares a global interface extension for the built-in `Object` type.
|
|
229
|
+
*/
|
|
230
|
+
declare global {
|
|
231
|
+
/**
|
|
232
|
+
* Interface extension for the built-in `Object` type.
|
|
233
|
+
*/
|
|
234
|
+
interface Object {
|
|
235
|
+
/**
|
|
236
|
+
* Clones a source object by returning a non-reference copy of the object.
|
|
237
|
+
*
|
|
238
|
+
* This method creates a deep clone of the provided object, ensuring that nested objects
|
|
239
|
+
* are also cloned, and modifications to the cloned object do not affect the original.
|
|
240
|
+
*
|
|
241
|
+
* @param {any} obj - The object to clone. This can be any type, including arrays and nested objects.
|
|
242
|
+
* @returns {any} The cloned object, which can be an object or an array.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* const original = { a: 1, b: { c: 2 } };
|
|
247
|
+
* const cloned = Object.clone(original);
|
|
248
|
+
* console.log(cloned); // Outputs: { a: 1, b: { c: 2 } }
|
|
249
|
+
*
|
|
250
|
+
* cloned.b.c = 3;
|
|
251
|
+
* console.log(original.b.c); // Outputs: 2 (original remains unchanged)
|
|
252
|
+
* ```
|
|
253
|
+
* @template T - The type of the object to clone.
|
|
254
|
+
* @param {T} obj - The object to clone.
|
|
255
|
+
* @returns {T} A clone of the object.
|
|
256
|
+
*/
|
|
257
|
+
clone: <T = any>(obj: T) => T;
|
|
258
|
+
/**
|
|
259
|
+
* Determines the size of an object or array.
|
|
260
|
+
*
|
|
261
|
+
* This method calculates the number of own enumerable properties in an object or
|
|
262
|
+
* the number of elements in an array. If the `breakOnFirstElementFound` parameter
|
|
263
|
+
* is set to true, it will return 1 immediately upon finding the first element or property.
|
|
264
|
+
*
|
|
265
|
+
* @param {any} obj - The object or array to determine the size of.
|
|
266
|
+
* @param {boolean} [breakOnFirstElementFound=false] - Whether to return immediately after the first element is found.
|
|
267
|
+
* @returns {number} The size of the object or array. Returns 0 if the input is not an object or array.
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* ```ts
|
|
271
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
272
|
+
* const size = Object.getSize(obj);
|
|
273
|
+
* console.log(size); // Outputs: 3
|
|
274
|
+
*
|
|
275
|
+
* const arr = [1, 2, 3];
|
|
276
|
+
* const arrSize = Object.getSize(arr);
|
|
277
|
+
* console.log(arrSize); // Outputs: 3
|
|
278
|
+
*
|
|
279
|
+
* const singleElementSize = Object.getSize(arr, true);
|
|
280
|
+
* console.log(singleElementSize); // Outputs: 1 (returns immediately)
|
|
281
|
+
*
|
|
282
|
+
* const emptyObjSize = Object.getSize({});
|
|
283
|
+
* console.log(emptyObjSize); // Outputs: 0
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
getSize: (obj: any, breakOnFirstElementFound?: boolean) => number;
|
|
287
|
+
/**
|
|
288
|
+
* Flattens a nested object structure into a single-level object with dot/bracket notation keys.
|
|
289
|
+
* Handles various data structures including Arrays, Sets, Maps, and plain objects.
|
|
290
|
+
* Skips non-primitive values like functions, class instances.
|
|
291
|
+
*
|
|
292
|
+
* @param {any} obj - The object to flatten
|
|
293
|
+
* @param {string} [prefix=''] - The prefix to use for nested keys
|
|
294
|
+
* @returns {Record<string, Primitive>} A flattened object with primitive values
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* // Basic object flattening
|
|
298
|
+
* _flattenObject({
|
|
299
|
+
* a: {
|
|
300
|
+
* b: 'value',
|
|
301
|
+
* c: 42
|
|
302
|
+
* }
|
|
303
|
+
* })
|
|
304
|
+
* // Returns: { 'a.b': 'value', 'a.c': 42 }
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* // Array handling
|
|
308
|
+
* _flattenObject({
|
|
309
|
+
* items: ['a', 'b', { nested: 'value' }]
|
|
310
|
+
* })
|
|
311
|
+
* // Returns: { 'items[0]': 'a', 'items[1]': 'b', 'items[2].nested': 'value' }
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* // Map handling
|
|
315
|
+
* _flattenObject({
|
|
316
|
+
* map: new Map([
|
|
317
|
+
* ['key1', 'value1'],
|
|
318
|
+
* ['key2', { nested: 'value2' }]
|
|
319
|
+
* ])
|
|
320
|
+
* })
|
|
321
|
+
* // Returns: { 'map[key1]': 'value1', 'map[key2].nested': 'value2' }
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* // Complex nested structure
|
|
325
|
+
* _flattenObject({
|
|
326
|
+
* array: [1, { a: 2 }],
|
|
327
|
+
* set: new Set(['x', { b: 'y' }]),
|
|
328
|
+
* map: new Map([['k', { c: 'v' }]]),
|
|
329
|
+
* obj: {
|
|
330
|
+
* deep: {
|
|
331
|
+
* nested: 'value',
|
|
332
|
+
* fn: () => {}, // Will be skipped
|
|
333
|
+
* date: new Date() // Will be skipped
|
|
334
|
+
* }
|
|
335
|
+
* }
|
|
336
|
+
* })
|
|
337
|
+
* // Returns: {
|
|
338
|
+
* // 'array[0]': 1,
|
|
339
|
+
* // 'array[1].a': 2,
|
|
340
|
+
* // 'set[0]': 'x',
|
|
341
|
+
* // 'set[1].b': 'y',
|
|
342
|
+
* // 'map[k].c': 'v',
|
|
343
|
+
* // 'obj.deep.nested': 'value'
|
|
344
|
+
* // }
|
|
345
|
+
*
|
|
346
|
+
* @throws {Error} Will not throw errors, but silently skips non-primitive values
|
|
347
|
+
*
|
|
348
|
+
* @category Utilities
|
|
349
|
+
*
|
|
350
|
+
*/
|
|
351
|
+
flatten(obj: any): Record<string, Primitive>;
|
|
352
|
+
/**
|
|
353
|
+
* Enhanced version of Object.entries that preserves key types in TypeScript inference.
|
|
354
|
+
*
|
|
355
|
+
* Unlike the standard Object.entries which types all keys as string, this method
|
|
356
|
+
* maintains the original key types (string | number | symbol) in TypeScript's type system
|
|
357
|
+
* while still following JavaScript's runtime behavior where all keys are strings.
|
|
358
|
+
*
|
|
359
|
+
* @template T - The object type extending Record<string | number, any>
|
|
360
|
+
* @param obj - The object to extract entries from
|
|
361
|
+
* @returns Array of key-value tuples with preserved key types in TypeScript
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```typescript
|
|
365
|
+
* const obj = { 1: "one", "foo": "bar", 42: "answer" } as const;
|
|
366
|
+
*
|
|
367
|
+
* // Standard Object.entries - all keys typed as string
|
|
368
|
+
* const standard = Object.entries(obj); // [string, string][]
|
|
369
|
+
*
|
|
370
|
+
* // Enhanced Object.typedEntries - preserves original key types
|
|
371
|
+
* const typed = Object.typedEntries(obj);
|
|
372
|
+
* // Type: (["1", "one"] | ["foo", "bar"] | ["42", "answer"])[]
|
|
373
|
+
* ```
|
|
374
|
+
*
|
|
375
|
+
* @remarks
|
|
376
|
+
* - Runtime behavior is identical to Object.entries (all keys are strings)
|
|
377
|
+
* - Only TypeScript inference is enhanced to remember original key types
|
|
378
|
+
* - Works best with objects declared with 'as const' for literal type inference
|
|
379
|
+
* - Particularly useful for objects with mixed string and numeric keys
|
|
380
|
+
*
|
|
381
|
+
*
|
|
382
|
+
*/
|
|
383
|
+
typedEntries<T extends Record<any, unknown> = any>(obj: T): Array<{
|
|
384
|
+
[K in keyof T]: [K, T[K]];
|
|
385
|
+
}[keyof T]>;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Extends an object by merging properties from one or more source objects.
|
|
390
|
+
*
|
|
391
|
+
* This function takes a target object and one or more source objects, merging the properties
|
|
392
|
+
* from the source objects into the target object. If a property exists in both the target
|
|
393
|
+
* and a source object, the value from the source object will overwrite the target's value.
|
|
394
|
+
* For arrays, the function merges the contents of the arrays, preserving the original order of the elements.
|
|
395
|
+
*@template T - The type of the target object.
|
|
396
|
+
* @param {T} target - The object to extend. It will receive the new properties.
|
|
397
|
+
* @param {...any[]} sources - The source objects to merge into the target. These objects will not be modified.
|
|
398
|
+
* @returns {T} The extended target object, which contains properties from the source objects.
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* const target = { a: 1, b: 2 };
|
|
403
|
+
* const source1 = { b: 3, c: 4 };
|
|
404
|
+
* const source2 = { d: 5 };
|
|
405
|
+
*
|
|
406
|
+
* const extended = extendObj(target, source1, source2);
|
|
407
|
+
* console.log(extended); // Outputs: { a: 1, b: 3, c: 4, d: 5 }
|
|
408
|
+
* console.log(target); // Outputs: { a: 1, b: 3 } (target is modified)
|
|
409
|
+
* ```
|
|
410
|
+
* Merges the contents of two or more objects together into the first object.
|
|
411
|
+
* Similar to jQuery's $.extend function.
|
|
412
|
+
* @remarks
|
|
413
|
+
* This function is used to merge the contents of multiple objects into a single object. It takes an optional target object as the first argument and one or more source objects as additional arguments. The function returns the merged object, which is a new object that contains all the properties from the source objects.
|
|
414
|
+
*
|
|
415
|
+
* If the target object is not provided or is not a plain object, an empty object is returned.
|
|
416
|
+
*
|
|
417
|
+
* If the target object is a plain object, the function iterates over the sources and copies the properties from each source object to the target object. If a property with the same name already exists in the target object, it is overwritten with the corresponding value from the source object.
|
|
418
|
+
*
|
|
419
|
+
* If the target object is a plain object, the function iterates over the sources and copies the properties from each source object to the target object. If a property with the same name already exists in the target object, it is overwritten with the corresponding value from the source object.
|
|
420
|
+
* For arrays, The function replaces the contents of the arrays, preserving the original order of the elements.
|
|
421
|
+
* Empty values like null, undefined, and empty strings are ignored.
|
|
422
|
+
*/
|
|
423
|
+
export declare function extendObj<T extends Record<any, any> = any>(target: any, ...sources: any[]): T;
|
|
424
|
+
/**
|
|
425
|
+
* Flattens a nested object structure into a single-level object with dot/bracket notation keys.
|
|
426
|
+
* Handles various data structures including Arrays, Sets, Maps, and plain objects.
|
|
427
|
+
* Skips non-primitive values like functions, class instances.
|
|
428
|
+
*
|
|
429
|
+
* @param {any} obj - The object to flatten
|
|
430
|
+
* @param {string} [prefix=''] - The prefix to use for nested keys
|
|
431
|
+
* @returns {Record<string, Primitive>} A flattened object with primitive values
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* // Basic object flattening
|
|
435
|
+
* _flattenObject({
|
|
436
|
+
* a: {
|
|
437
|
+
* b: 'value',
|
|
438
|
+
* c: 42
|
|
439
|
+
* }
|
|
440
|
+
* })
|
|
441
|
+
* // Returns: { 'a.b': 'value', 'a.c': 42 }
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* // Array handling
|
|
445
|
+
* _flattenObject({
|
|
446
|
+
* items: ['a', 'b', { nested: 'value' }]
|
|
447
|
+
* })
|
|
448
|
+
* // Returns: { 'items[0]': 'a', 'items[1]': 'b', 'items[2].nested': 'value' }
|
|
449
|
+
*
|
|
450
|
+
* @example
|
|
451
|
+
* // Map handling
|
|
452
|
+
* _flattenObject({
|
|
453
|
+
* map: new Map([
|
|
454
|
+
* ['key1', 'value1'],
|
|
455
|
+
* ['key2', { nested: 'value2' }]
|
|
456
|
+
* ])
|
|
457
|
+
* })
|
|
458
|
+
* // Returns: { 'map[key1]': 'value1', 'map[key2].nested': 'value2' }
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* // Complex nested structure
|
|
462
|
+
* _flattenObject({
|
|
463
|
+
* array: [1, { a: 2 }],
|
|
464
|
+
* set: new Set(['x', { b: 'y' }]),
|
|
465
|
+
* map: new Map([['k', { c: 'v' }]]),
|
|
466
|
+
* obj: {
|
|
467
|
+
* deep: {
|
|
468
|
+
* nested: 'value',
|
|
469
|
+
* fn: () => {}, // Will be skipped
|
|
470
|
+
* date: new Date() // Will be skipped
|
|
471
|
+
* }
|
|
472
|
+
* }
|
|
473
|
+
* })
|
|
474
|
+
* // Returns: {
|
|
475
|
+
* // 'array[0]': 1,
|
|
476
|
+
* // 'array[1].a': 2,
|
|
477
|
+
* // 'set[0]': 'x',
|
|
478
|
+
* // 'set[1].b': 'y',
|
|
479
|
+
* // 'map[k].c': 'v',
|
|
480
|
+
* // 'obj.deep.nested': 'value'
|
|
481
|
+
* // }
|
|
482
|
+
*
|
|
483
|
+
* @throws {Error} Will not throw errors, but silently skips non-primitive values
|
|
484
|
+
*
|
|
485
|
+
* @category Utilities
|
|
486
|
+
*
|
|
487
|
+
*/
|
|
488
|
+
export declare function flattenObject(obj: any, options?: FlattenObjectOptions): Record<string, any>;
|
|
489
|
+
/**
|
|
490
|
+
* Checks if a value is an iterable data structure (Array, Set, Map, WeakMap, WeakSet).
|
|
491
|
+
*
|
|
492
|
+
* @param {any} value - The value to check
|
|
493
|
+
* @returns {boolean} True if the value is an iterable structure, false otherwise
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* isIterableStructure([1, 2, 3]) // returns true
|
|
497
|
+
* isIterableStructure(new Set([1, 2, 3])) // returns true
|
|
498
|
+
* isIterableStructure(new Map()) // returns true
|
|
499
|
+
* isIterableStructure({}) // returns false
|
|
500
|
+
*/
|
|
501
|
+
export declare function isIterableStructure(value: any): boolean;
|
|
502
|
+
/**
|
|
503
|
+
* Enhanced version of Object.entries that preserves key types in TypeScript inference.
|
|
504
|
+
*
|
|
505
|
+
* Unlike the standard Object.entries which types all keys as string, this method
|
|
506
|
+
* maintains the original key types (string | number | symbol) in TypeScript's type system
|
|
507
|
+
* while still following JavaScript's runtime behavior where all keys are strings.
|
|
508
|
+
*
|
|
509
|
+
* @template T - The object type extending Record<string | number, any>
|
|
510
|
+
* @param obj - The object to extract entries from
|
|
511
|
+
* @returns Array of key-value tuples with preserved key types in TypeScript
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```typescript
|
|
515
|
+
* const obj = { 1: "one", "foo": "bar", 42: "answer" } as const;
|
|
516
|
+
*
|
|
517
|
+
* // Standard Object.entries - all keys typed as string
|
|
518
|
+
* const standard = Object.entries(obj); // [string, string][]
|
|
519
|
+
*
|
|
520
|
+
* // Enhanced Object.typedEntries - preserves original key types
|
|
521
|
+
* const typed = Object.typedEntries(obj);
|
|
522
|
+
* // Type: (["1", "one"] | ["foo", "bar"] | ["42", "answer"])[]
|
|
523
|
+
* ```
|
|
524
|
+
*
|
|
525
|
+
* @remarks
|
|
526
|
+
* - Runtime behavior is identical to Object.entries (all keys are strings)
|
|
527
|
+
* - Only TypeScript inference is enhanced to remember original key types
|
|
528
|
+
* - Works best with objects declared with 'as const' for literal type inference
|
|
529
|
+
* - Particularly useful for objects with mixed string and numeric keys
|
|
530
|
+
*
|
|
531
|
+
*
|
|
532
|
+
*/
|
|
533
|
+
export declare function typedEntries<T extends Record<any, unknown> = any>(obj: T): Array<{
|
|
534
|
+
[K in keyof T]: [K, T[K]];
|
|
535
|
+
}[keyof T]>;
|
|
536
|
+
export interface FlattenObjectOptions {
|
|
537
|
+
/**
|
|
538
|
+
* Whether to skip arrays during the flattening process.
|
|
539
|
+
*
|
|
540
|
+
* When set to `true`, arrays will be treated as primitive values and included
|
|
541
|
+
* directly in the flattened result instead of being recursively flattened.
|
|
542
|
+
* This can be useful for performance optimization or when you want to preserve
|
|
543
|
+
* array structures in your flattened output.
|
|
544
|
+
*
|
|
545
|
+
* @default false - Arrays are recursively flattened by default
|
|
546
|
+
*
|
|
547
|
+
* @example
|
|
548
|
+
* ```typescript
|
|
549
|
+
* const obj = {
|
|
550
|
+
* name: 'John',
|
|
551
|
+
* tags: ['developer', 'typescript'],
|
|
552
|
+
* scores: [85, 92, 78]
|
|
553
|
+
* };
|
|
554
|
+
*
|
|
555
|
+
* // Default behavior (skipArrays: false)
|
|
556
|
+
* flattenObject(obj);
|
|
557
|
+
* // Result: { 'name': 'John', 'tags[0]': 'developer', 'tags[1]': 'typescript', 'scores[0]': 85, 'scores[1]': 92, 'scores[2]': 78 }
|
|
558
|
+
*
|
|
559
|
+
* // With skipArrays: true
|
|
560
|
+
* flattenObject(obj, { skipArrays: true });
|
|
561
|
+
* // Result: { 'name': 'John', 'tags': ['developer', 'typescript'], 'scores': [85, 92, 78] }
|
|
562
|
+
* ```
|
|
563
|
+
*
|
|
564
|
+
*
|
|
565
|
+
*/
|
|
566
|
+
skipArrays?: boolean;
|
|
567
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';function p(n){return !n||typeof n!="object"?false:n instanceof Date?true:typeof n.getTime!="function"?false:!(Object.prototype.toString.call(n)!=="[object Date]"||isNaN(n.getTime()))}function d(n){return typeof window!="object"||!window||typeof document=="undefined"||typeof HTMLElement=="undefined"||!HTMLElement?false:n===document?true:"HTMLElement"in window?!!n&&n instanceof HTMLElement:!!n&&typeof n=="object"&&n.nodeType===1&&!!n.nodeName}function T(n){return n==null||typeof n=="string"||typeof n=="number"||typeof n=="boolean"}function m(n){if(n instanceof RegExp)return true;if(!n||typeof n!="object"||!Object.prototype.toString.call(n).includes("RegExp"))return false;try{return new RegExp(n),!0}catch(e){return false}}function f(n){if(n===null||typeof n!="object"||d(n)||p(n)||m(n)||T(n))return false;let e=Object.getPrototypeOf(n);if(e===null)return true;let t=e.constructor;if(typeof t!="function")return false;if(t===Object)return true;let r=t.prototype;return typeof r!="object"?false:r===Object.prototype?true:typeof e.hasOwnProperty=="function"&&Object.prototype.hasOwnProperty.call(e,"isPrototypeOf")&&typeof e.isPrototypeOf=="function"}function A(n){if(Array.isArray(n)){let r=[];for(var e=0;e<n.length;e++)r[e]=A(n[e]);return r}else if(f(n)&&n){let r={};for(var t in n)n.hasOwnProperty(t)&&(r[t]=A(n[t]));return r}else return n}var $=Object.getSize=function(n,e=false){if(!n||typeof n!="object")return 0;if(Array.isArray(n))return n.length;typeof e!="boolean"&&(e=false);let t=0;for(let r in n)if(Object.prototype.hasOwnProperty.call(n,r)&&(t++,e===true))return t;return t};function C(...n){if(n.length===1)return f(n[0])?n[0]:{};let e=null;for(var t in n){let r=n[t];if(f(r)){if(Object.getSize(r,true)>0)return r;e||(e=r);}}return e||{}}function h(n,...e){let t=Array.isArray(n),r=f(n);(n==null||!t&&!r)&&(n={});for(let i=0;i<e.length;i++){let o=e[i];if(o==null)continue;let a=f(o),y=Array.isArray(o);if(!(!a&&!y)){if(t){y&&w(n,o);continue}else if(y)continue;for(let c in o){let s=o[c];if(s==null)continue;if(s===o){n[c]=n;continue}let u=n[c],x=Array.isArray(u),O=Array.isArray(s);if(x){O?w(n[c],s):n[c]=s;continue}else if(!f(u)){n[c]=s;continue}if(O||!f(s)){n[c]=s;continue}n[c]=h({},u,s);}}}return n}var w=(n,e)=>{let t=e.length,r=0;for(let i=0;i<n.length;i++){let o=n[i],a=e[i];if(i<t){let y=Array.isArray(o),c=Array.isArray(a),s=f(o),u=f(a);y&&c||s&&u?(n[r]=h(y?[]:{},o,a),r++):a!=null?(n[r]=a,r++):o!=null&&(n[r]=o,r++);}}for(let i=n.length;i<t;i++)e[i]!==void 0&&e[i]!==null&&n.push(e[i]);return n};function b(n,e){return l(n,"",{},e)}function l(n,e="",t={},r){if(t=f(t)?t:{},T(n)||p(n)||m(n)||Array.isArray(n)&&(r!=null&&r.skipArrays))return e&&(t[e]=n),t;if(typeof n=="function"||typeof n=="object"&&!f(n)&&!g(n))return t;if(n instanceof Map||n instanceof WeakMap)return Array.from(n.entries()).forEach(([i,o])=>{let a=e?`${e}[${String(i)}]`:String(i);l(o,a,t,r);}),t;if(Array.isArray(n)||n instanceof Set||n instanceof WeakSet)return (Array.isArray(n)?n:Array.from(n)).forEach((o,a)=>{let y=e?`${e}[${a}]`:String(a);l(o,y,t,r);}),t;if(f(n))for(let i in n){if(!Object.prototype.hasOwnProperty.call(n,i))continue;let o=n[i],a=e?e.endsWith("]")?`${e}.${i}`:`${e}.${i}`:i;l(o,a,t,r);}return t}function g(n){return Array.isArray(n)||n instanceof Set||n instanceof Map||n instanceof WeakMap||n instanceof WeakSet}function S(n){return Object.entries(n)}Object.typedEntries=S;Object.flatten=b;Object.clone=A;exports.cloneObject=A;exports.defaultObj=C;exports.extendObj=h;exports.flattenObject=b;exports.isIterableStructure=g;exports.isObj=f;exports.objectSize=$;exports.typedEntries=S;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A highly optimized sorting function capable of efficiently handling billions of array elements
|
|
3
|
+
* with support for complex objects and various data types.
|
|
4
|
+
*
|
|
5
|
+
* For very large arrays (millions of elements), this function automatically switches to
|
|
6
|
+
* a chunked sorting algorithm that reduces memory pressure and improves cache efficiency.
|
|
7
|
+
* You can control the chunk size with the `chunkSize` option.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of array elements being sorted
|
|
10
|
+
* @template V - The type of values being compared for sorting
|
|
11
|
+
*
|
|
12
|
+
* @param data - The array to be sorted
|
|
13
|
+
* @param getItemValue - Function that extracts the comparable value from each array item
|
|
14
|
+
* @param options - Configuration options to control the sorting behavior
|
|
15
|
+
*
|
|
16
|
+
* @returns The sorted array (either the original array modified in-place or a new array)
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Sort an array of objects by their 'name' property
|
|
20
|
+
* const users = [
|
|
21
|
+
* { id: 101, name: "Alice", age: 28 },
|
|
22
|
+
* { id: 102, name: "bob", age: 34 },
|
|
23
|
+
* { id: 103, name: "Charlie", age: 21 }
|
|
24
|
+
* ];
|
|
25
|
+
*
|
|
26
|
+
* // Case-sensitive sort (default)
|
|
27
|
+
* const sortedByName = sortBy(users, user => user.name);
|
|
28
|
+
* // Result: [{ id: 101, name: "Alice", age: 28 }, { id: 103, name: "Charlie", age: 21 }, { id: 102, name: "bob", age: 34 }]
|
|
29
|
+
*
|
|
30
|
+
* // Case-insensitive sort
|
|
31
|
+
* const sortedIgnoringCase = sortBy(users, user => user.name, { ignoreCase: true });
|
|
32
|
+
* // Result: [{ id: 101, name: "Alice", age: 28 }, { id: 102, name: "bob", age: 34 }, { id: 103, name: "Charlie", age: 21 }]
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // Sort by date values in descending order (newest first)
|
|
36
|
+
* const tasks = [
|
|
37
|
+
* { id: 1, title: "Task 1", deadline: new Date("2023-12-01") },
|
|
38
|
+
* { id: 2, title: "Task 2", deadline: new Date("2023-05-15") },
|
|
39
|
+
* { id: 3, title: "Task 3", deadline: new Date("2024-02-20") }
|
|
40
|
+
* ];
|
|
41
|
+
*
|
|
42
|
+
* const sortedByDeadline = sortBy(tasks, task => task.deadline, { direction: 'desc' });
|
|
43
|
+
* // Result: Tasks ordered with newest deadline first
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* // Create a new sorted array without modifying the original
|
|
47
|
+
* const numbers = [5, 2, 9, 1, 5, 6];
|
|
48
|
+
* const sortedNumbers = sortBy(numbers, n => n, { inPlace: false });
|
|
49
|
+
* // numbers is still [5, 2, 9, 1, 5, 6]
|
|
50
|
+
* // sortedNumbers is [1, 2, 5, 5, 6, 9]
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* // Handle very large datasets with custom chunk size for memory efficiency
|
|
54
|
+
* const largeDataset = Array.from({ length: 1000000 }, () => Math.random());
|
|
55
|
+
* const sortedLarge = sortBy(largeDataset, n => n, { chunkSize: 25000 });
|
|
56
|
+
* // Uses chunked sorting to reduce memory pressure on large arrays
|
|
57
|
+
*/
|
|
58
|
+
export declare function sortBy<T, V = unknown>(data: T[], getItemValue: (item: T) => V, options?: {
|
|
59
|
+
direction?: SortOrder;
|
|
60
|
+
inPlace?: boolean;
|
|
61
|
+
/** Controls chunk size for large array sorting. Arrays larger than chunkSize
|
|
62
|
+
* will be sorted using a memory-efficient chunked algorithm. Default: 50000 */
|
|
63
|
+
chunkSize?: number;
|
|
64
|
+
ignoreCase?: boolean;
|
|
65
|
+
}): T[];
|
|
66
|
+
type SortOrder = 'asc' | 'desc';
|
|
67
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';function S(n,t,e={}){if(!Array.isArray(n))return [];if(n.length<=1)return e.inPlace===false?[...n]:n;let f=e.direction==="desc"?"desc":"asc",s=e.ignoreCase!==false,c=e.inPlace!==false,i=e.chunkSize&&e.chunkSize>0?e.chunkSize:5e4,r=c?n:[...n];return r.length>i?T(r,t,f,s,i):r.sort((o,l)=>u(t(o),t(l),f,s))}function u(n,t,e,f){if(n==null&&t==null)return 0;if(n==null)return e==="asc"?-1:1;if(t==null)return e==="asc"?1:-1;if(typeof n=="number"&&typeof t=="number"){let r=n-t;return e==="asc"?r:-r}if(typeof n=="string"&&typeof t=="string"){let r=n,o=t;f&&(r=r.toLowerCase(),o=o.toLowerCase());let l=r<o?-1:r>o?1:0;return e==="asc"?l:-l}if(n instanceof Date&&t instanceof Date){let r=n.getTime()-t.getTime();return e==="asc"?r:-r}let s=String(n),c=String(t),i=f?s.toLowerCase()<c.toLowerCase()?-1:s.toLowerCase()>c.toLowerCase()?1:0:s<c?-1:s>c?1:0;return e==="asc"?i:-i}function T(n,t,e,f,s){if(n.length<=s)return n.sort((r,o)=>u(t(r),t(o),e,f));let c=[];for(let r=0;r<n.length;r+=s)c.push(n.slice(r,r+s));let i=c.map(r=>r.sort((o,l)=>u(t(o),t(l),e,f)));return h(i,t,e,f)}function h(n,t,e,f){if(n.length===0)return [];if(n.length===1)return n[0];let s=[],c=new Array(n.length).fill(0);for(;;){let i=-1,r=null;for(let o=0;o<n.length;o++)if(c[o]<n[o].length){let l=n[o][c[o]];(r===null||u(t(l),t(r),e,f)<0)&&(r=l,i=o);}if(i===-1)break;s.push(r),c[i]++;}return s}exports.sortBy=S;
|