-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
537 lines (505 loc) · 14.4 KB
/
index.js
File metadata and controls
537 lines (505 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/**
* Returns a random element from an array.
* @param {Array} arr - The array to pick from.
* @returns {*} A random element from the array, or undefined if invalid.
*/
export function randomElementFromArray(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
console.error(`Error: ${arr} is not a valid non-empty array`);
return;
}
const randomInteger = Math.floor(Math.random() * arr.length);
return arr[randomInteger];
}
/**
* Returns a random integer between min and max (inclusive).
* @param {number} min - Minimum value.
* @param {number} max - Maximum value.
* @returns {number} Random integer in the range.
*/
export function randomIntFromRange(min, max) {
if (typeof min !== 'number' || typeof max !== 'number') {
console.error('Error: min and max must be numbers');
return;
}
if (min > max) {
console.error('Error: min cannot be greater than max');
return;
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Capitalizes the first letter of a string.
* @param {string} str - The string to capitalize.
* @returns {string|undefined} Capitalized string, or undefined if invalid.
*/
export function capitalize(str) {
if (typeof str !== 'string') {
console.error(`Error: ${str} is not a string`);
return;
}
str = str.trim();
let firstLetter = str.slice(0, 1).toUpperCase(),
otherLetters = str.slice(1).toLowerCase();
return firstLetter + otherLetters;
}
/**
* Truncates text to a given length and adds ellipsis if needed.
* @param {Object} params - Parameters object.
* @param {string} params.text - The text to truncate.
* @param {number} params.length - Maximum length before ellipsis.
* @returns {string} Truncated text.
*/
export function ellipsify({ text, length }) {
if (typeof text !== 'string' || typeof length !== 'number') {
console.error('Error: invalid parameters for ellipsify');
return '';
}
return text.length <= length ? text : text.slice(0, length) + '...';
}
/**
* Checks if a parameter is a plain object.
* @param {*} param - The value to check.
* @returns {boolean} True if plain object, false otherwise.
*/
export function isObj(param) {
return typeof param === 'object' && !Array.isArray(param) && param !== null;
}
/**
* Shuffles the elements of an array in place.
* @param {Array} arr - The array to shuffle.
* @returns {Array|undefined} Shuffled array, or undefined if invalid.
*/
export function shuffleArr(arr) {
if (!Array.isArray(arr)) {
console.error(`Error: ${arr} is not an array`);
return;
}
for (let i = 0; i < arr.length; i++) {
let randIndex = Math.floor(Math.random() * arr.length);
let tempVal = arr[i];
arr[i] = arr[randIndex];
arr[randIndex] = tempVal;
}
return arr;
}
/**
* Shuffles the characters of a string.
* @param {string} str - The string to shuffle.
* @returns {string|undefined} Shuffled string, or undefined if invalid.
*/
export function shuffleStr(str) {
if (typeof str !== 'string') {
console.error(`Error: ${str} is not a string`);
return;
}
const arr = [...str];
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr.join('');
}
/**
* Sorts an array using quicksort algorithm.
* @param {Array<number>} arr - Array of numbers to sort.
* @returns {Array<number>|undefined} Sorted array, or undefined if invalid.
*/
export function sortArr(arr) {
if (!Array.isArray(arr)) {
console.error(`Error: ${arr} is not an array`);
return;
}
if (arr.length <= 1) {
return arr;
}
const pivot = arr[arr.length - 1];
const left = [];
const right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [...sortArr(left), pivot, ...sortArr(right)];
}
/**
* Removes duplicates from an array.
* @param {Array} arr - The array to process.
* @returns {Array|undefined} Array with unique values, or undefined if invalid.
*/
export function unique(arr) {
if (!Array.isArray(arr)) {
console.error(`Error: ${arr} is not an array`);
return;
}
return [...new Set(arr)];
}
/**
* Splits an array into chunks of given size.
* @param {Array} arr - The array to split.
* @param {number} size - Size of each chunk.
* @returns {Array<Array>|undefined} Array of chunks, or undefined if invalid.
*/
export function chunk(arr, size) {
if (!Array.isArray(arr)) {
console.error(`Error: ${arr} is not an array`);
return;
}
if (typeof size !== 'number' || size <= 0) {
console.error('Error: size must be a positive number');
return;
}
const result = [];
for (let i = 0; i < arr.length; i += size) {
result.push(arr.slice(i, i + size));
}
return result;
}
/**
* Flattens a nested array into a single level.
* @param {Array} arr - The array to flatten.
* @returns {Array|undefined} Flattened array, or undefined if invalid.
*/
export function flatten(arr) {
if (!Array.isArray(arr)) {
console.error(`Error: ${arr} is not an array`);
return;
}
return arr.reduce(
(acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val),
[]
);
}
/**
* Removes falsy values from an array.
* @param {Array} arr - The array to compact.
* @returns {Array|undefined} Compacted array, or undefined if invalid.
*/
export function compact(arr) {
if (!Array.isArray(arr)) {
console.error(`Error: ${arr} is not an array`);
return;
}
return arr.filter(Boolean);
}
/**
* Returns the intersection of two arrays.
* @param {Array} arr1 - First array.
* @param {Array} arr2 - Second array.
* @returns {Array|undefined} Intersection array, or undefined if invalid.
*/
export function intersection(arr1, arr2) {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
console.error('Error: both parameters must be arrays');
return;
}
const set2 = new Set(arr2);
return arr1.filter((item) => set2.has(item));
}
/**
* Returns the difference between two arrays (elements in arr1 not in arr2).
* @param {Array} arr1 - First array.
* @param {Array} arr2 - Second array.
* @returns {Array|undefined} Difference array, or undefined if invalid.
*/
export function difference(arr1, arr2) {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
console.error('Error: both parameters must be arrays');
return;
}
const set2 = new Set(arr2);
return arr1.filter((item) => !set2.has(item));
}
/**
* Shallow or recursive merge of two objects.
* @param {Object} obj1 - Target object.
* @param {Object} obj2 - Source object.
* @param {boolean} [deep=false] - Whether to perform deep merge.
* @returns {Object} Merged object.
*/
export function merge(obj1, obj2, deep = false) {
if (!isObj(obj1) || !isObj(obj2)) {
console.error('Error: both parameters must be plain objects');
return {};
}
const result = { ...obj1 };
for (const key in obj2) {
if (deep && isObj(obj2[key])) {
result[key] = merge(result[key] || {}, obj2[key], true);
} else {
result[key] = obj2[key];
}
}
return result;
}
/**
* Returns a new object without specified keys.
* @param {Object} obj - Source object.
* @param {string[]} keys - Keys to omit.
* @returns {Object} New object without specified keys.
*/
export function omit(obj, keys) {
if (!isObj(obj) || !Array.isArray(keys)) {
console.error('Error: invalid parameters for omit');
return {};
}
const result = {};
for (const key in obj) {
if (!keys.includes(key)) {
result[key] = obj[key];
}
}
return result;
}
/**
* Returns a new object with only specified keys.
* @param {Object} obj - Source object.
* @param {string[]} keys - Keys to pick.
* @returns {Object} New object with only specified keys.
*/
export function pick(obj, keys) {
if (!isObj(obj) || !Array.isArray(keys)) {
console.error('Error: invalid parameters for pick');
return {};
}
const result = {};
for (const key of keys) {
if (key in obj) {
result[key] = obj[key];
}
}
return result;
}
/**
* Swaps keys and values of an object.
* @param {Object} obj - Source object.
* @returns {Object} New object with inverted keys and values.
*/
export function invert(obj) {
if (!isObj(obj)) {
console.error('Error: parameter must be a plain object');
return {};
}
const result = {};
for (const key in obj) {
result[obj[key]] = key;
}
return result;
}
/**
* Deeply compares two objects for equality.
* @param {Object} obj1 - First object.
* @param {Object} obj2 - Second object.
* @returns {boolean} True if objects are deeply equal, false otherwise.
*/
export function deepCompareObj(obj1, obj2) {
if (!isObj(obj1) || !isObj(obj2)) {
console.error('Error: both parameters must be plain objects');
return false;
}
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) return false;
for (const key of keys1) {
const val1 = obj1[key];
const val2 = obj2[key];
const areObjects = isObj(val1) && isObj(val2);
if (areObjects && !deepCompareObj(val1, val2)) {
return false;
} else if (!areObjects && val1 !== val2) {
return false;
}
}
return true;
}
/**
* Restricts a number within a given range.
* @param {number} num - The number to clamp.
* @param {number} min - Minimum allowed value.
* @param {number} max - Maximum allowed value.
* @returns {number|undefined} Clamped number, or undefined if invalid.
*/
export function clamp(num, min, max) {
if (
typeof num !== 'number' ||
typeof min !== 'number' ||
typeof max !== 'number'
) {
console.error('Error: num, min, and max must be numbers');
return;
}
if (min > max) {
console.error('Error: min cannot be greater than max');
return;
}
return Math.min(Math.max(num, min), max);
}
/**
* Calculates the sum of numbers in an array.
* @param {number[]} arr - Array of numbers.
* @returns {number|undefined} Sum of array values, or undefined if invalid.
*/
export function sumArr(arr) {
if (!Array.isArray(arr) || arr.some((n) => typeof n !== 'number')) {
console.error('Error: arr must be an array of numbers');
return;
}
return arr.reduce((acc, val) => acc + val, 0);
}
/**
* Calculates the average (mean) of numbers in an array.
* @param {number[]} arr - Array of numbers.
* @returns {number|undefined} Average of array values, or undefined if invalid.
*/
export function averageArr(arr) {
if (
!Array.isArray(arr) ||
arr.length === 0 ||
arr.some((n) => typeof n !== 'number')
) {
console.error('Error: arr must be a non-empty array of numbers');
return;
}
return sumArr(arr) / arr.length;
}
/**
* Computes the factorial of a number.
* @param {number} n - Non-negative integer.
* @returns {number|undefined} Factorial of n, or undefined if invalid.
*/
export function factorial(n) {
if (typeof n !== 'number' || n < 0 || !Number.isInteger(n)) {
console.error('Error: n must be a non-negative integer');
return;
}
if (n === 0 || n === 1) return 1;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
/**
* Checks if a number is prime.
* @param {number} n - Integer to check.
* @returns {boolean} True if prime, false otherwise.
*/
export function isPrime(n) {
if (typeof n !== 'number' || n <= 1 || !Number.isInteger(n)) {
console.error('Error: n must be an integer greater than 1');
return false;
}
if (n === 2) return true;
if (n % 2 === 0) return false;
const sqrt = Math.sqrt(n);
for (let i = 3; i <= sqrt; i += 2) {
if (n % i === 0) return false;
}
return true;
}
/**
* Converts a string to camelCase.
* @param {string} str - The string to convert.
* @returns {string|undefined} camelCase string, or undefined if invalid.
*/
export function camelCase(str) {
if (typeof str !== 'string') {
console.error(`Error: ${str} is not a string`);
return;
}
return str
.toLowerCase()
.replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ''))
.replace(/^(.)/, (c) => c.toLowerCase());
}
/**
* Converts a string to kebab-case.
* @param {string} str - The string to convert.
* @returns {string|undefined} kebab-case string, or undefined if invalid.
*/
export function kebabCase(str) {
if (typeof str !== 'string') {
console.error(`Error: ${str} is not a string`);
return;
}
return str
.trim()
.toLowerCase()
.replace(/[\s_]+/g, '-')
.replace(/[^a-z0-9-]/g, '');
}
/**
* Converts a string to a URL-friendly slug.
* @param {string} str - The string to slugify.
* @returns {string|undefined} Slugified string, or undefined if invalid.
*/
export function slugify(str) {
if (typeof str !== 'string') {
console.error(`Error: ${str} is not a string`);
return;
}
return str
.trim()
.toLowerCase()
.replace(/[\s]+/g, '-')
.replace(/[^a-z0-9-]/g, '')
.replace(/-+/g, '-');
}
/**
* Reverses the characters in a string.
* @param {string} str - The string to reverse.
* @returns {string|undefined} Reversed string, or undefined if invalid.
*/
export function reverse(str) {
if (typeof str !== 'string') {
console.error(`Error: ${str} is not a string`);
return;
}
return [...str].reverse().join('');
}
/**
* Pads a string to a given length with a specified character.
* @param {string} str - The string to pad.
* @param {number} length - Desired total length.
* @param {string} char - Character to pad with.
* @returns {string|undefined} Padded string, or undefined if invalid.
*/
export function pad(str, length, char = ' ') {
if (
typeof str !== 'string' ||
typeof length !== 'number' ||
typeof char !== 'string'
) {
console.error('Error: invalid parameters for pad');
return;
}
if (char.length !== 1) {
console.error('Error: pad character must be a single character');
return;
}
if (str.length >= length) return str;
const padLength = length - str.length;
const leftPad = Math.floor(padLength / 2);
const rightPad = padLength - leftPad;
return char.repeat(leftPad) + str + char.repeat(rightPad);
}
/**
* Converts a string to snake_case.
* @param {string} str - The string to convert.
* @returns {string|undefined} snake_case string, or undefined if invalid.
*/
export function snakeCase(str) {
if (typeof str !== 'string') {
console.error(`Error: ${str} is not a string`);
return;
}
return str
.trim()
.toLowerCase()
.replace(/[\s\-]+/g, '_')
.replace(/[^a-z0-9_]/g, '')
.replace(/_+/g, '_');
}