-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamplefaq.js
More file actions
140 lines (117 loc) · 2.47 KB
/
samplefaq.js
File metadata and controls
140 lines (117 loc) · 2.47 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
console.log(1);
setTimeout(()=>{
console.log(2);
},10);
var promise = new Promise((resolve,reject)=>{
console.log(5);
setTimeout(()=>{
console.log(6);
},10)
resolve('done');
})
promise.then(()=>{
console.log(3);
setTimeout(()=>{
console.log(4);
},10)
})
//1 5 3 2 6 4
export default function promiseAll(iterable) {
return new Promise((resolve, reject) =>{
let unresolved = iterable.length;
const results = new Array(unresolved);
if(unresolved === 0){
resolve(results);
return
}
iterable.forEach(async (item,index)=>{
try{
const value = await item;
results[index] = value;
unresolved -=1;
if(unresolved === 0){
resolve(results)
}
}catch(e){
reject(e)
}
})
});
}
const arrayData = [[1,2],3,[4,[5,6]],7];
export default function flatten(value) {
const result = [];
value.forEach(data =>{
if(Array.isArray(data)){
result.push(...flatten(data));
}else{
result.push(data);
}
});
return result;
}
var fnSum = function(a){
return function(b){
if(b){
return fnSum(a*b);
}
return a;
}
}
console.log(fnSum(1)(2)(3)(4)());
/*
ClosureSum(10,5)
ClosureSum(10,5.8,9,15,69,12)
ClosureSum(10)(5)
ClosureSum(2)(3,7,9,12,17)
*/
function ClosureSum(){
var total = 0;
var args1 = Array.prototype.slice.call(arguments);
args1.forEach(function(val){
total+= val;
});
return function innerSum(){
var args2 = Array.prototype.slice.call(arguments);
args2.forEach(function(val){
total+= val;
});
return total;
}
return total;
}
console.log(ClosureSum(10)(2,3,4,5));
export default function debounce(func, wait) {
let timerId = null;
if(wait < 50000){
return function(...args){
clearTimeout(timerId)
timerId = setTimeout(()=>{
func.apply(this, args);
}, wait)
}
}
}
export default function deepClone(value) {
//return structuredClone(value);
if(value == null || typeof value != "object"){
return value;
}
const newObj = Array.isArray(value) ? [] : {};
for(let key in value){
newObj[key] = deepClone(value[key]);
}
return newObj
}
export default memoize = (fn) => {
const cache = new Map();
return (...args) => {
const key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
const result = fn(...args);
cache.set(key, result);
return result;
};
};