@mongez/reinforcements 2.0.12 → 2.2.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 +75 -127
- package/cjs/Random/random.js +14 -34
- package/cjs/array/chunk.js +2 -2
- package/cjs/array/count.js +2 -4
- package/cjs/array/even.js +2 -2
- package/cjs/array/evenIndexes.js +1 -3
- package/cjs/array/groupBy.js +29 -98
- package/cjs/array/max.js +3 -3
- package/cjs/array/median.js +5 -12
- package/cjs/array/min.js +3 -3
- package/cjs/array/odd.js +2 -2
- package/cjs/array/oddIndexes.js +1 -3
- package/cjs/array/pluck.js +2 -6
- package/cjs/array/pushUnique.js +2 -6
- package/cjs/array/sum.js +3 -3
- package/cjs/array/unique.js +1 -2
- package/cjs/index.d.ts +0 -1
- package/cjs/index.js +0 -3
- package/cjs/mixed/clone/clone.js +1 -2
- package/cjs/mixed/shuffle/shuffle.js +1 -3
- package/cjs/number/round.js +2 -5
- package/cjs/object/except.d.ts +1 -1
- package/cjs/object/except.js +4 -9
- package/cjs/object/flatten.js +20 -41
- package/cjs/object/get.js +2 -5
- package/cjs/object/map.js +1 -1
- package/cjs/object/merge.js +20 -42
- package/cjs/object/obj.js +11 -11
- package/cjs/object/only.js +7 -22
- package/cjs/object/set.js +8 -8
- package/cjs/object/sort.js +4 -7
- package/cjs/object/unset.d.ts +5 -0
- package/cjs/object/unset.js +26 -0
- package/cjs/string/capitalize.js +1 -3
- package/cjs/string/extension.js +1 -1
- package/cjs/string/ltrim.js +2 -5
- package/cjs/string/readMoreChars.js +1 -4
- package/cjs/string/readMoreWords.js +3 -6
- package/cjs/string/repeatsOf.js +3 -6
- package/cjs/string/replaceLast.js +1 -1
- package/cjs/string/rtrim.js +2 -5
- package/cjs/string/startsWithArabic.js +2 -5
- package/cjs/string/toCamelCase.js +3 -8
- package/cjs/string/toInputName.js +8 -27
- package/cjs/string/toKebabCase.js +1 -4
- package/cjs/string/toSnakeCase.js +2 -8
- package/cjs/string/toStudlyCase.js +3 -8
- package/cjs/string/trim.js +2 -5
- package/cjs/utils/debounce.js +3 -6
- package/esm/Random/random.js +14 -34
- package/esm/array/chunk.js +2 -2
- package/esm/array/count.js +2 -4
- package/esm/array/even.js +2 -2
- package/esm/array/evenIndexes.js +1 -3
- package/esm/array/groupBy.js +29 -98
- package/esm/array/max.js +3 -3
- package/esm/array/median.js +5 -12
- package/esm/array/min.js +3 -3
- package/esm/array/odd.js +2 -2
- package/esm/array/oddIndexes.js +1 -3
- package/esm/array/pluck.js +2 -6
- package/esm/array/pushUnique.js +2 -6
- package/esm/array/sum.js +3 -3
- package/esm/array/unique.js +1 -2
- package/esm/index.d.ts +0 -1
- package/esm/index.js +0 -4
- package/esm/mixed/clone/clone.js +1 -2
- package/esm/mixed/shuffle/shuffle.js +1 -3
- package/esm/number/round.js +2 -5
- package/esm/object/except.d.ts +1 -1
- package/esm/object/except.js +4 -9
- package/esm/object/flatten.js +20 -41
- package/esm/object/get.js +2 -5
- package/esm/object/map.js +1 -1
- package/esm/object/merge.js +20 -41
- package/esm/object/obj.js +11 -11
- package/esm/object/only.js +7 -22
- package/esm/object/set.js +8 -8
- package/esm/object/sort.js +4 -7
- package/esm/object/unset.d.ts +5 -0
- package/esm/object/unset.js +24 -0
- package/esm/string/capitalize.js +1 -3
- package/esm/string/extension.js +1 -1
- package/esm/string/ltrim.js +2 -5
- package/esm/string/readMoreChars.js +1 -4
- package/esm/string/readMoreWords.js +3 -6
- package/esm/string/repeatsOf.js +3 -6
- package/esm/string/replaceLast.js +1 -1
- package/esm/string/rtrim.js +2 -5
- package/esm/string/startsWithArabic.js +2 -5
- package/esm/string/toCamelCase.js +3 -8
- package/esm/string/toInputName.js +8 -26
- package/esm/string/toKebabCase.js +1 -4
- package/esm/string/toSnakeCase.js +2 -8
- package/esm/string/toStudlyCase.js +3 -8
- package/esm/string/trim.js +2 -5
- package/esm/utils/debounce.js +3 -6
- package/package.json +1 -11
- package/cjs/Collection/ImmutableCollection.d.ts +0 -651
- package/cjs/Collection/ImmutableCollection.js +0 -1763
- package/cjs/__helpers/tslib/tslib.es6.js +0 -102
- package/cjs/array/countBy.js +0 -23
- package/cjs/array/prependUnique.js +0 -20
- package/cjs/types.js +0 -79
- package/docs/collection-todo.md +0 -56
- package/docs/collection.md +0 -2606
- package/docs/todo.md +0 -3
- package/esm/Collection/ImmutableCollection.d.ts +0 -651
- package/esm/Collection/ImmutableCollection.js +0 -1684
- package/esm/__helpers/tslib/tslib.es6.js +0 -95
- package/esm/array/countBy.js +0 -21
- package/esm/array/prependUnique.js +0 -18
- package/esm/types.js +0 -75
package/README.md
CHANGED
|
@@ -14,43 +14,18 @@ or using `npm`
|
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
|
-
##
|
|
17
|
+
## Getting value from an object
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
To get a value from an object using `dot.notation` syntax, you can use `get` function.
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const collection = collect([
|
|
25
|
-
{ name: 'John', age: 20 },
|
|
26
|
-
{ name: 'Jane', age: 25 },
|
|
27
|
-
{ name: 'Jack', age: 30 },
|
|
28
|
-
]);
|
|
29
|
-
|
|
30
|
-
// get the first item
|
|
31
|
-
const firstItem = collection.first();
|
|
32
|
-
|
|
33
|
-
// get the last item
|
|
34
|
-
const lastItem = collection.last();
|
|
35
|
-
|
|
36
|
-
// get the item at index 1
|
|
37
|
-
const itemAtIndex1 = collection.at(1);
|
|
38
|
-
|
|
39
|
-
// get users with age greater than 25
|
|
40
|
-
const users = collection.where('age', '>', 25);
|
|
41
|
-
|
|
42
|
-
// get users with age greater than 25 and less than 30
|
|
43
|
-
const users = collection.where('age', '>', 25).where('age', '<', 30);
|
|
22
|
+
get(object: object, key: string, defaultValue?: any): any
|
|
44
23
|
```
|
|
45
24
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
## Working with objects
|
|
49
|
-
|
|
50
|
-
Reinforcements is shipped with `Obj` object which provides good utilities for working with objects.
|
|
25
|
+
Let's see an example:
|
|
51
26
|
|
|
52
27
|
```ts
|
|
53
|
-
import {
|
|
28
|
+
import { get } from "@mongez/reinforcements";
|
|
54
29
|
|
|
55
30
|
let user = {
|
|
56
31
|
id: 1,
|
|
@@ -69,12 +44,12 @@ let user = {
|
|
|
69
44
|
},
|
|
70
45
|
};
|
|
71
46
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
47
|
+
get(user, "id"); // 1
|
|
48
|
+
get(user, "name"); // {first: 'Hasan', last: 'Zohdy'}
|
|
49
|
+
get(user, "name.first"); // Hasan
|
|
50
|
+
get(user, "address.country"); // Egypt
|
|
51
|
+
get(user, "address.building.number"); // 12
|
|
52
|
+
get(user, "address.building.floor.number"); // 3
|
|
78
53
|
```
|
|
79
54
|
|
|
80
55
|
As we can see in the previous example, we can get values from objects using **dot.notation.syntax**.
|
|
@@ -82,23 +57,15 @@ As we can see in the previous example, we can get values from objects using **do
|
|
|
82
57
|
If the key is missing in the object, we may return default value instead.
|
|
83
58
|
|
|
84
59
|
```ts
|
|
85
|
-
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
You can also import `get` function directly from the package.
|
|
89
|
-
|
|
90
|
-
```ts
|
|
91
|
-
import { get } from "@mongez/reinforcements";
|
|
92
|
-
|
|
93
|
-
get(user, "id"); // 1
|
|
60
|
+
get(user, "email", "no-email"); // no-email
|
|
94
61
|
```
|
|
95
62
|
|
|
96
63
|
### Setting value in object
|
|
97
64
|
|
|
98
|
-
This works exactly but `
|
|
65
|
+
This works exactly but `set(object, key, value)` will set the value to the given object, which means it won't return **a new object** but the same object.
|
|
99
66
|
|
|
100
67
|
```ts
|
|
101
|
-
import {
|
|
68
|
+
import { set } from "@mongez/reinforcements";
|
|
102
69
|
|
|
103
70
|
let user = {
|
|
104
71
|
id: 1,
|
|
@@ -117,14 +84,14 @@ let user = {
|
|
|
117
84
|
},
|
|
118
85
|
};
|
|
119
86
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
87
|
+
set(user, "email", "hassanzohdy@gmail.com");
|
|
88
|
+
set(user, "address.building.floor.apartment", 36);
|
|
89
|
+
set(user, "job.title", "Software Engineer");
|
|
123
90
|
```
|
|
124
91
|
|
|
125
|
-
In the previous example, we've three different cases, first case which would not be used with `
|
|
92
|
+
In the previous example, we've three different cases, first case which would not be used with `set` which is setting one level key to the given object `user`, in this case we added `email` key.
|
|
126
93
|
|
|
127
|
-
In the second scenario, we added a new nested key in `address.building.floor` object, which is `apartment`, this would be a nice case to use `
|
|
94
|
+
In the second scenario, we added a new nested key in `address.building.floor` object, which is `apartment`, this would be a nice case to use `set`.
|
|
128
95
|
|
|
129
96
|
The last scenario, we don't have `job` key, the function will create `job` key then set `job.title` inside it.
|
|
130
97
|
|
|
@@ -154,22 +121,12 @@ The final user object will be:
|
|
|
154
121
|
}
|
|
155
122
|
```
|
|
156
123
|
|
|
157
|
-
You can also import `set` function directly from the package.
|
|
158
|
-
|
|
159
|
-
```ts
|
|
160
|
-
import { set } from "@mongez/reinforcements";
|
|
161
|
-
|
|
162
|
-
set(user, "email", "hassanzohdy@gmail.com");
|
|
163
|
-
```
|
|
164
|
-
|
|
165
124
|
### Merging objects deeply
|
|
166
125
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
You may use `Obj.merge` or import `merge` directly from the package.
|
|
126
|
+
To merge two objects deeply, you can use `merge` function.
|
|
170
127
|
|
|
171
128
|
```ts
|
|
172
|
-
import {
|
|
129
|
+
import { merge } from "@mongez/reinforcements";
|
|
173
130
|
|
|
174
131
|
const user = {
|
|
175
132
|
id: 1,
|
|
@@ -183,7 +140,7 @@ const userJob = {
|
|
|
183
140
|
},
|
|
184
141
|
};
|
|
185
142
|
|
|
186
|
-
const userWithJob =
|
|
143
|
+
const userWithJob = merge(user, userJob);
|
|
187
144
|
```
|
|
188
145
|
|
|
189
146
|
Final output:
|
|
@@ -222,7 +179,7 @@ const userWithJob = Object.assign({}, user, userJob);
|
|
|
222
179
|
In the previous example, that would be the proper approach as the merging depth here is simple, but let's take another example.
|
|
223
180
|
|
|
224
181
|
```ts
|
|
225
|
-
import {
|
|
182
|
+
import { merge } from "@mongez/reinforcements";
|
|
226
183
|
|
|
227
184
|
const user = {
|
|
228
185
|
id: 1,
|
|
@@ -238,7 +195,7 @@ const userJob = {
|
|
|
238
195
|
},
|
|
239
196
|
};
|
|
240
197
|
|
|
241
|
-
const userWithJob =
|
|
198
|
+
const userWithJob = merge(user, userJob);
|
|
242
199
|
```
|
|
243
200
|
|
|
244
201
|
The output will be:
|
|
@@ -286,17 +243,9 @@ const userWithJob = Object.assign({}, user, userJob);
|
|
|
286
243
|
}
|
|
287
244
|
```
|
|
288
245
|
|
|
289
|
-
You can also import `merge` function directly from the package.
|
|
290
|
-
|
|
291
|
-
```ts
|
|
292
|
-
import { merge } from "@mongez/reinforcements";
|
|
293
|
-
|
|
294
|
-
const userWithJob = merge(user, userJob);
|
|
295
|
-
```
|
|
296
|
-
|
|
297
246
|
### Clone objects
|
|
298
247
|
|
|
299
|
-
You can also make a **deep copy** for the given object using `
|
|
248
|
+
You can also make a **deep copy** for the given object using `clone`
|
|
300
249
|
|
|
301
250
|
```ts
|
|
302
251
|
const user = {
|
|
@@ -315,10 +264,10 @@ console.log(normalClonedUser.name.first); // Ali
|
|
|
315
264
|
console.log(user.name.first); // Ali
|
|
316
265
|
```
|
|
317
266
|
|
|
318
|
-
Now using `
|
|
267
|
+
Now using `clone` method
|
|
319
268
|
|
|
320
269
|
```ts
|
|
321
|
-
import {
|
|
270
|
+
import { clone } from "@mongez/reinforcements";
|
|
322
271
|
|
|
323
272
|
const user = {
|
|
324
273
|
id: 1,
|
|
@@ -327,7 +276,7 @@ const user = {
|
|
|
327
276
|
},
|
|
328
277
|
};
|
|
329
278
|
|
|
330
|
-
const normalClonedUser =
|
|
279
|
+
const normalClonedUser = clone(user);
|
|
331
280
|
|
|
332
281
|
cloned.name.first = "Ali";
|
|
333
282
|
|
|
@@ -338,10 +287,10 @@ console.log(user.name.first); // Hasan
|
|
|
338
287
|
|
|
339
288
|
### Getting certain values from object
|
|
340
289
|
|
|
341
|
-
To get a new object from the base object with only list of keys, use `
|
|
290
|
+
To get a new object from the base object with only list of keys, use `only(object: object, keys: string[]): object`
|
|
342
291
|
|
|
343
292
|
```ts
|
|
344
|
-
import {
|
|
293
|
+
import { only } from "@mongez/reinforcements";
|
|
345
294
|
|
|
346
295
|
const user = {
|
|
347
296
|
id: 1,
|
|
@@ -361,23 +310,15 @@ const user = {
|
|
|
361
310
|
},
|
|
362
311
|
};
|
|
363
312
|
|
|
364
|
-
const simpleUserData =
|
|
365
|
-
```
|
|
366
|
-
|
|
367
|
-
You can also import `only` function directly from the package.
|
|
368
|
-
|
|
369
|
-
```ts
|
|
370
|
-
import { only } from "@mongez/reinforcements";
|
|
371
|
-
|
|
372
|
-
const simpleUserData = only(user, ["id", "name", "email"]);
|
|
313
|
+
const simpleUserData = only(user, ["id", "name", "email"]); // {id: 1, name: 'Hasan Zohdy', email: 'hassanzohdy@gmail.com'}
|
|
373
314
|
```
|
|
374
315
|
|
|
375
316
|
### Getting all object except for certain keys
|
|
376
317
|
|
|
377
|
-
This is the reverse of `
|
|
318
|
+
This is the reverse of `only`, which returns the entire object except for the given keys.
|
|
378
319
|
|
|
379
320
|
```ts
|
|
380
|
-
import {
|
|
321
|
+
import { except } from "@mongez/reinforcements";
|
|
381
322
|
|
|
382
323
|
const user = {
|
|
383
324
|
id: 1,
|
|
@@ -397,15 +338,37 @@ const user = {
|
|
|
397
338
|
},
|
|
398
339
|
};
|
|
399
340
|
|
|
400
|
-
const simpleUserData =
|
|
341
|
+
const simpleUserData = except(user, ["id", "address", "email"]); // { name: 'Hasan Zohdy', email: 'hassanzohdy@gmail.com', job: {title: 'Software Engineer'}}
|
|
401
342
|
```
|
|
402
343
|
|
|
403
|
-
|
|
344
|
+
### Unset keys from object
|
|
345
|
+
|
|
346
|
+
> Added in 2.1.0
|
|
347
|
+
|
|
348
|
+
To remove certain keys from the object, use `unset(object: object, keys: string[]): object`
|
|
404
349
|
|
|
405
350
|
```ts
|
|
406
|
-
import {
|
|
351
|
+
import { unset } from "@mongez/reinforcements";
|
|
407
352
|
|
|
408
|
-
const
|
|
353
|
+
const user = {
|
|
354
|
+
id: 1,
|
|
355
|
+
name: "Hasan Zohdy",
|
|
356
|
+
email: "hassanzohdy@gmail.com",
|
|
357
|
+
job: {
|
|
358
|
+
title: "Software Engineer",
|
|
359
|
+
},
|
|
360
|
+
address: {
|
|
361
|
+
country: "Egypt",
|
|
362
|
+
building: {
|
|
363
|
+
number: 12,
|
|
364
|
+
floor: {
|
|
365
|
+
number: 3,
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
const simpleUserData = unset(user, ["id", "address", "email"]); // { name: 'Hasan Zohdy', job: {title: 'Software Engineer'}}
|
|
409
372
|
```
|
|
410
373
|
|
|
411
374
|
### Flatten objects
|
|
@@ -413,7 +376,7 @@ const simpleUserData = except(user, ["id", "address", "email"]);
|
|
|
413
376
|
We can flatten any big fat objects into one object, with only one dimension.
|
|
414
377
|
|
|
415
378
|
```ts
|
|
416
|
-
import {
|
|
379
|
+
import { flatten } from "@mongez/reinforcements";
|
|
417
380
|
|
|
418
381
|
const user = {
|
|
419
382
|
id: 1,
|
|
@@ -433,7 +396,7 @@ const user = {
|
|
|
433
396
|
},
|
|
434
397
|
};
|
|
435
398
|
|
|
436
|
-
console.log(
|
|
399
|
+
console.log(flatten(user));
|
|
437
400
|
```
|
|
438
401
|
|
|
439
402
|
Output:
|
|
@@ -453,7 +416,7 @@ Output:
|
|
|
453
416
|
You may set the separator by passing second argument to the function.
|
|
454
417
|
|
|
455
418
|
```ts
|
|
456
|
-
import {
|
|
419
|
+
import { flatten } from "@mongez/reinforcements";
|
|
457
420
|
|
|
458
421
|
const user = {
|
|
459
422
|
id: 1,
|
|
@@ -473,7 +436,7 @@ const user = {
|
|
|
473
436
|
},
|
|
474
437
|
};
|
|
475
438
|
|
|
476
|
-
console.log(
|
|
439
|
+
console.log(flatten(user, "->"));
|
|
477
440
|
```
|
|
478
441
|
|
|
479
442
|
Output:
|
|
@@ -490,18 +453,10 @@ Output:
|
|
|
490
453
|
}
|
|
491
454
|
```
|
|
492
455
|
|
|
493
|
-
You can also import `flatten` function directly from the package.
|
|
494
|
-
|
|
495
|
-
```ts
|
|
496
|
-
import { flatten } from "@mongez/reinforcements";
|
|
497
|
-
|
|
498
|
-
console.log(flatten(user));
|
|
499
|
-
```
|
|
500
|
-
|
|
501
456
|
If the object has an instance of class, all class members will be included in the flattened object.
|
|
502
457
|
|
|
503
458
|
```ts
|
|
504
|
-
import {
|
|
459
|
+
import { flatten } from "@mongez/reinforcements";
|
|
505
460
|
|
|
506
461
|
class User {
|
|
507
462
|
id = 1;
|
|
@@ -523,7 +478,7 @@ class User {
|
|
|
523
478
|
|
|
524
479
|
const user = new User();
|
|
525
480
|
|
|
526
|
-
console.log(
|
|
481
|
+
console.log(flatten(user));
|
|
527
482
|
```
|
|
528
483
|
|
|
529
484
|
Output:
|
|
@@ -542,10 +497,10 @@ Output:
|
|
|
542
497
|
|
|
543
498
|
### Sort object by its keys
|
|
544
499
|
|
|
545
|
-
To sort objects based on their keys alphabets recursively use `
|
|
500
|
+
To sort objects based on their keys alphabets recursively use `sort(object: object, recursive: boolean = true): object` function.
|
|
546
501
|
|
|
547
502
|
```ts
|
|
548
|
-
import {
|
|
503
|
+
import { sort } from "@mongez/reinforcements";
|
|
549
504
|
|
|
550
505
|
const user = {
|
|
551
506
|
id: 1,
|
|
@@ -565,7 +520,7 @@ const user = {
|
|
|
565
520
|
},
|
|
566
521
|
};
|
|
567
522
|
|
|
568
|
-
console.log(
|
|
523
|
+
console.log(sort(user));
|
|
569
524
|
```
|
|
570
525
|
|
|
571
526
|
Output:
|
|
@@ -593,7 +548,7 @@ Output:
|
|
|
593
548
|
To sort the object only the first level, pass the second argument as false.
|
|
594
549
|
|
|
595
550
|
```ts
|
|
596
|
-
import {
|
|
551
|
+
import { sort } from "@mongez/reinforcements";
|
|
597
552
|
|
|
598
553
|
const user = {
|
|
599
554
|
id: 1,
|
|
@@ -613,7 +568,7 @@ const user = {
|
|
|
613
568
|
},
|
|
614
569
|
};
|
|
615
570
|
|
|
616
|
-
console.log(
|
|
571
|
+
console.log(sort(user, false));
|
|
617
572
|
```
|
|
618
573
|
|
|
619
574
|
Output:
|
|
@@ -638,14 +593,6 @@ Output:
|
|
|
638
593
|
}
|
|
639
594
|
```
|
|
640
595
|
|
|
641
|
-
You can also import `sort` function directly from the package.
|
|
642
|
-
|
|
643
|
-
```ts
|
|
644
|
-
import { sort } from "@mongez/reinforcements";
|
|
645
|
-
|
|
646
|
-
console.log(sort(user));
|
|
647
|
-
```
|
|
648
|
-
|
|
649
596
|
## Equal Values
|
|
650
597
|
|
|
651
598
|
Using `areEqual` function will check if the given two values equal to each other, it will validate against any type such as objects, arrays, strings, numbers, booleans, null, undefined, symbols.
|
|
@@ -1258,12 +1205,13 @@ function sendEmail(e: any) {
|
|
|
1258
1205
|
|
|
1259
1206
|
To run tests run `npm run test` or `yarn test`
|
|
1260
1207
|
|
|
1261
|
-
## TODO
|
|
1262
|
-
|
|
1263
|
-
If you want to contribute to this package, you can check the [todo list page](./docs/todo.md).
|
|
1264
|
-
|
|
1265
1208
|
## Change Log
|
|
1266
1209
|
|
|
1210
|
+
- 2.2.0 (08 Nov 2022)
|
|
1211
|
+
- Migrated Collections From Package to separate package.
|
|
1212
|
+
- 2.1.0 (06 Nov 2022)
|
|
1213
|
+
- Added [unset](#unset-keys-from-object) function.
|
|
1214
|
+
- Fixed `except` and `only` functions to accept dot notation syntax.
|
|
1267
1215
|
- 2.0.5 (25 Oct 2022)
|
|
1268
1216
|
- Added `toKebabCase` function
|
|
1269
1217
|
- 2.0.0 (24 Oct 2022)
|
package/cjs/Random/random.js
CHANGED
|
@@ -1,40 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const possible =
|
|
4
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
5
|
+
const Random = {
|
|
5
6
|
/**
|
|
6
7
|
* Get a random integer
|
|
7
8
|
*/
|
|
8
|
-
int
|
|
9
|
-
if (min === void 0) {
|
|
10
|
-
min = 1;
|
|
11
|
-
}
|
|
12
|
-
if (max === void 0) {
|
|
13
|
-
max = 9999999;
|
|
14
|
-
}
|
|
9
|
+
int(min = 1, max = 9999999) {
|
|
15
10
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
16
11
|
},
|
|
17
12
|
/**
|
|
18
13
|
* @alias Random.int
|
|
19
14
|
*/
|
|
20
|
-
integer
|
|
21
|
-
if (min === void 0) {
|
|
22
|
-
min = 1;
|
|
23
|
-
}
|
|
24
|
-
if (max === void 0) {
|
|
25
|
-
max = 9999999;
|
|
26
|
-
}
|
|
15
|
+
integer(min = 1, max = 9999999) {
|
|
27
16
|
return this.int(min, max);
|
|
28
17
|
},
|
|
29
18
|
/**
|
|
30
19
|
* Generate random string
|
|
31
20
|
*/
|
|
32
|
-
string
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
var text = "";
|
|
37
|
-
for (var i = 0; i < length; i++) {
|
|
21
|
+
string(length = 32) {
|
|
22
|
+
let text = "";
|
|
23
|
+
for (let i = 0; i < length; i++) {
|
|
38
24
|
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
39
25
|
}
|
|
40
26
|
return text;
|
|
@@ -42,44 +28,38 @@ var Random = {
|
|
|
42
28
|
/**
|
|
43
29
|
* Generate random id
|
|
44
30
|
*/
|
|
45
|
-
id
|
|
46
|
-
if (length === void 0) {
|
|
47
|
-
length = 6;
|
|
48
|
-
}
|
|
49
|
-
if (startsWith === void 0) {
|
|
50
|
-
startsWith = "el-";
|
|
51
|
-
}
|
|
31
|
+
id(length = 6, startsWith = "el-") {
|
|
52
32
|
return startsWith + Random.string(length);
|
|
53
33
|
},
|
|
54
34
|
/**
|
|
55
35
|
* Generate random boolean value
|
|
56
36
|
*/
|
|
57
|
-
bool
|
|
37
|
+
bool() {
|
|
58
38
|
return Random.int(0, 1) === 1;
|
|
59
39
|
},
|
|
60
40
|
/**
|
|
61
41
|
* Alias to bool method
|
|
62
42
|
*/
|
|
63
|
-
boolean
|
|
43
|
+
boolean() {
|
|
64
44
|
return Random.int(0, 1) === 1;
|
|
65
45
|
},
|
|
66
46
|
/**
|
|
67
47
|
* Get random date
|
|
68
48
|
*/
|
|
69
|
-
date
|
|
49
|
+
date(minDate, maxDate) {
|
|
70
50
|
if (minDate && !maxDate) {
|
|
71
51
|
return new Date(Random.int(minDate.getTime(), Date.now()));
|
|
72
52
|
}
|
|
73
53
|
if (!minDate && maxDate) {
|
|
74
54
|
return new Date(Random.int(0, maxDate.getTime()));
|
|
75
55
|
}
|
|
76
|
-
|
|
56
|
+
const now = Date.now();
|
|
77
57
|
return new Date(Random.int(now - 100000000000, now));
|
|
78
58
|
},
|
|
79
59
|
/**
|
|
80
60
|
* Get random color
|
|
81
61
|
*/
|
|
82
|
-
color
|
|
62
|
+
color() {
|
|
83
63
|
return "#" + Math.floor(Math.random() * 16777215).toString(16);
|
|
84
64
|
},
|
|
85
65
|
};
|
package/cjs/array/chunk.js
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
function chunk(array, size) {
|
|
7
7
|
if (!Array.isArray(array) && typeof array !== "string") return [];
|
|
8
|
-
|
|
9
|
-
for (
|
|
8
|
+
const result = [];
|
|
9
|
+
for (let i = 0; i < array.length; i += size) {
|
|
10
10
|
result.push(array.slice(i, i + size));
|
|
11
11
|
}
|
|
12
12
|
return result;
|
package/cjs/array/count.js
CHANGED
|
@@ -2,16 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
var get = require("../object/get.js");
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const notFound = Symbol("notFound");
|
|
6
6
|
/**
|
|
7
7
|
* Count data by the given key or callback
|
|
8
8
|
*/
|
|
9
9
|
function count(data, key) {
|
|
10
10
|
if (!Array.isArray(data)) return 0;
|
|
11
11
|
if (typeof key === "string") {
|
|
12
|
-
return data.filter(
|
|
13
|
-
return get(item, key, notFound) !== notFound;
|
|
14
|
-
}).length;
|
|
12
|
+
return data.filter(item => get(item, key, notFound) !== notFound).length;
|
|
15
13
|
}
|
|
16
14
|
return data.filter(key).length;
|
|
17
15
|
}
|
package/cjs/array/even.js
CHANGED
|
@@ -9,8 +9,8 @@ var get = require("../object/get.js");
|
|
|
9
9
|
*/
|
|
10
10
|
function even(array, key) {
|
|
11
11
|
if (!Array.isArray(array)) return [];
|
|
12
|
-
return array.filter(
|
|
13
|
-
|
|
12
|
+
return array.filter(item => {
|
|
13
|
+
const value = key ? get(item, key) : item;
|
|
14
14
|
return typeof value !== "number" ? false : value % 2 === 0;
|
|
15
15
|
});
|
|
16
16
|
}
|
package/cjs/array/evenIndexes.js
CHANGED