Skip to content

Commit b93a6a8

Browse files
committed
refactor: tidy up
1 parent e326386 commit b93a6a8

File tree

17 files changed

+66
-46
lines changed

17 files changed

+66
-46
lines changed

src/array/quickFind.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { isDef } from '../is/isDef'
2+
import { isUndef } from '../is/isUndef'
3+
14
export function quickFind(array: any[], id: string | number) {
25
const indexMap = new Map()
36
array.forEach((item, i) => indexMap.set(item[id], i))
@@ -17,23 +20,23 @@ class QuickFind {
1720

1821
find(id: any) {
1922
const index = this.indexMap.get(id)
20-
if (index === undefined)
23+
if (isUndef(index))
2124
return undefined
2225
return this.array[index]
2326
}
2427

2528
_update(id: any, key: any, value: any) {
26-
if (key === undefined) {
29+
if (isUndef(key)) {
2730
const index = this.indexMap.get(id)
28-
if (index === undefined)
31+
if (isUndef(index))
2932
throw new Error('当前id不存在')
3033
if (value[this.id] !== id)
3134
throw new Error('不可修改唯一id')
3235
this.array[index] = value
3336
}
3437
else {
3538
const target = this.find(id)
36-
if (target === undefined)
39+
if (isUndef(target))
3740
return
3841
target[key] = value
3942
}
@@ -42,7 +45,7 @@ class QuickFind {
4245

4346
delete(id: any) {
4447
const index = this.indexMap.get(id)
45-
if (index === undefined)
48+
if (isUndef(index))
4649
return
4750
this.array.splice(index, 1)
4851
this.indexMap.delete(id)
@@ -51,17 +54,17 @@ class QuickFind {
5154

5255
set(id: any, key: any, value?: any) {
5356
const index = this.indexMap.get(id)
54-
if (value === undefined) {
55-
if (key === undefined)
57+
if (isUndef(value)) {
58+
if (isUndef(key))
5659
return
5760
value = key
5861
key = undefined
5962
}
60-
if (index !== undefined) {
63+
if (isDef(index)) {
6164
return this._update(id, key, value)
6265
}
6366
else {
64-
if (value[this.id] === undefined)
67+
if (isUndef(value[this.id]))
6568
throw new Error('新增的数据必须包含唯一id')
6669
if (value[this.id] !== id)
6770
throw new Error('新增的数据id必须与当前id一致')

src/array/sortByOrder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDef } from '../is/isDef'
2+
13
export function sortByOrder(sortArr: any[], order: string[], prop?: string) {
24
if (!order)
35
return sortArr
@@ -13,7 +15,7 @@ export function sortByOrder(sortArr: any[], order: string[], prop?: string) {
1315
sortArr.splice(index, 1)
1416
}
1517
})
16-
if (insertIndex !== undefined)
18+
if (isDef(insertIndex))
1719
result.splice(insertIndex, 0, ...sortArr)
1820
else result.concat(sortArr)
1921
return result

src/event/insertElement.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { mount } from '../utils/mount'
2+
import { isNull } from '../is/isNull'
23

34
export function insertElement(
45
parent: HTMLElement | string,
56
element: HTMLElement | string,
67
target?: HTMLElement | null,
78
): void {
8-
mount(parent, element, (parent, element) =>
9+
return mount(parent, element, (parent, element) =>
910
(parent as HTMLElement).insertBefore(
1011
element as HTMLElement,
11-
target === undefined ? (parent as HTMLElement).firstChild : target,
12+
isNull(target) ? null : target || (parent as HTMLElement).firstChild,
1213
),
1314
)
1415
}

src/is/isArray.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export function isArray(o: any): o is Array<any> {
2-
return Array.isArray(o)
3-
}
1+
export const isArray = Array.isArray

src/is/isBrowser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export const isBrowser = () => typeof window !== 'undefined'
1+
import { isDef } from './isDef'
2+
3+
export const isBrowser = () => isDef(window)

src/is/isDef.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export function isDef<T = any>(v: T | undefined): v is T {
1+
export function isDef<T = any>(v: T): v is T extends undefined ? never : T {
22
return typeof v !== 'undefined'
33
}

src/node/getExportBundle.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import fs from 'fs'
1+
import fsp from 'fs/promises'
22
import path from 'path'
33
import process from 'process'
4-
export function getExportBundle(url: string) {
4+
export async function getExportBundle(url: string) {
55
if (/[./]/.test(url))
66
throw new Error('module must be a npm module')
77
const pkg = path.resolve(process.cwd(), 'node_modules', url, 'package.json')
8-
const { module, main } = JSON.parse(fs.readFileSync(pkg, 'utf-8'))
8+
const { module, main } = JSON.parse(await fsp.readFile(pkg, 'utf-8'))
99
const modulePath = path.resolve('./node_modules/vitest', module || main)
10-
return fs.readFileSync(modulePath, 'utf-8')
10+
return fsp.readFile(modulePath, 'utf-8')
1111
}

src/node/writeFile.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs'
1+
import fsp from 'fs/promises'
22
import { isStr } from '../is/isStr'
33

44
export function writeFile(
@@ -9,11 +9,10 @@ export function writeFile(
99
if (isStr(paths))
1010
paths = [paths as string]
1111
;(paths as string[]).forEach(async (relativepath, i) => {
12-
const content = await fs.readFileSync(relativepath, encoding)
12+
const content = await fsp.readFile(relativepath, encoding)
1313
const result = callback?.(content, i) || content
14-
fs.writeFile(relativepath, result, (err) => {
15-
if (err)
16-
throw err
14+
fsp.writeFile(relativepath, result).catch((err) => {
15+
throw err
1716
})
1817
})
1918
}

src/object/deepClone.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1+
import { isFn } from '../is/isFn'
2+
import { isNull } from '../is/isNull'
3+
import { isObject } from '../is/isObject'
4+
15
const types = [Set, Map, WeakMap, WeakSet, RegExp, Date]
26
const targetMap = new WeakMap()
37
export function deepClone(target: any) {
48
if (targetMap.has(target))
59
return targetMap.get(target)
6-
if (typeof target === 'function' || target === null)
10+
if (isFn(target) || isNull(target))
711
return target
812
if (types.includes(target.constructor))
913
return new target.constructor(target)
10-
if (typeof target !== 'object')
14+
if (!isObject(target))
1115
return target
1216
const cloneObj = Object.create(
1317
Object.getPrototypeOf(target),
1418
Object.getOwnPropertyDescriptors(target),
1519
)
1620
targetMap.set(target, cloneObj)
1721
for (const key of Reflect.ownKeys(target)) {
18-
if (typeof target[key] === 'object')
22+
if (isObject(target[key]))
1923
cloneObj[key] = deepClone(target[key])
2024
else cloneObj[key] = target[key]
2125
}

src/object/deepCompare.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function deepCompare(
2828
deepCompare(value1, value2, ignoreKeys, error, errorMsg, _key)
2929
}
3030
}
31-
else if (Array.isArray(comp1) && Array.isArray(comp2)) {
31+
else if (isArray(comp1) && isArray(comp2)) {
3232
const longer = comp1.length >= comp2.length ? comp1 : comp2
3333
for (const key in longer) {
3434
const value1 = comp1[key]

0 commit comments

Comments
 (0)