8000 Merge pull request #24 from github-learning/dev · github-learning/vue3-admin@b66ac3c · GitHub
[go: up one dir, main page]

Skip to content

Commit b66ac3c

Browse files
Merge pull request #24 from github-learning/dev
1Dev
2 parents 0c970dd + 9910bc0 commit b66ac3c

File tree

11 files changed

+238
-12
lines changed

11 files changed

+238
-12
lines changed

src/face/js/debounce.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
<script>
1212
const debounce = (fn, delay) => {
1313
let timer = null
14-
return (...args) => {
15-
if (timer) clearTimeout(timer)
14+
15+
return function (...args) {
16+
if (timer) {
17+
clearTimeout(timer)
18+
}
19+
1620
timer = setTimeout(() => {
17-
fn(...args)
18-
}, fn)
21+
fn.apply(this, args)
22+
}, delay)
1923
}
2024
}
2125

src/face/js/deepClone.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ const deepClone = (obj, cache = new WeakMap()) => {
1313
clone[key] = deepClone(obj[key], cache)
1414
}
1515
}
16+
return clone
1617
}
1718

1819
const a = { val: 1 }
1920
const b = { a }
2021
a.b = b
2122

2223
const cloned = deepClone(a)
24+
25+
console.log(
26+
'%c [ ]-24',
27+
'font-size:13px; background:pink; color:#bf2c9f;',
28+
cloned
29+
)
2330
console.log(cloned.b.a === cloned) // true ✅

src/face/js/event-loop.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ new Promise((resolve) => {
1818
console.log(7)
1919
})
2020
console.log(8)
21+
// 5, 1, 3,6,8,2,7,4

src/face/js/promise/Scheduler.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,47 @@
114114
// queue.addTask(asyncTask(i))
115115
// }
116116

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+
// }
117158
class AsyncQueue {
118159
constructor() {
119160
this.queue = []
@@ -141,8 +182,6 @@ class AsyncQueue {
141182
}
142183
}
143184
}
144-
const queue = new AsyncQueue()
145-
const taskCount = 1000
146185
const asyncFunc = (i) => {
147186
return () =>
148187
new Promise((resolve) => {
@@ -152,6 +191,8 @@ const asyncFunc = (i) => {
152191
}, 1000)
153192
})
154193
}
194+
const queue = new AsyncQueue()
195+
const taskCount = 1000
155196
for (let i = 0; i < taskCount; i++) {
156197
queue.addTask(asyncFunc(i))
157198
}

src/face/js/promise/light.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// function delay(ms) {
2+
// return new Promise((resolve) => setTimeout(resolve, ms))
3+
// }
4+
5+
// async function light(color, duration) {
6+
// console.log(`${color} 灯亮了`)
7+
// // 模拟亮灯的时间
8+
// await delay(duration)
9+
// }
10+
11+
// async function startTrafficLight() {
12+
// while (true) {
13+
// await light('🔴 红', 3000)
14+
// await light('🟡 黄', 2000)
15+
// await light('🟢 绿', 1000)
16+
// }
17+
// }
18+
19+
// startTrafficLight()
20+
21+
const delay = (ms) => {
22+
return new Promise((resolve) => {
23+
setTimeout(resolve, ms)
24+
})
25+
}
26+
async function light(color, ms) {
27+
console.log(`${color}`)
28+
await delay(ms)
29+
}
30+
async function startTrafficLight() {
31+
while (true) {
32+
await light('33', 3000)
33+
await light('22', 2000)
34+
await light('11', 1000)
35+
}
36+
}
37+
startTrafficLight()

src/face/js/slice.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fn = (arr, n) => {
2+
const result = []
3+
for (let i = 0; i < arr.length; i += n) {
4+
result.push(arr.slice(i, i + n))
5+
}
6+
return result
7+
}
8+
console.log('fn', fn([1, 2, 3, 4, 5], 2))

src/face/js/split.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var obj = { a: { b: { c: 2 } } }
2+
const get = (obj, path) => {
3+
const keys = path.split('.')
4+
let result = obj
5+
for (let key of keys) {
6+
result = result[key]
7+
}
8+
return result
9+
}
10+
11+
console.log(get(obj, 'a.b.c')) // 输出 2

src/face/js/test/tree.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const flatData = [
2+
{ id: 1, name: '根节点', parentId: null },
3+
{ id: 2, name: '子节点1', parentId: 1 },
4+
{ id: 3, name: '子节点2', parentId: 1 },
5+
{ id: 4, name: '子节点1-1', parentId: 2 },
6+
{ id: 5, name: '子节点1-2', parentId: 2 },
7+
{ id: 6, name: '子节点2-1', parentId: 3 }
8+
]
9+
const buildTree = (arr) => {
10+
const map = new Map()
11+
const tree = []
12+
arr.forEach((el) => {
13+
map.set(el.id, { ...el, children: [] })
14+
})
15+
arr.forEach((el) => {
16+
const parentId = el.parentId
17+
if (parentId === null) {
18+
tree.push(map.get(el.id))
19+
} else {
20+
const parent = map.get(parentId)
21+
parent.children.push(map.get(el.id))
22+
}
23+
})
24+
return tree
25+
}
26+
const tree = buildTree(flatData)
27+
console.log(JSON.stringify(tree, null, 2))

src/face/js/throole.html renamed to src/face/js/throttle.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</head>
1111
<body></body>
1212
<script>
13-
const throlole = (fn, delay) => {
13+
const throttle = (fn, delay) => {
1414
let lastTime = 0
1515
return function (...args) {
1616
const now = Date.now()

src/face/js/tree.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,78 @@ function flattenTree(tree, parentId = null) {
116116
// 执行拍平
117117
const flatData1 = flattenTree(nestedDataWithMap)
118118
console.log('flatData1', flatData1)
119+
120+
// var obj = {a: {b: {c: 2}}}; console.log(get(obj, 'a.b.c')); // 输出 2
121+
// const get = (obj, path) => {
122+
// let keys = path.split('.')
123+
// let result = obj
124+
// for (let key of keys) {
125+
// if (result) {
126+
// result = result[key]
127+
// } else {
128+
// return undefined
129+
// }
130+
// }
131+
// return result
132+
// }
133+
var obj = { a: { b: { c: 2 } } }
134+
135+
console.log(get(obj, 'a.b.c')) // 输出 2
136+
137+
// // 做题 2 把数组平分,实现 fn
138+
// // fn([1, 2, 3, 4, 5], 2) //
139+
const fn = (arr, count) => {
140+
let result = []
141+
for (let k = 0; k < arr.length; k += count) {
142+
result.push(arr.slice(k, k + count))
143+
}
144+
return result
145+
}
146+
147+
console.log('fn', fn([1, 2, 3, 4, 5], 2))
148+
149+
// const data = [
150+
// { id: 1, parentId: null, name: '根节点' },
151+
// { id: 2, parentId: 1, name: '节点 1' },
152+
// { id: 3, parentId: 1, name: '节点 2' },
153+
// { id: 4, parentId: 2, name: '节点 1-1' },
154+
// { id: 5, parentId: 2, name: '节点 1-2' },
155+
// { id: 6, parentId: 3, name: '节点 2-1' }
156+
// ]
157+
158+
// const buildTree = (data) => {
159+
// const tree = []
160+
// const map = new Map()
161+
// data.forEach(item => {
162+
// map.set(item.id, { ...item, children: [] })
163+
// })
164+
// data.forEach(item => {
165+
// let parentId = item.parentId
166+
// if (parentId == null) {
167+
// tree.push(map.get(item.id))
168+
// } else {
169+
// let parent = map.get(item.parentId)
170+
// parent.children.push(map.get(item.id))
171+
// }
172+
// })
173+
// return tree
174+
// }
175+
// const tree = buildTree(data)
176+
// console.log(JSON.stringify(tree, null, 2))
177+
// const flattenTree = (tree, parentId = null) => {
178+
// let flatData = []
179+
// for (node of tree) {
180+
// flatData.push({
181+
// id: node.id,
182+
// name: node.name,
183+
// parentId: node.parentId
184+
// })
185+
// }
186+
// if (node.children && node.children.length) {
187+
// flatData = flatData.concat(flattenTree(node.children, node.parentId))
188+
189+
// }
190+
// return flatData
191+
// }
192+
// const flatData = flattenTree(tree)
193+
// console.log('flatData', flatData)

0 commit comments

Comments
 (0)
0