Skip to content

Commit d31aa36

Browse files
author
IceyWu
committed
feat: [toPro] 新增toPro方法
1 parent 2bbe9cc commit d31aa36

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export * from './tools'
77
export * from './lodash-lite'
88
export * from './download'
99
export * from './to'
10+
export * from './to/toPro'
1011
export * from './log'
1112
// export * from './browser'

src/to/toPro.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { getObjVal } from '../object'
2+
import { isArray } from '../is'
3+
import { to } from './index'
4+
5+
interface ValItem {
6+
keys: string[]
7+
valFormat?: any
8+
}
9+
10+
export async function toPro(promise: Promise<T>, valList?: ValItem[]) {
11+
const [err, res] = await to(promise)
12+
if (err)
13+
return [err, undefined]
14+
15+
if (isArray(valList)) {
16+
const resObj = res as any
17+
const dataList = [] as any
18+
valList.forEach(({ keys, valFormat }) => {
19+
const valList = keys.map((key) => {
20+
return getObjVal(resObj, key)
21+
})
22+
const tempVal = valFormat ? valFormat(valList) : valList
23+
if (isArray(tempVal) && tempVal.length === 1 && !valFormat)
24+
dataList.push(tempVal[0])
25+
else
26+
dataList.push(tempVal)
27+
})
28+
return [undefined, dataList]
29+
}
30+
else {
31+
return [undefined, res]
32+
}
33+
}
34+
35+
export default {
36+
toPro,
37+
}

0 commit comments

Comments
 (0)