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

Skip to content

Commit da58653

Browse files
fix: merge
2 parents 0f26ef0 + 0c970dd commit da58653

File tree

12 files changed

+809
-16
lines changed

12 files changed

+809
-16
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()
152+
}, 1000)
153+
})
154+
}
155+
for (let i = 0; i < taskCount; i++) {
156+
queue.addTask(asyncFunc(i))
157+
}

src/face/js/sort.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
class AsyncTaskQueue {
2+
constructor() {
3+
this.queue = []
4+
this.runing = false
5+
}
6+
addTask(task) {
7+
this.queue.push(task)
8+
if (!this.runing) {
9+
this.runTask()
10+
}
11+
}
12+
async runTask() {
13+
if (this.queue.length === 0) {
14+
this.runing = false
15+
return
16+
}
17+
const task = this.queue.shift()
18+
this.runing = true
19+
try {
20+
await task()
21+
this.runTask()
22+
} catch (error) {
23+
console.log(
24+
'%c [ ]-69',
25+
'font-size:13px; background:pink; color:#bf2c9f;',
26+
error
27+
)
28+
this.runing = false
29+
}
30+
}
31+
}
32+
// const queue = new AsyncTaskQueue()
33+
// const taskNum = 100
34+
// const taskFunction = (i) => {
35+
// return () =>
36+
// new Promise((resolve) => {
37+
// setTimeout(() => {
38+
// console.log(`${i}`)
39+
// resolve()
40+
// }, 1000)
41+
// })
42+
// }
43+
// for (let i = 0; i < taskNum; i++) {
44+
// queue.addTask(taskFunction(i))
45+
// }
46+
const nums = [2, 3, 5, 7, 8, 9]
47+
const target = 11
48+
const twoSum = (arr, target) => {
49+
const map = new Map()
50+
for (let i = 0; i < arr.length; i++) {
51+
let diff = target - arr[i]
52+
if (map.get(diff)) {
53+
return [map.get(diff), i]
54+
}
55+
map.set(arr[i], i)
56+
}
57+
return []
58+
}
59+
60+
console.log(
61+
'%c [ ]-103',
62+
'font-size:13px; background:pink; color:#bf2c9f;',
63+
twoSum(nums, target)
64+
)
65+
66+
const dataTree = [
67+
{ id: 1, parentId: null, name: '根节点' },
68+
{ id: 2, parentId: 1, name: '节点 1' },
69+
{ id: 3, parentId: 1, name: '节点 2' },
70+
{ id: 4, parentId: 2, name: '节点 1-1' },
71+
{ id: 5, parentId: 2, name: '节点 1-2' },
72+
{ id: 6, parentId: 3, name: '节点 2-1' }
73+
]
74+
const buildTree = (data) => {
75+
const tree = []
76+
const map = new Map()
77+
data.forEach((element) => {
78+
map.set(element.id, { ...element, children: [] })
79+
})
80+
data.forEach((item) => {
81+
let parentId = item.parentId
82+
if (parentId === null) {
83+
tree.push(map.get(item.id))
84+
} else {
85+
let parent = map.get(item.parentId)
86+
parent.children.push(map.get(item.id))
87+
}
88+
})
89+
return tree
90+
}
91+
92+
const tree = buildTree(dataTree)
93+
94+
console.log(
95+
'%c [ ]-124',
96+
'font-size:13px; background:pink; color:#bf2c9f;',
97+
JSON.stringify(tree, null, 2)
98+
)
99+
100+
const flattenTree = (tree, parentId = null) => {
101+
let result = []
102+
for (let node of tree) {
103+
result.push({
104+
id: node.id,
105+
parentId: node.parentId,
106+
name: node.name
107+
})
108+
result = result.concat(flattenTree(node.children, parentId))
109+
}
110+
return result
111+
}
112+
113+
console.log(
114+
'%c [ ]-155',
115+
'font-size:13px; background:pink; color:#bf2c9f;',
116+
flattenTree(tree)
117+
)

src/face/js/sum.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1-
// 给定 nums = [2, 3, 5, 9], target = 7
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+
// }
216

3-
// 因为 nums[0] + nums[3] = 2 + 5 = 7
4-
// 所以返回 [0, 2]
17+
// console.log(
18+
// '%c [ ]-17',
19+
// 'font-size:13px; background:pink; color:#bf2c9f;',
20+
// twoSum(nums, target)
21+
// )
522

23+
const nums = [2, 3, 5, 7, 8, 9]
24+
const target = 11
625
const twoSum = (nums, target) => {
726
const map = new Map()
827
for (let i = 0; i < nums.length; i++) {
9-
const item = nums[i]
10-
const diff = target - item
11-
if (map.has(diff)) {
28+
const diff = target - nums[i]
29+
if (map.get(diff)) {
1230
return [map.get(diff), i]
13-
} else {
14-
map.set(item, i)
1531
}
32+
map.set(nums[i], i)
1633
}
1734
return []
1835
}
19-
console.log(twoSum([2, 3, 5, 9], 7))
36+
console.log(
37+
'%c [ ]-17',
38+
'font-size:13px; background:pink; color:#bf2c9f;',
39+
twoSum(nums, target)
40+
)

0 commit comments

Comments
 (0)
0