@zairakai/js-utils 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/LICENSE +21 -0
- package/README.md +270 -0
- package/dist/arrays.cjs +210 -0
- package/dist/arrays.d.cts +119 -0
- package/dist/arrays.d.ts +119 -0
- package/dist/arrays.js +32 -0
- package/dist/chunk-27YHP2CK.js +407 -0
- package/dist/chunk-3WNRYKPG.js +37 -0
- package/dist/chunk-42CHLXT7.js +214 -0
- package/dist/chunk-6F4PWJZI.js +0 -0
- package/dist/chunk-7SXRFZBB.js +173 -0
- package/dist/chunk-F6RSTW65.js +156 -0
- package/dist/chunk-G7ZJ23DW.js +253 -0
- package/dist/chunk-IPP7PA6H.js +136 -0
- package/dist/chunk-LDSWHSRX.js +96 -0
- package/dist/chunk-TY75OOIQ.js +700 -0
- package/dist/chunk-W6JEMFAF.js +54 -0
- package/dist/chunk-XEJLBAXE.js +164 -0
- package/dist/chunk-Z7G3SIQH.js +270 -0
- package/dist/chunk-ZJPKS2MQ.js +101 -0
- package/dist/collections.cjs +797 -0
- package/dist/collections.d.cts +353 -0
- package/dist/collections.d.ts +353 -0
- package/dist/collections.js +17 -0
- package/dist/datetime.cjs +80 -0
- package/dist/datetime.d.cts +75 -0
- package/dist/datetime.d.ts +75 -0
- package/dist/datetime.js +24 -0
- package/dist/equals.cjs +121 -0
- package/dist/equals.d.cts +24 -0
- package/dist/equals.d.ts +24 -0
- package/dist/equals.js +8 -0
- package/dist/formatters.cjs +201 -0
- package/dist/formatters.d.cts +180 -0
- package/dist/formatters.d.ts +180 -0
- package/dist/formatters.js +48 -0
- package/dist/index.cjs +2906 -0
- package/dist/index.d.cts +120 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +348 -0
- package/dist/number.cjs +279 -0
- package/dist/number.d.cts +177 -0
- package/dist/number.d.ts +177 -0
- package/dist/number.js +10 -0
- package/dist/obj.cjs +427 -0
- package/dist/obj.d.cts +177 -0
- package/dist/obj.d.ts +177 -0
- package/dist/obj.js +12 -0
- package/dist/php-arrays.cjs +954 -0
- package/dist/php-arrays.d.cts +256 -0
- package/dist/php-arrays.d.ts +256 -0
- package/dist/php-arrays.js +70 -0
- package/dist/runtime.cjs +134 -0
- package/dist/runtime.d.cts +90 -0
- package/dist/runtime.d.ts +90 -0
- package/dist/runtime.js +24 -0
- package/dist/schemas.cjs +86 -0
- package/dist/schemas.d.cts +108 -0
- package/dist/schemas.d.ts +108 -0
- package/dist/schemas.js +22 -0
- package/dist/str.cjs +499 -0
- package/dist/str.d.cts +282 -0
- package/dist/str.d.ts +282 -0
- package/dist/str.js +11 -0
- package/dist/types.cjs +18 -0
- package/dist/types.d.cts +13 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/validator.cjs +251 -0
- package/dist/validator.d.cts +99 -0
- package/dist/validator.d.ts +99 -0
- package/dist/validator.js +11 -0
- package/dist/validators.cjs +217 -0
- package/dist/validators.d.cts +216 -0
- package/dist/validators.d.ts +216 -0
- package/dist/validators.js +64 -0
- package/package.json +180 -0
- package/src/arrays.ts +316 -0
- package/src/collections.ts +866 -0
- package/src/datetime.ts +103 -0
- package/src/equals.ts +134 -0
- package/src/formatters.ts +342 -0
- package/src/index.ts +36 -0
- package/src/number.ts +281 -0
- package/src/obj.ts +303 -0
- package/src/php-arrays.ts +445 -0
- package/src/pipe.ts +29 -0
- package/src/runtime.ts +194 -0
- package/src/schemas.ts +136 -0
- package/src/str.ts +438 -0
- package/src/types.ts +13 -0
- package/src/validator.ts +157 -0
- package/src/validators.ts +359 -0
|
@@ -0,0 +1,954 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/php-arrays.ts
|
|
21
|
+
var php_arrays_exports = {};
|
|
22
|
+
__export(php_arrays_exports, {
|
|
23
|
+
array_chunk: () => array_chunk,
|
|
24
|
+
array_column: () => array_column,
|
|
25
|
+
array_count_values: () => array_count_values,
|
|
26
|
+
array_diff: () => array_diff,
|
|
27
|
+
array_filter: () => array_filter,
|
|
28
|
+
array_flip: () => array_flip,
|
|
29
|
+
array_intersect: () => array_intersect,
|
|
30
|
+
array_key_exists: () => array_key_exists,
|
|
31
|
+
array_keys: () => array_keys,
|
|
32
|
+
array_map: () => array_map,
|
|
33
|
+
array_merge: () => array_merge,
|
|
34
|
+
array_pop: () => array_pop,
|
|
35
|
+
array_product: () => array_product,
|
|
36
|
+
array_push: () => array_push,
|
|
37
|
+
array_rand: () => array_rand,
|
|
38
|
+
array_reduce: () => array_reduce,
|
|
39
|
+
array_reverse: () => array_reverse,
|
|
40
|
+
array_search: () => array_search,
|
|
41
|
+
array_shift: () => array_shift,
|
|
42
|
+
array_slice: () => array_slice,
|
|
43
|
+
array_splice: () => array_splice,
|
|
44
|
+
array_sum: () => array_sum,
|
|
45
|
+
array_unique: () => array_unique,
|
|
46
|
+
array_unshift: () => array_unshift,
|
|
47
|
+
php_array: () => php_array,
|
|
48
|
+
range: () => range,
|
|
49
|
+
rsort: () => rsort,
|
|
50
|
+
shuffle: () => shuffle,
|
|
51
|
+
sort: () => sort,
|
|
52
|
+
uasort: () => uasort,
|
|
53
|
+
uksort: () => uksort,
|
|
54
|
+
usort: () => usort
|
|
55
|
+
});
|
|
56
|
+
module.exports = __toCommonJS(php_arrays_exports);
|
|
57
|
+
|
|
58
|
+
// src/equals.ts
|
|
59
|
+
var isEqual = (a, b) => {
|
|
60
|
+
if (a === b) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
if (a && b && "object" === typeof a && "object" === typeof b) {
|
|
64
|
+
if (a.constructor !== b.constructor) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
if (Array.isArray(a)) {
|
|
68
|
+
if (a.length !== b.length) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
for (let i = 0; i < a.length; i++) {
|
|
72
|
+
if (!isEqual(a[i], b[i])) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
if (a instanceof Map && b instanceof Map) {
|
|
79
|
+
if (a.size !== b.size) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
for (const [key, val] of a) {
|
|
83
|
+
if (!b.has(key) || !isEqual(val, b.get(key))) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
if (a instanceof Set && b instanceof Set) {
|
|
90
|
+
if (a.size !== b.size) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
for (const val of a) {
|
|
94
|
+
if (!b.has(val)) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
if (a.constructor === RegExp) {
|
|
101
|
+
return a.source === b.source && a.flags === b.flags;
|
|
102
|
+
}
|
|
103
|
+
if (a.valueOf !== Object.prototype.valueOf) {
|
|
104
|
+
return a.valueOf() === b.valueOf();
|
|
105
|
+
}
|
|
106
|
+
if (a.toString !== Object.prototype.toString) {
|
|
107
|
+
return a.toString() === b.toString();
|
|
108
|
+
}
|
|
109
|
+
const keys = Object.keys(a);
|
|
110
|
+
if (keys.length !== Object.keys(b).length) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const bObj = b;
|
|
114
|
+
const aObj = a;
|
|
115
|
+
for (const key of keys) {
|
|
116
|
+
if (!Object.prototype.hasOwnProperty.call(bObj, key)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
if (!isEqual(aObj[key], bObj[key])) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
return a !== a && b !== b;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// src/collections.ts
|
|
129
|
+
var EnhancedArray = class _EnhancedArray extends Array {
|
|
130
|
+
/**
|
|
131
|
+
* Create a new EnhancedArray instance.
|
|
132
|
+
*
|
|
133
|
+
* @param {...T[]} items The items to initialize the collection with
|
|
134
|
+
*/
|
|
135
|
+
constructor(...items) {
|
|
136
|
+
super(...items);
|
|
137
|
+
Object.setPrototypeOf(this, _EnhancedArray.prototype);
|
|
138
|
+
this.originalItems = structuredClone(items);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Determine if the collection has been modified since initialization or last sync.
|
|
142
|
+
*
|
|
143
|
+
* @returns {boolean} True if modified, false otherwise
|
|
144
|
+
*/
|
|
145
|
+
isDirty() {
|
|
146
|
+
return !isEqual(this.originalItems, Array.from(this));
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Determine if the collection is clean (not modified).
|
|
150
|
+
*
|
|
151
|
+
* @returns {boolean} True if not modified, false otherwise
|
|
152
|
+
*/
|
|
153
|
+
isClean() {
|
|
154
|
+
return !this.isDirty();
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Sync the original items with current items to mark the collection as clean.
|
|
158
|
+
*
|
|
159
|
+
* @returns {this} The collection instance
|
|
160
|
+
*/
|
|
161
|
+
syncOriginal() {
|
|
162
|
+
this.originalItems = structuredClone(Array.from(this));
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Compare with another array or collection for equality.
|
|
167
|
+
*
|
|
168
|
+
* @param {T[] | EnhancedArray<T>} other The array or collection to compare with
|
|
169
|
+
* @returns {boolean} True if equivalent, false otherwise
|
|
170
|
+
*/
|
|
171
|
+
isEquivalent(other) {
|
|
172
|
+
return isEqual(Array.from(this), Array.from(other));
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Create an EnhancedArray from an iterable or array-like object.
|
|
176
|
+
*
|
|
177
|
+
* @param {Iterable<T> | ArrayLike<T>} items The items to convert
|
|
178
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
179
|
+
*/
|
|
180
|
+
static from(items) {
|
|
181
|
+
return new _EnhancedArray(...Array.from(items));
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Create an EnhancedArray from a variable number of arguments.
|
|
185
|
+
*
|
|
186
|
+
* @param {...T[]} items The items to include
|
|
187
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
188
|
+
*/
|
|
189
|
+
static of(...items) {
|
|
190
|
+
return new _EnhancedArray(...items);
|
|
191
|
+
}
|
|
192
|
+
// Laravel Collection inspired methods
|
|
193
|
+
/**
|
|
194
|
+
* Create a new collection instance from the current items.
|
|
195
|
+
*
|
|
196
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
197
|
+
*/
|
|
198
|
+
collect() {
|
|
199
|
+
return new _EnhancedArray(...this);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Pluck an array of values from an array of objects.
|
|
203
|
+
*
|
|
204
|
+
* @param {K} key The key to pluck
|
|
205
|
+
* @returns {EnhancedArray<T[K]>} A new collection of plucked values
|
|
206
|
+
*/
|
|
207
|
+
pluck(key) {
|
|
208
|
+
return new _EnhancedArray(
|
|
209
|
+
...this.map((item) => item && "object" === typeof item ? item[key] : void 0).filter(
|
|
210
|
+
(val) => val !== void 0
|
|
211
|
+
)
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Group the collection's items by a given key or callback.
|
|
216
|
+
*
|
|
217
|
+
* @param {K | Callback<T, string | number>} key The key or callback to group by
|
|
218
|
+
* @returns {Record<string, EnhancedArray<T>>} A record of grouped collections
|
|
219
|
+
*/
|
|
220
|
+
groupBy(key) {
|
|
221
|
+
const groups = {};
|
|
222
|
+
for (let i = 0; i < this.length; i++) {
|
|
223
|
+
const item = this[i];
|
|
224
|
+
const groupKey = "function" === typeof key ? String(key(item, i)) : String(item && "object" === typeof item ? item[key] : item);
|
|
225
|
+
if (!groups[groupKey]) {
|
|
226
|
+
groups[groupKey] = new _EnhancedArray();
|
|
227
|
+
}
|
|
228
|
+
groups[groupKey].push(item);
|
|
229
|
+
}
|
|
230
|
+
return groups;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Return only unique items from the collection.
|
|
234
|
+
*
|
|
235
|
+
* @param {K} [key] Optional key to determine uniqueness for objects
|
|
236
|
+
* @returns {EnhancedArray<T>} A new collection of unique items
|
|
237
|
+
*/
|
|
238
|
+
unique(key) {
|
|
239
|
+
if (!key) {
|
|
240
|
+
return new _EnhancedArray(...new Set(this));
|
|
241
|
+
}
|
|
242
|
+
const seen = /* @__PURE__ */ new Set();
|
|
243
|
+
return new _EnhancedArray(
|
|
244
|
+
...this.filter((item) => {
|
|
245
|
+
const value = item && "object" === typeof item ? item[key] : item;
|
|
246
|
+
if (seen.has(value)) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
seen.add(value);
|
|
250
|
+
return true;
|
|
251
|
+
})
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Chunk the collection into smaller collections of a given size.
|
|
256
|
+
*
|
|
257
|
+
* @param {number} size The size of each chunk
|
|
258
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of chunked collections
|
|
259
|
+
*/
|
|
260
|
+
chunk(size) {
|
|
261
|
+
const chunks = new _EnhancedArray();
|
|
262
|
+
for (let i = 0; i < this.length; i += size) {
|
|
263
|
+
chunks.push(new _EnhancedArray(...this.slice(i, i + size)));
|
|
264
|
+
}
|
|
265
|
+
return chunks;
|
|
266
|
+
}
|
|
267
|
+
// Advanced manipulation methods
|
|
268
|
+
/**
|
|
269
|
+
* Flatten a multi-dimensional collection into a single level.
|
|
270
|
+
*
|
|
271
|
+
* @param {number} [depth=Infinity] The depth to flatten to
|
|
272
|
+
* @returns {EnhancedArray<unknown>} A new flattened collection
|
|
273
|
+
*/
|
|
274
|
+
deepFlatten(depth = Infinity) {
|
|
275
|
+
const flatten = (arr, currentDepth) => {
|
|
276
|
+
const result = [];
|
|
277
|
+
for (const item of arr) {
|
|
278
|
+
if (Array.isArray(item) && 0 < currentDepth) {
|
|
279
|
+
result.push(...flatten(item, currentDepth - 1));
|
|
280
|
+
} else {
|
|
281
|
+
result.push(item);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return result;
|
|
285
|
+
};
|
|
286
|
+
return new _EnhancedArray(...flatten(this, depth));
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Rotate the collection by a given number of positions.
|
|
290
|
+
*
|
|
291
|
+
* @param {number} [positions=1] The number of positions to rotate
|
|
292
|
+
* @returns {EnhancedArray<T>} A new rotated collection
|
|
293
|
+
*/
|
|
294
|
+
rotate(positions = 1) {
|
|
295
|
+
if (0 === this.length) {
|
|
296
|
+
return new _EnhancedArray();
|
|
297
|
+
}
|
|
298
|
+
const count = this.length;
|
|
299
|
+
positions = positions % count;
|
|
300
|
+
if (0 === positions) {
|
|
301
|
+
return this.collect();
|
|
302
|
+
}
|
|
303
|
+
if (0 > positions) {
|
|
304
|
+
positions = count + positions;
|
|
305
|
+
}
|
|
306
|
+
return new _EnhancedArray(...this.slice(positions), ...this.slice(0, positions));
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Chunk the collection as long as the given predicate returns true.
|
|
310
|
+
*
|
|
311
|
+
* @param {Predicate<T>} predicate The predicate to check
|
|
312
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of chunked collections
|
|
313
|
+
*/
|
|
314
|
+
chunkWhile(predicate) {
|
|
315
|
+
const chunks = new _EnhancedArray();
|
|
316
|
+
let currentChunk = new _EnhancedArray();
|
|
317
|
+
for (let i = 0; i < this.length; i++) {
|
|
318
|
+
const item = this[i];
|
|
319
|
+
if (0 < currentChunk.length && !predicate(item, i)) {
|
|
320
|
+
chunks.push(currentChunk);
|
|
321
|
+
currentChunk = new _EnhancedArray();
|
|
322
|
+
}
|
|
323
|
+
currentChunk.push(item);
|
|
324
|
+
}
|
|
325
|
+
if (0 < currentChunk.length) {
|
|
326
|
+
chunks.push(currentChunk);
|
|
327
|
+
}
|
|
328
|
+
return chunks;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Transpose the collection (swap rows and columns).
|
|
332
|
+
*
|
|
333
|
+
* @returns {EnhancedArray<EnhancedArray<unknown>>} A new transposed collection
|
|
334
|
+
*/
|
|
335
|
+
transpose() {
|
|
336
|
+
if (0 === this.length) {
|
|
337
|
+
return new _EnhancedArray();
|
|
338
|
+
}
|
|
339
|
+
const maxLength = Math.max(...this.map((row) => Array.isArray(row) ? row.length : 1));
|
|
340
|
+
const result = new _EnhancedArray();
|
|
341
|
+
for (let i = 0; i < maxLength; i++) {
|
|
342
|
+
const column = new _EnhancedArray(
|
|
343
|
+
...this.map((row) => {
|
|
344
|
+
if (Array.isArray(row)) {
|
|
345
|
+
return row[i] ?? null;
|
|
346
|
+
}
|
|
347
|
+
return 0 === i ? row : null;
|
|
348
|
+
})
|
|
349
|
+
);
|
|
350
|
+
result.push(column);
|
|
351
|
+
}
|
|
352
|
+
return result;
|
|
353
|
+
}
|
|
354
|
+
// Validation methods
|
|
355
|
+
/**
|
|
356
|
+
* Check if the collection is sorted.
|
|
357
|
+
*
|
|
358
|
+
* @param {Function} [compareFn] Optional comparison function
|
|
359
|
+
* @returns {boolean} True if sorted, false otherwise
|
|
360
|
+
*/
|
|
361
|
+
isSorted(compareFn) {
|
|
362
|
+
if (1 >= this.length) {
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
for (let i = 1; i < this.length; i++) {
|
|
366
|
+
const prev = this[i - 1];
|
|
367
|
+
const current = this[i];
|
|
368
|
+
if (compareFn) {
|
|
369
|
+
if (0 < compareFn(prev, current)) {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
if (prev > current) {
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Check if the collection contains duplicate items.
|
|
382
|
+
*
|
|
383
|
+
* @param {K} [key] Optional key to check for uniqueness in objects
|
|
384
|
+
* @returns {boolean} True if duplicates found, false otherwise
|
|
385
|
+
*/
|
|
386
|
+
hasDuplicates(key) {
|
|
387
|
+
if (key) {
|
|
388
|
+
const seen = /* @__PURE__ */ new Set();
|
|
389
|
+
for (const item of this) {
|
|
390
|
+
const value = item && "object" === typeof item ? item[key] : item;
|
|
391
|
+
if (seen.has(value)) {
|
|
392
|
+
return true;
|
|
393
|
+
}
|
|
394
|
+
seen.add(value);
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
return this.length !== new Set(this).size;
|
|
399
|
+
}
|
|
400
|
+
// Statistical methods
|
|
401
|
+
/**
|
|
402
|
+
* Calculate the median value of the collection.
|
|
403
|
+
*
|
|
404
|
+
* @param {Callback<T, number>} [accessor] Optional accessor for non-numeric items
|
|
405
|
+
* @returns {number | null} The median value or null if collection is empty
|
|
406
|
+
*/
|
|
407
|
+
median(accessor) {
|
|
408
|
+
if (0 === this.length) {
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
const values = accessor ? this.map(accessor) : this;
|
|
412
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
413
|
+
const count = sorted.length;
|
|
414
|
+
if (0 === count % 2) {
|
|
415
|
+
return (sorted[count / 2 - 1] + sorted[count / 2]) / 2;
|
|
416
|
+
}
|
|
417
|
+
return sorted[Math.floor(count / 2)];
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Calculate the mode (most frequent values) of the collection.
|
|
421
|
+
*
|
|
422
|
+
* @param {Callback<T, unknown>} [accessor] Optional accessor for the values
|
|
423
|
+
* @returns {EnhancedArray<unknown>} A collection of mode values
|
|
424
|
+
*/
|
|
425
|
+
mode(accessor) {
|
|
426
|
+
if (0 === this.length) {
|
|
427
|
+
return new _EnhancedArray();
|
|
428
|
+
}
|
|
429
|
+
const frequencies = this.frequencies(accessor);
|
|
430
|
+
const maxFrequency = Math.max(...Object.values(frequencies));
|
|
431
|
+
return new _EnhancedArray(...Object.keys(frequencies).filter((key) => frequencies[key] === maxFrequency));
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Calculate the standard deviation of the collection.
|
|
435
|
+
*
|
|
436
|
+
* @param {Callback<T, number>} [accessor] Optional accessor for non-numeric items
|
|
437
|
+
* @returns {number} The standard deviation
|
|
438
|
+
*/
|
|
439
|
+
standardDeviation(accessor) {
|
|
440
|
+
if (0 === this.length) {
|
|
441
|
+
return 0;
|
|
442
|
+
}
|
|
443
|
+
const values = accessor ? this.map(accessor) : this;
|
|
444
|
+
const mean = values.reduce((sum, val) => sum + val, 0) / values.length;
|
|
445
|
+
const sumSquaredDifferences = values.reduce(
|
|
446
|
+
(sum, val) => sum + Math.pow(val - mean, 2),
|
|
447
|
+
0
|
|
448
|
+
);
|
|
449
|
+
return Math.sqrt(sumSquaredDifferences / values.length);
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Calculate a specific percentile of the collection.
|
|
453
|
+
*
|
|
454
|
+
* @param {number} percentile The percentile to calculate (0-100)
|
|
455
|
+
* @param {Callback<T, number>} [accessor] Optional accessor for non-numeric items
|
|
456
|
+
* @returns {number | null} The percentile value or null if collection is empty
|
|
457
|
+
*/
|
|
458
|
+
percentile(percentile, accessor) {
|
|
459
|
+
if (0 === this.length) {
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
const values = accessor ? this.map(accessor) : this;
|
|
463
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
464
|
+
const index = percentile / 100 * (sorted.length - 1);
|
|
465
|
+
const lower = Math.floor(index);
|
|
466
|
+
const upper = Math.ceil(index);
|
|
467
|
+
if (lower === upper) {
|
|
468
|
+
return sorted[lower];
|
|
469
|
+
}
|
|
470
|
+
const weight = index - lower;
|
|
471
|
+
return sorted[lower] * (1 - weight) + sorted[upper] * weight;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Calculate the frequencies of items in the collection.
|
|
475
|
+
*
|
|
476
|
+
* @param {Callback<T, unknown>} [accessor] Optional accessor for the values
|
|
477
|
+
* @returns {Record<string, number>} A record of values and their frequencies
|
|
478
|
+
*/
|
|
479
|
+
frequencies(accessor) {
|
|
480
|
+
const frequencies = {};
|
|
481
|
+
for (let i = 0; i < this.length; i++) {
|
|
482
|
+
const item = this[i];
|
|
483
|
+
const key = accessor ? String(accessor(item, i)) : String(item);
|
|
484
|
+
frequencies[key] = (frequencies[key] || 0) + 1;
|
|
485
|
+
}
|
|
486
|
+
return frequencies;
|
|
487
|
+
}
|
|
488
|
+
// Advanced operations
|
|
489
|
+
/**
|
|
490
|
+
* Calculate the Cartesian product of the collection and other arrays.
|
|
491
|
+
*
|
|
492
|
+
* @param {...U[][]} arrays The arrays to calculate the product with
|
|
493
|
+
* @returns {EnhancedArray<[T, ...U[]]>} A collection of product combinations
|
|
494
|
+
*/
|
|
495
|
+
cartesian(...arrays) {
|
|
496
|
+
const result = new _EnhancedArray();
|
|
497
|
+
const allArrays = [this, ...arrays];
|
|
498
|
+
function* cartesianProduct(arrays2, index = 0, current = []) {
|
|
499
|
+
if (index === arrays2.length) {
|
|
500
|
+
yield [...current];
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
for (const item of arrays2[index]) {
|
|
504
|
+
yield* cartesianProduct(arrays2, index + 1, [...current, item]);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
for (const combination of cartesianProduct(allArrays)) {
|
|
508
|
+
result.push(combination);
|
|
509
|
+
}
|
|
510
|
+
return result;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Interleave the collection with other arrays.
|
|
514
|
+
*
|
|
515
|
+
* @param {...U[][]} arrays The arrays to interleave with
|
|
516
|
+
* @returns {EnhancedArray<T | U>} A new interleaved collection
|
|
517
|
+
*/
|
|
518
|
+
interleave(...arrays) {
|
|
519
|
+
const result = new _EnhancedArray();
|
|
520
|
+
const allArrays = [this, ...arrays];
|
|
521
|
+
const maxLength = Math.max(...allArrays.map((arr) => arr.length));
|
|
522
|
+
for (let i = 0; i < maxLength; i++) {
|
|
523
|
+
for (const array of allArrays) {
|
|
524
|
+
if (i < array.length) {
|
|
525
|
+
result.push(array[i]);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return result;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Get a sliding window of items from the collection.
|
|
533
|
+
*
|
|
534
|
+
* @param {number} [size=2] The size of the window
|
|
535
|
+
* @param {number} [step=1] The step between windows
|
|
536
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of window collections
|
|
537
|
+
*/
|
|
538
|
+
sliding(size = 2, step = 1) {
|
|
539
|
+
if (0 >= size || 0 >= step) {
|
|
540
|
+
return new _EnhancedArray();
|
|
541
|
+
}
|
|
542
|
+
const windows = new _EnhancedArray();
|
|
543
|
+
for (let i = 0; i <= this.length - size; i += step) {
|
|
544
|
+
windows.push(new _EnhancedArray(...this.slice(i, i + size)));
|
|
545
|
+
}
|
|
546
|
+
return windows;
|
|
547
|
+
}
|
|
548
|
+
// Navigation macros
|
|
549
|
+
/**
|
|
550
|
+
* Get the item at the given index.
|
|
551
|
+
*
|
|
552
|
+
* @param {number} index The index to retrieve
|
|
553
|
+
* @returns {T | undefined} The item at the index or undefined
|
|
554
|
+
*/
|
|
555
|
+
at(index) {
|
|
556
|
+
return this[index];
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Get the item before the given item.
|
|
560
|
+
*
|
|
561
|
+
* @param {T} item The item to search for
|
|
562
|
+
* @returns {T | undefined} The item before or undefined
|
|
563
|
+
*/
|
|
564
|
+
before(item) {
|
|
565
|
+
const index = this.indexOf(item);
|
|
566
|
+
return 0 < index ? this[index - 1] : void 0;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Get the item after the given item.
|
|
570
|
+
*
|
|
571
|
+
* @param {T} item The item to search for
|
|
572
|
+
* @returns {T | undefined} The item after or undefined
|
|
573
|
+
*/
|
|
574
|
+
after(item) {
|
|
575
|
+
const index = this.indexOf(item);
|
|
576
|
+
return 0 <= index && index < this.length - 1 ? this[index + 1] : void 0;
|
|
577
|
+
}
|
|
578
|
+
// Transformation macros
|
|
579
|
+
/**
|
|
580
|
+
* Chunk the collection by a given key or callback when its value changes.
|
|
581
|
+
*
|
|
582
|
+
* @param {K | Callback<T, unknown>} key The key or callback to chunk by
|
|
583
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of chunked collections
|
|
584
|
+
*/
|
|
585
|
+
chunkBy(key) {
|
|
586
|
+
if (0 === this.length) {
|
|
587
|
+
return new _EnhancedArray();
|
|
588
|
+
}
|
|
589
|
+
const chunks = new _EnhancedArray();
|
|
590
|
+
let currentChunk = new _EnhancedArray();
|
|
591
|
+
let lastValue = null;
|
|
592
|
+
for (let i = 0; i < this.length; i++) {
|
|
593
|
+
const item = this[i];
|
|
594
|
+
const currentValue = "function" === typeof key ? key(item, i) : item && "object" === typeof item ? item[key] : item;
|
|
595
|
+
if (0 === i || currentValue === lastValue) {
|
|
596
|
+
currentChunk.push(item);
|
|
597
|
+
} else {
|
|
598
|
+
chunks.push(currentChunk);
|
|
599
|
+
currentChunk = new _EnhancedArray(item);
|
|
600
|
+
}
|
|
601
|
+
lastValue = currentValue;
|
|
602
|
+
}
|
|
603
|
+
if (0 < currentChunk.length) {
|
|
604
|
+
chunks.push(currentChunk);
|
|
605
|
+
}
|
|
606
|
+
return chunks;
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Map and filter the collection simultaneously.
|
|
610
|
+
* Items for which the callback returns null or undefined are removed.
|
|
611
|
+
*
|
|
612
|
+
* @param {Function} callback The callback to map items
|
|
613
|
+
* @returns {EnhancedArray<U>} A new mapped and filtered collection
|
|
614
|
+
*/
|
|
615
|
+
filterMap(callback) {
|
|
616
|
+
const result = new _EnhancedArray();
|
|
617
|
+
for (let i = 0; i < this.length; i++) {
|
|
618
|
+
const mapped = callback(this[i], i);
|
|
619
|
+
if (null !== mapped && mapped !== void 0) {
|
|
620
|
+
result.push(mapped);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return result;
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Extract a subset of properties from each item in the collection.
|
|
627
|
+
*
|
|
628
|
+
* @param {K[]} keys The keys to extract
|
|
629
|
+
* @returns {EnhancedArray<Partial<T>>} A new collection of partial items
|
|
630
|
+
*/
|
|
631
|
+
extract(keys) {
|
|
632
|
+
return new _EnhancedArray(
|
|
633
|
+
...this.map((item) => {
|
|
634
|
+
if (!item || "object" !== typeof item) {
|
|
635
|
+
return {};
|
|
636
|
+
}
|
|
637
|
+
const extracted = {};
|
|
638
|
+
for (const key of keys) {
|
|
639
|
+
if (key in item) {
|
|
640
|
+
extracted[key] = item[key];
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
return extracted;
|
|
644
|
+
})
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
// Utility macros
|
|
648
|
+
/**
|
|
649
|
+
* Get the first item or push a default item if the collection is empty.
|
|
650
|
+
*
|
|
651
|
+
* @param {T} item The default item to push if empty
|
|
652
|
+
* @returns {T} The first item or the pushed item
|
|
653
|
+
*/
|
|
654
|
+
firstOrPush(item) {
|
|
655
|
+
if (0 === this.length) {
|
|
656
|
+
this.push(item);
|
|
657
|
+
return item;
|
|
658
|
+
}
|
|
659
|
+
return this[0];
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Get every n-th item from the collection.
|
|
663
|
+
*
|
|
664
|
+
* @param {number} n The step size
|
|
665
|
+
* @returns {EnhancedArray<T>} A new collection of every n-th item
|
|
666
|
+
*/
|
|
667
|
+
getNth(n) {
|
|
668
|
+
const result = new _EnhancedArray();
|
|
669
|
+
for (let i = n - 1; i < this.length; i += n) {
|
|
670
|
+
result.push(this[i]);
|
|
671
|
+
}
|
|
672
|
+
return result;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Get a random item from the collection based on weights.
|
|
676
|
+
*
|
|
677
|
+
* @param {number[]} [weights] Optional weights for each item
|
|
678
|
+
* @returns {T | undefined} A random item or undefined if collection is empty
|
|
679
|
+
*/
|
|
680
|
+
weightedRandom(weights) {
|
|
681
|
+
if (0 === this.length) {
|
|
682
|
+
return void 0;
|
|
683
|
+
}
|
|
684
|
+
const effectiveWeights = weights ?? new Array(this.length).fill(1);
|
|
685
|
+
if (effectiveWeights.length !== this.length) {
|
|
686
|
+
throw new Error("Weights array must have same length as collection");
|
|
687
|
+
}
|
|
688
|
+
const totalWeight = effectiveWeights.reduce((sum, weight) => sum + weight, 0);
|
|
689
|
+
const random = Math.random() * totalWeight;
|
|
690
|
+
let currentWeight = 0;
|
|
691
|
+
for (let i = 0; i < this.length; i++) {
|
|
692
|
+
currentWeight += effectiveWeights[i];
|
|
693
|
+
if (random <= currentWeight) {
|
|
694
|
+
return this[i];
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
return this[this.length - 1];
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Paginate the collection items.
|
|
701
|
+
*
|
|
702
|
+
* @param {number} perPage Items per page
|
|
703
|
+
* @param {number} [page=1] Current page number
|
|
704
|
+
* @returns {Object} Pagination results
|
|
705
|
+
*/
|
|
706
|
+
paginate(perPage, page = 1) {
|
|
707
|
+
const total = this.length;
|
|
708
|
+
const lastPage = Math.ceil(total / perPage);
|
|
709
|
+
const currentPage = Math.max(1, Math.min(page, lastPage));
|
|
710
|
+
const from = (currentPage - 1) * perPage;
|
|
711
|
+
const to = Math.min(from + perPage, total);
|
|
712
|
+
return {
|
|
713
|
+
data: new _EnhancedArray(...this.slice(from, to)),
|
|
714
|
+
total,
|
|
715
|
+
perPage,
|
|
716
|
+
currentPage,
|
|
717
|
+
lastPage,
|
|
718
|
+
from: from + 1,
|
|
719
|
+
to
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Recursively convert all nested arrays to EnhancedArray instances.
|
|
724
|
+
*
|
|
725
|
+
* @returns {EnhancedArray<unknown>} A new collection with recursive EnhancedArrays
|
|
726
|
+
*/
|
|
727
|
+
recursive() {
|
|
728
|
+
const convert = (item) => {
|
|
729
|
+
if (Array.isArray(item)) {
|
|
730
|
+
return new _EnhancedArray(...item.map(convert));
|
|
731
|
+
}
|
|
732
|
+
if (item && "object" === typeof item && item.constructor === Object) {
|
|
733
|
+
const converted = {};
|
|
734
|
+
for (const [key, value] of Object.entries(item)) {
|
|
735
|
+
converted[key] = convert(value);
|
|
736
|
+
}
|
|
737
|
+
return converted;
|
|
738
|
+
}
|
|
739
|
+
return item;
|
|
740
|
+
};
|
|
741
|
+
return new _EnhancedArray(...this.map(convert));
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
// src/php-arrays.ts
|
|
746
|
+
var array_chunk = (array, size) => {
|
|
747
|
+
if (0 >= size) {
|
|
748
|
+
return [];
|
|
749
|
+
}
|
|
750
|
+
const chunks = [];
|
|
751
|
+
for (let i = 0; i < array.length; i += size) {
|
|
752
|
+
chunks.push(array.slice(i, i + size));
|
|
753
|
+
}
|
|
754
|
+
return chunks;
|
|
755
|
+
};
|
|
756
|
+
var array_filter = (array, callback) => {
|
|
757
|
+
if (!callback) {
|
|
758
|
+
return array.filter((item) => Boolean(item));
|
|
759
|
+
}
|
|
760
|
+
return array.filter(callback);
|
|
761
|
+
};
|
|
762
|
+
var array_map = (callback, array) => {
|
|
763
|
+
return array.map(callback);
|
|
764
|
+
};
|
|
765
|
+
var array_reduce = (array, callback, initial) => {
|
|
766
|
+
return array.reduce((carry, item, index) => callback(carry, item, index), initial);
|
|
767
|
+
};
|
|
768
|
+
var array_merge = (...arrays) => {
|
|
769
|
+
return arrays.flat();
|
|
770
|
+
};
|
|
771
|
+
var array_unique = (array) => {
|
|
772
|
+
return [...new Set(array)];
|
|
773
|
+
};
|
|
774
|
+
var array_reverse = (array, _preserveKeys = false) => {
|
|
775
|
+
return [...array].reverse();
|
|
776
|
+
};
|
|
777
|
+
var array_slice = (array, offset, length) => {
|
|
778
|
+
const start = 0 > offset ? Math.max(0, array.length + offset) : offset;
|
|
779
|
+
const end = length !== void 0 ? start + length : void 0;
|
|
780
|
+
return array.slice(start, end);
|
|
781
|
+
};
|
|
782
|
+
var array_splice = (array, offset, length, ...replacement) => {
|
|
783
|
+
const result = [...array];
|
|
784
|
+
const actualLength = length ?? result.length - offset;
|
|
785
|
+
result.splice(offset, actualLength, ...replacement);
|
|
786
|
+
return result;
|
|
787
|
+
};
|
|
788
|
+
var array_keys = (array) => {
|
|
789
|
+
return array.map((_, index) => index);
|
|
790
|
+
};
|
|
791
|
+
var array_search = (needle, haystack) => {
|
|
792
|
+
const index = haystack.indexOf(needle);
|
|
793
|
+
return -1 !== index ? index : false;
|
|
794
|
+
};
|
|
795
|
+
var array_key_exists = (key, array) => {
|
|
796
|
+
return 0 <= key && key < array.length;
|
|
797
|
+
};
|
|
798
|
+
var array_pop = (array) => {
|
|
799
|
+
const result = [...array];
|
|
800
|
+
return result.pop();
|
|
801
|
+
};
|
|
802
|
+
var array_push = (array, ...values) => {
|
|
803
|
+
return [...array, ...values];
|
|
804
|
+
};
|
|
805
|
+
var array_shift = (array) => {
|
|
806
|
+
const result = [...array];
|
|
807
|
+
return result.shift();
|
|
808
|
+
};
|
|
809
|
+
var array_unshift = (array, ...values) => {
|
|
810
|
+
return [...values, ...array];
|
|
811
|
+
};
|
|
812
|
+
var array_sum = (array) => {
|
|
813
|
+
return array.reduce((sum, num) => sum + num, 0);
|
|
814
|
+
};
|
|
815
|
+
var array_product = (array) => {
|
|
816
|
+
return array.reduce((product, num) => product * num, 1);
|
|
817
|
+
};
|
|
818
|
+
var array_rand = (array, num = 1) => {
|
|
819
|
+
if (0 === array.length) {
|
|
820
|
+
return 1 === num ? void 0 : [];
|
|
821
|
+
}
|
|
822
|
+
const shuffled = [...array].sort(() => Math.random() - 0.5);
|
|
823
|
+
if (1 === num) {
|
|
824
|
+
return shuffled[0];
|
|
825
|
+
}
|
|
826
|
+
return shuffled.slice(0, Math.min(num, array.length));
|
|
827
|
+
};
|
|
828
|
+
var array_flip = (array) => {
|
|
829
|
+
const result = {};
|
|
830
|
+
array.forEach((value, index) => {
|
|
831
|
+
result[String(value)] = index;
|
|
832
|
+
});
|
|
833
|
+
return result;
|
|
834
|
+
};
|
|
835
|
+
var array_count_values = (array) => {
|
|
836
|
+
const result = {};
|
|
837
|
+
array.forEach((value) => {
|
|
838
|
+
const key = String(value);
|
|
839
|
+
result[key] = (result[key] || 0) + 1;
|
|
840
|
+
});
|
|
841
|
+
return result;
|
|
842
|
+
};
|
|
843
|
+
var array_intersect = (...arrays) => {
|
|
844
|
+
if (0 === arrays.length) {
|
|
845
|
+
return [];
|
|
846
|
+
}
|
|
847
|
+
const first = arrays[0];
|
|
848
|
+
return first.filter((item) => arrays.slice(1).every((arr) => arr.includes(item)));
|
|
849
|
+
};
|
|
850
|
+
var array_diff = (array1, ...arrays) => {
|
|
851
|
+
const otherItems = new Set(arrays.flat());
|
|
852
|
+
return array1.filter((item) => !otherItems.has(item));
|
|
853
|
+
};
|
|
854
|
+
var array_column = (array, column) => {
|
|
855
|
+
return array.map((item) => item && "object" === typeof item ? item[column] : void 0).filter((value) => value !== void 0);
|
|
856
|
+
};
|
|
857
|
+
var sort = (array) => {
|
|
858
|
+
return [...array].sort();
|
|
859
|
+
};
|
|
860
|
+
var rsort = (array) => {
|
|
861
|
+
return [...array].sort().reverse();
|
|
862
|
+
};
|
|
863
|
+
var usort = (array, compareFunction) => {
|
|
864
|
+
return [...array].sort(compareFunction);
|
|
865
|
+
};
|
|
866
|
+
var uasort = (array, compareFunction) => {
|
|
867
|
+
return [...array].sort(compareFunction);
|
|
868
|
+
};
|
|
869
|
+
var uksort = (array, compareFunction) => {
|
|
870
|
+
const indices = Array.from({ length: array.length }, (_, i) => i);
|
|
871
|
+
const sortedIndices = indices.sort(compareFunction);
|
|
872
|
+
return sortedIndices.map((i) => array[i]);
|
|
873
|
+
};
|
|
874
|
+
var shuffle = (array) => {
|
|
875
|
+
const result = [...array];
|
|
876
|
+
for (let i = result.length - 1; 0 < i; i--) {
|
|
877
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
878
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
879
|
+
}
|
|
880
|
+
return result;
|
|
881
|
+
};
|
|
882
|
+
var range = (start, end, step = 1) => {
|
|
883
|
+
const result = [];
|
|
884
|
+
if (0 < step) {
|
|
885
|
+
for (let i = start; i <= end; i += step) {
|
|
886
|
+
result.push(i);
|
|
887
|
+
}
|
|
888
|
+
} else if (0 > step) {
|
|
889
|
+
for (let i = start; i >= end; i += step) {
|
|
890
|
+
result.push(i);
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
return result;
|
|
894
|
+
};
|
|
895
|
+
var php_array = (items) => {
|
|
896
|
+
const enhanced = new EnhancedArray(...items);
|
|
897
|
+
Object.assign(enhanced, {
|
|
898
|
+
// PHP array functions as methods
|
|
899
|
+
chunk: (size) => array_chunk(enhanced, size),
|
|
900
|
+
merge: (...arrays) => new EnhancedArray(...array_merge(enhanced, ...arrays)),
|
|
901
|
+
unique: () => new EnhancedArray(...array_unique(enhanced)),
|
|
902
|
+
reverse: (preserveKeys = false) => new EnhancedArray(...array_reverse(enhanced, preserveKeys)),
|
|
903
|
+
search: (needle) => array_search(needle, enhanced),
|
|
904
|
+
sum: () => array_sum(enhanced),
|
|
905
|
+
product: () => array_product(enhanced),
|
|
906
|
+
rand: (num = 1) => array_rand(enhanced, num),
|
|
907
|
+
flip: () => array_flip(enhanced),
|
|
908
|
+
countValues: () => array_count_values(enhanced),
|
|
909
|
+
intersect: (...arrays) => new EnhancedArray(...array_intersect(enhanced, ...arrays)),
|
|
910
|
+
diff: (...arrays) => new EnhancedArray(...array_diff(enhanced, ...arrays)),
|
|
911
|
+
column: (column) => array_column(enhanced, column),
|
|
912
|
+
// PHP sorting as methods
|
|
913
|
+
sort: () => new EnhancedArray(...sort(enhanced)),
|
|
914
|
+
rsort: () => new EnhancedArray(...rsort(enhanced)),
|
|
915
|
+
shuffle: () => new EnhancedArray(...shuffle(enhanced)),
|
|
916
|
+
usort: (compareFunction) => new EnhancedArray(...usort(enhanced, compareFunction))
|
|
917
|
+
});
|
|
918
|
+
return enhanced;
|
|
919
|
+
};
|
|
920
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
921
|
+
0 && (module.exports = {
|
|
922
|
+
array_chunk,
|
|
923
|
+
array_column,
|
|
924
|
+
array_count_values,
|
|
925
|
+
array_diff,
|
|
926
|
+
array_filter,
|
|
927
|
+
array_flip,
|
|
928
|
+
array_intersect,
|
|
929
|
+
array_key_exists,
|
|
930
|
+
array_keys,
|
|
931
|
+
array_map,
|
|
932
|
+
array_merge,
|
|
933
|
+
array_pop,
|
|
934
|
+
array_product,
|
|
935
|
+
array_push,
|
|
936
|
+
array_rand,
|
|
937
|
+
array_reduce,
|
|
938
|
+
array_reverse,
|
|
939
|
+
array_search,
|
|
940
|
+
array_shift,
|
|
941
|
+
array_slice,
|
|
942
|
+
array_splice,
|
|
943
|
+
array_sum,
|
|
944
|
+
array_unique,
|
|
945
|
+
array_unshift,
|
|
946
|
+
php_array,
|
|
947
|
+
range,
|
|
948
|
+
rsort,
|
|
949
|
+
shuffle,
|
|
950
|
+
sort,
|
|
951
|
+
uasort,
|
|
952
|
+
uksort,
|
|
953
|
+
usort
|
|
954
|
+
});
|