-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
133 lines (113 loc) · 3.04 KB
/
index.js
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
// Copyright (c) 2023 by Shakhbozbek Usmonov Miracle Programmer.
// leetcode.com 30 Days of JavaScript challenge
// ----- Started -----
/**
* @param {Function} fn
* @param {number} t milliseconds
* @return {Function}
*/
var debounce = function (fn, t) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => fn.apply(this, args), t);
};
};
/**
* const log = debounce(console.log, 100);
* log('Hello'); // cancelled
* log('Hello'); // cancelled
* log('Hello'); // Logged at t=100ms
*/
//----------------------------------------------------------------
/**
* @param {Array<Function>} functions
* @return {Promise<any>}
*/
var promiseAll = async function (functions) {
return new Promise((resolve, reject) => {
let results = new Array(functions.length);
let count = 0;
functions.forEach((fn, i) => {
fn()
.then((value) => {
results[i] = value;
count++;
if (count === functions.length) resolve(results);
})
.catch((err) => reject(err));
});
});
};
/**
* const promise = promiseAll([() => new Promise(res => res(42))])
* promise.then(console.log); // [42]
*/
//----------------------------------------------------------------
/**
* @param {Function} fn
* @return {Array}
*/
Array.prototype.groupBy = function (fn) {
return this.reduce((result, item) => {
const groupKey = fn(item);
if (!result[groupKey]) {
result[groupKey] = [];
}
result[groupKey].push(item);
return result;
}, {});
};
/**
* [1,2,3].groupBy(String) // {"1":[1],"2":[2],"3":[3]}
*/
//-------------------------------------------------------------
/**
* @param {Array} arr
* @param {Function} fn
* @return {Array}
*/
var sortBy = function (arr, fn) {
return arr.sort((a, b) => fn(a) - fn(b));
};
//-------------------------------------------------------------
/**
* @param {Array} arr1
* @param {Array} arr2
* @return {Array}
*/
var join = function (arr1, arr2) {
const result = {};
for (let i = 0; i < arr1.length; i++) {
result[arr1[i].id] = arr1[i];
}
for (let i = 0; i < arr2.length; i++) {
if (result[arr2[i].id]) {
for (const key in arr2[i]) result[arr2[i].id][key] = arr2[i][key];
} else {
result[arr2[i].id] = arr2[i];
}
}
return Object.values(result);
};
//-------------------------------------------------------------
/**
* @param {number[]} nums
*/
var ArrayWrapper = function (nums) {
this.nums = nums;
};
ArrayWrapper.prototype.valueOf = function () {
return this.nums.reduce((acc, cur) => acc + cur, 0);
};
ArrayWrapper.prototype.toString = function () {
return JSON.stringify(this.nums);
};
/**
* const obj1 = new ArrayWrapper([1,2]);
* const obj2 = new ArrayWrapper([3,4]);
* obj1 + obj2; // 10
* String(obj1); // "[1,2]"
* String(obj2); // "[3,4]"
*/
//-------------------- END ------------------------