-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, {}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|