8000 fix: add · github-learning/vue3-admin@0c970dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c970dd

Browse files
author
zjb
committed
fix: add
1 parent f0fdec4 commit 0c970dd

File tree

7 files changed

+327
-27
lines changed

7 files changed

+327
-27
lines changed

index.js

Whitespace-only changes.

src/face/js/debounce.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<input id="inputEl" />
10+
</body>
11+
<script>
12+
const debounce = (fn, delay) => {
13+
let timer = null
14+
return (...args) => {
15+
if (timer) clearTimeout(timer)
16+
timer = setTimeout(() => {
17+
fn(...args)
18+
}, fn)
19+
}
20+
}
21+
22+
const handleSearch = debounce((val) => {
23+
console.log('搜索关键词:', val)
24+
}, 500)
25+
26+
// 输入框绑定
27+
inputEl.addEventListener('input', (e) => {
28+
handleSearch(e.target.value)
29+
})
30+
</script>
31+
</html>

src/face/js/deepClone.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 定义一个深度克隆函数,接受两个参数:要克隆的对象和缓存对象
2+
const deepClone = (obj, cache = new WeakMap()) => {
3+
if (typeof obj !== 'object' || obj === null) return obj
4+
5+
if (cache.has(obj)) return cache.get(obj)
6+
7+
const clone = Array.isArray(obj) ? [] : {}
8+
9+
cache.set(obj, clone)
10+
11+
for (const key in obj) {
12+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
13+
clone[key] = deepClone(obj[key], cache)
14+
}
15+
}
16+
}
17+
18+
const a = { val: 1 }
19+
const b = { a }
20+
a.b = b
21+
22+
const cloned = deepClone(a)
23+
console.log(cloned.b.a === cloned) // true ✅

src/face/js/promise/Scheduler.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// // 异步任务,一个一个执行
2+
// /**
3+
// * 1. 将任务放到队列,
4+
// */
5+
// // const asyncTask = (i) => {
6+
// // return () =>
7+
// // new Promise((resolve) => {
8+
// // setTimeout(() => {
9+
// // console.log(`${i}`)
10+
// // resolve()
11+
// // }, 1000)
12+
// // })
13+
// // }
14+
15+
// class AsyncTaskQueue {
16+
// constructor() {
17+
// this.queue = []
18+
// this.runing = false
19+
// }
20+
// addTask(task) {
21+
// this.queue.push(task)
22+
// if (!this.runing) {
23+
// this.runTask()
24+
// }
25+
// }
26+
// async runTask() {
27+
// if (this.queue.length === 0) {
28+
// this.runing = true
29+
// return
30+
// }
31+
// const task = this.queue.shift()
32+
// this.runing = true
33+
// try {
34+
// await task()
35+
// this.runTask()
36+
// } catch {
37+
// console.log('err')
38+
// }
39+
// }
40+
// }
41+
// const asyncTask = (i) => {
42+
// return () =>
43+
// new Promise((resolve) => {
44+
// setTimeout(() => {
45+
// console.log(`${i}`)
46+
// resolve()
47+
// }, 1000)
48+
// })
49+
// }
50+
// const askCount = 1000
51+
// const queue = new AsyncTaskQueue()
52+
// for (let i = 0; i < askCount; i++) {
53+
// queue.addTask(asyncTask(i))
54+
// }
55+
56+
// class AsyncTaskQueue {
57+
// constructor(concurrency) {
58+
// this.queue = []
59+
// this.running = 0
60+
// this.concurrency = concurrency
61+
// }
62+
63+
// addTask(task) {
64+
// return new Promise((resolve, reject) => {
65+
// const taskWrapper = async () => {
66+
// this.running++
67+
68+
// console.log(
69+
// '%c [ ]-68',
70+
// 'font-size:13px; background:pink; color:#bf2c9f;',
71+
// this.running
72+
// )
73+
// try {
74+
// const result = await task()
75+
// resolve(result)
76+
// } catch (error) {
77+
// reject(error)
78+
// } finally {
79+
// this.running--
80+
// this.runNext()
81+
// }
82+
// }
83+
84+
// if (this.running < this.concurrency) {
85+
// taskWrapper()
86+
// } else {
87+
// this.queue.push(taskWrapper)
88+
// }
89+
// })
90+
// }
91+
92+
// runNext() {
93+
// if (this.queue.length > 0 && this.running < this.concurrency) {
94+
// const nextTask = this.queue.shift()
95+
// nextTask()
96+
// }
97+
// }
98+
// }
99+
100+
// const asyncTask = (i) => {
101+
// return () =>
102+
// new Promise((resolve) => {
103+
// setTimeout(() => {
104+
// console.log(`${i}`)
105+
// resolve()
106+
// }, 1000)
107+
// })
108+
// }
109+
110+
// const concurrency = 5 // 这里设置最大并发数为 5
111+
// const askCount = 1000
112+
// const queue = new AsyncTaskQueue(concurrency)
113+
// for (let i = 0; i < askCount; i++) {
114+
// queue.addTask(asyncTask(i))
115+
// }
116+
117+
class AsyncQueue {
118+
constructor() {
119+
this.queue = []
120+
this.runing = false
121+
}
122+
addTask(task) {
123+
this.queue.push(task)
124+
if (!this.runing) {
125+
this.runTask()
126+
}
127+
}
128+
async runTask() {
129+
if (this.queue.length === 0) {
130+
this.runing = false
131+
return
132+
}
133+
const task = this.queue.shift()
134+
this.runing = true
135+
try {
136+
await task()
137+
this.runTask()
138+
} catch {
139+
console.log('err')
140+
this.runing = true
141+
}
142+
}
143+
}
144+
const queue = new AsyncQueue()
145+
const taskCount = 1000
146+
const asyncFunc = (i) => {
147+
return () =>
148+
new Promise((resolve) => {
149+
setTimeout(() => {
150+
console.log(`${i}`)
151+
resolve()
15 F438 2+
}, 1000)
153+
})
154+
}
155+
for (let i = 0; i < taskCount; i++) {
156+
queue.addTask(asyncFunc(i))
157+
}

src/face/js/sum.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1+
// const nums = [2, 3, 5, 7, 8, 9]
2+
// const target = 11
3+
// // 定义一个名为 twoSum 的函数,接收两个参数:一个数组 arr 和一个目标值 target
4+
// function twoSum(arr, target) {
5+
// // 使用 for 循环遍历数组
6+
// for (let i = 0; i < arr.length; i++) {
7+
// // 使用 for 循环遍历数组,从 i+1 开始
8+
// for (let j = i + 1; j < arr.length; j++) {
9+
// // 如果当前两个数的和等于目标值,则返回这两个数的索引
10+
// if (arr[i] + arr[j] === target) {
11+
// return [i, j]
12+
// }
13+
// }
14+
// }
15+
// }
16+
17+
// console.log(
18+
// '%c [ ]-17',
19+
// 'font-size:13px; background:pink; color:#bf2c9f;',
20+
// twoSum(nums, target)
21+
// )
22+
123
const nums = [2, 3, 5, 7, 8, 9]
224
const target = 11
3-
// 定义一个名为 twoSum 的函数,接收两个参数:一个数组 arr 和一个目标值 target
4-
function twoSum(arr, target) {
5-
// 使用 for 循环遍历数组
6-
for (let i = 0; i < arr.length; i++) {
7-
// 使用 for 循环遍历数组,从 i+1 开始
8-
for (let j = i + 1; j < arr.length; j++) {
9-
// 如果当前两个数的和等于目标值,则返回这两个数的索引
10-
if (arr[i] + arr[j] === target) {
11-
return [i, j]
12-
}
25+
const twoSum = (nums, target) => {
26+
const map = new Map()
27+
for (let i = 0; i < nums.length; i++) {
28+
const diff = target - nums[i]
29+
if (map.get(diff)) {
30+
return [map.get(diff), i]
1331
}
32+
map.set(nums[i], i)
1433
}
34+
return []
1535
}
16-
1736
console.log(
1837
'%c [ ]-17',
1938
'font-size:13px; background:pink; color:#bf2c9f;',

src/face/js/throole.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>
7+
防抖 / 节流 / n 个最大数组之和 / 括号匹配的检验js版 / 返回下标 /
8+
异步任务,单个输出 / tree / flat / 异步执行顺序
9+
</title>
10+
</head>
11+
<body></body>
12+
<script>
13+
const throlole = (fn, delay) => {
14+
let lastTime = 0
15+
return function (...args) {
16+
const now = Date.now()
17+
if (now - lastTime > delay) {
18+
lastTime = now
19+
fn.apply(this, args)
20+
}
21+
}
22+
}
23+
const handleScroll = throttle(() => {
24+
console.log('滚动事件')
25+
}, 200)
26+
27+
window.addEventListener('scroll', handleScroll)
28+
</script>
29+
</html>

src/face/js/tree.js

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,37 @@ const flatData = [
2525
// const nestedDataWithMap = buildNestedStructureWithMap(flatData)
2626
// console.log(JSON.stringify(nestedDataWithMap, null, 2))
2727
function buildNestedStructureWithMap(arr) {
28+
// const map = new Map()
29+
// const result = []
30+
// arr.forEach((el) => {
31+
// map.set(el.id, { ...el, children: [] })
32+
// })
33+
// arr.forEach((el) => {
34+
// if (el.parentId == null) {
35+
// result.push(map.get(el.id))
36+
// } else {
37+
// map.get(el.parentId).children.push(map.get(el.id))
38+
// }
39+
// })
40+
// return result
2841
const map = new Map()
29-
const result = []
42+
const tree = []
3043
arr.forEach((el) => {
31-
map.set(el.id, { ...el, children: [] })
44+
map.set(el.id, {
45+
...el,
46+
children: []
47+
})
3248
})
3349
arr.forEach((el) => {
34-
if (el.parentId == null) {
35-
result.push(map.get(el.id))
50+
let parentId = el.parentId
51+
if (parentId === null) {
52+
tree.push(map.get(el.id))
3653
} else {
37-
map.get(el.parentId).children.push(map.get(el.id))
54+
let parent = map.get(el.parentId)
55+
parent.children.push(map.get(el.id))
3856
}
3957
})
40-
return result
58+
return tree
4159
}
4260
const nestedDataWithMap = buildNestedStructureWithMap(flatData)
4361
console.log(JSON.stringify(nestedDataWithMap, null, 2))
@@ -59,17 +77,40 @@ console.log(JSON.stringify(nestedDataWithMap, null, 2))
5977

6078
// return result;
6179
// }
62-
function flattenTree(tree) {
63-
const result = []
64-
tree.forEach((node) => {
65-
const { children, ...rest } = node
66-
result.push(rest)
67-
console.log('rest', rest)
68-
if (children && children.length) {
69-
console.log('children', children)
70-
result.push(...flattenTree(children))
80+
function flattenTree(tree, parentId = null) {
81+
// const result = []
82+
// tree.forEach((node) => {
83+
// const { children, ...rest } = node
84+
// result.push(rest)
85+
// console.log('rest', rest)
86+
// if (children && children.length) {
87+
// console.log('children', children)
88+
// result.push(...flattenTree(children))
89+
// }
90+
// })
91+
let result = []
92+
for (let node of tree) {
93+
result.push({
94+
id: node.id,
95+
name: node.name,
96+
parentId: parentId
97+
})
98+
if (node.children && node.children.length) {
99+
result = result.concat(flattenTree(node.children, node.id))
71100
}
72-
})
101+
}
102+
// let result = []
103+
// for (let node of tree) {
104+
// result.push({
105+
// id: node.id,
106+
// name: node.name,
107+
// parentId: node.parentId
108+
// })
109+
// if (node.children && node.children.length) {
110+
// result = result.concat(flattenTree(node.children, node.parentId))
111+
// }
112+
// }
113+
return result
73114
}
74115

75116
// 执行拍平

0 commit comments

Comments
 (0)
0