-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.js
312 lines (291 loc) · 6.32 KB
/
demo.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
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
function merge(left, right) {
let i = 0
let j = 0
let result = []
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
result.push(left[i])
i++
} else {
result.push(right[j])
j++
}
}
return result.concat(left.slice(i), right.slice(j))
}
function mergeSort(arr) {
if (arr.length > 1) {
var mid = Math.floor(arr.length / 2)
var left = arr.slice(0, mid)
var right = arr.slice(mid, arr.length)
return merge(mergeSort(left), mergeSort(right))
}
return arr
}
function flattenDeep(arr, deep = 1) {
return deep > 0
? arr.reduce((a, b) => {
return a.concat(Array.isArray(b) ? flattenDeep(b, deep - 1) : b)
}, [])
: arr.slice()
}
function flattenDeep2(arr) {
var stack = [...arr]
var result = []
while (stack.length) {
var next = stack.pop()
if (Array.isArray(next)) {
stack.push(...next)
} else {
result.push(next)
}
}
return result.reverse()
}
console.log(flattenDeep2([1, 2, 3, [4, 2, [7, 8], 5]], 1))
Function.prototype.bind2 = function() {
var fn = this
var context = arguments[0]
var args = [].slice.call(arguments,1)
var resultFn = function(){
return fn.apply( this instanceof resultFn?this:context, args.concat([].slice.call(arguments)))
}
resultFn.prototype = fn.prototype
return resultFn
}
Promise.all = function (promises) {
return new Promise(function (resolve, reject) {
let result = []
for (var i = 0; i < promises.length; i++) {
!(function (i) {
promises[i].then(
(value) => {
result.push(value)
if (result.length === promises.length) {
resolve(result)
}
},
(error) => {
reject(error)
}
)
})(i)
}
})
}
Promise.race2 = function (promises) {
return new Promise((resolve, reject) => {
var isend = false
for (let i = 0; i < promises.length; i++) {
Promise.resolve(promises[i]).then(
(value) => {
!isend && resolve(value)
isend = true
},
(err) => {
!isend && reject(err)
isend = true
}
)
}
})
}
Promise.allSettled = function (promises) {
return Promise.all(
promises.map((item) => {
return item.then(
(value) => {
return { status: 'ok', value }
},
(err) => {
return { status: 'error', error: err }
}
)
})
)
}
function debounce(callback, wait) {
let timer = null
return function () {
const context = this
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
callback.apply(context, arguments)
timer = null
}, wait)
}
}
function throttle2(callback, wait) {
let previous = 0
return function() {
const context = this
let now = Date.now()
if(now-previous>wait) {
previous = now
callback.apply(context, arguments)
}
}
}
function throttle2(callback, wait) {
let timer = null
return function() {
let context = this;
let args = arguments;
if(!timer) {
timer = setTimeout(()=>{
callback.apply(context, args)
timer = null
},wait)
}
}
}
function currify(fn,params=[]){
return function(...args){
if(params.length+args.length===fn.length){
return fn(...params,...args)
}else{
return currify(fn,[...args,...params])
}
}
}
function test(a,b,c){
console.log(a,b,c)
}
currify(fn,1,2)
function currify(fn, params = []) {
return function (...args) {
if (fn.length === params.length + args.length) {
return fn(...params, ...args)
}
return currify(fn, [...params, ...args])
}
}
function maopao(arr){
for(i=arr.length; i>0; i--){
for(j=0; j < i-1;j++){
if(arr[j]>arr[j+1]){
var temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
return arr
}
maopao([23,334,54,3445,5645,24,5634,5656,23,65,43,554])
// 深拷贝
let cache = []
function deepClone(source){
let dist
const index = cache.findIndex((item) => {
return item[0] === source
})
if(index>-1){
console.log('有缓存')
dist = cache[index][1]
return dist
}
if(source instanceof Object){
if(source instanceof Array){
dist = []
}else if(source instanceof RegExp){
dist = new RegExp(source.source, source.flags)
}else if(source instanceof Date){
dist = new Date(source)
}else if(source instanceof Function){
dist = function(){
return source.apply(this, arguments)
}
}else{
dist = {}
}
cache.push([dist,source])
for(let i in source){
dist[i] = deepClone(source[i])
}
return dist
}else{
return source
}
}
function traversDom(root){
var stack = [root]
while(stack.length){
var current = stack.shift()
if(current){
parseInfo(current)
}
if(current.childNodes.length){
stack.push(root.childNodes)
}
}
}
// 前根序
const traverseRoodQian = (bTree, fn)=>{
var current = bTree
var stack = []
while(stack.length>0||current){
if(current){
fn(current.value)
stack.push(current)
current = current.left
}else{
current = stack.pop()
current = current.right
}
}
}
const traverseRoodMiddle = (bTree, fn)=>{
var current = bTree
var stack = []
while(stack.length>0||current){
if(current){
// fn(current.value)
stack.push(current)
current = current.left
}else{
current = stack.pop()
fn(current.value)
current = current.right
}
}
}
const tree = {
value:1,
left:{value:2,left:{value:4,left:null,right:null}}
}
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var insertionSortList = function(head) {
var stack = [head]
var current = head
while (current.next){
stack.push(current.next)
current = current.next
if(!current){
for(let i = 1; i <stack.length; i++){
let count = i
while(count>0){
if(stack[count].val<stack[count-1].val){
var temp = stack[count].val
stack[count].val = stack[count-1].val
stack[count-1].val = temp
}
count--
}
}
}
}
return head
};