Skip to content

Commit

Permalink
feat(utils): 新增 prepareData 准备数据
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 8, 2022
1 parent cd566f6 commit f4c5045
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
"tree-tool": "1.1.8",
"ts-essentials": "7.0.3",
"type-fest": "2.7.0",
"typescript": "4.4.2",
"typescript": "^4.6.2",
"typescript-snapshots-plugin": "1.7.0",
"vscode-generate-index-standalone": "1.5.3",
"yargs": "17.1.1"
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export * from './pascalCase'
export * from './pickStrict'
export * from './placeKitten'
export * from './pMap'
export * from './prepareData'
export * from './readFile'
export * from './RichUrl'
export * from './rot13'
Expand Down
20 changes: 20 additions & 0 deletions src/utils/prepareData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expectType } from '../dev'
import { prepareData } from './prepareData'

describe('prepareData', () => {
test('ok', async () => {
const data = await prepareData({
x: 1,
y: Promise.resolve(Promise.resolve('y')),
z: null,
})
expectType<typeof data['x'], number>()
expectType<typeof data['y'], string>()
expectType<typeof data['z'], null>()
expect(data).toEqual({
x: 1,
y: 'y',
z: null,
})
})
})
27 changes: 27 additions & 0 deletions src/utils/prepareData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { isPromiseLike } from './isPromiseLike'

/**
* 准备数据。
*
* @param getter 数据源
*/
export function prepareData<T extends Record<string, any>>(
getter: T,
): Promise<
{
[K in keyof T]: Awaited<T[K]>
}
> {
return Promise.all<[string, any]>(
Object.keys(getter).map(key =>
isPromiseLike(getter[key])
? getter[key].then((value: any) => [key, value])
: [key, getter[key]],
),
).then<any>(list =>
list.reduce<any>((res, item) => {
res[item[0]] = item[1]
return res
}, {}),
)
}
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9155,11 +9155,16 @@ [email protected]:
resolved "https://registry.nlark.com/typescript-snapshots-plugin/download/typescript-snapshots-plugin-1.7.0.tgz#14c7699381e2244a37e4273a68fd11fd2bb480a9"
integrity sha1-FMdpk4HiJEo35Cc6aP0R/Su0gKk=

typescript@4.4.2, typescript@^4.3.5, typescript@~4.3.5:
typescript@^4.3.5, typescript@~4.3.5:
version "4.4.2"
resolved "https://registry.nlark.com/typescript/download/typescript-4.4.2.tgz#6d618640d430e3569a1dfb44f7d7e600ced3ee86"
integrity sha1-bWGGQNQw41aaHftE99fmAM7T7oY=

typescript@^4.6.2:
version "4.6.2"
resolved "https://registry.npmmirror.com/typescript/-/typescript-4.6.2.tgz#fe12d2727b708f4eef40f51598b3398baa9611d4"
integrity sha512-HM/hFigTBHZhLXshn9sN37H085+hQGeJHJ/X7LpBWLID/fbc2acUMfU+lGD98X81sKP+pFa9f0DZmCwB9GnbAg==

uglify-js@^3.1.4:
version "3.14.1"
resolved "https://registry.nlark.com/uglify-js/download/uglify-js-3.14.1.tgz?cache=0&sync_timestamp=1627379850568&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fuglify-js%2Fdownload%2Fuglify-js-3.14.1.tgz#e2cb9fe34db9cb4cf7e35d1d26dfea28e09a7d06"
Expand Down

0 comments on commit f4c5045

Please sign in to comment.