Skip to content

Commit 5629ee6

Browse files
author
Tamas Kiss
committed
fix(p-map-values): Keep order of entries in pMapValue
This method is used during the pack phase in pnpm. As the key order is not guaranteed to be kept by it, the dependency order may be changed as a result, leading to non-reproducible builds. This fixes #208
1 parent 221d0b8 commit 5629ee6

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

p-map-values/__tests__/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,22 @@ import pMapValues from '../src'
33
test('pMapValues', async () => {
44
expect(await pMapValues(async (value: number) => value * 2, {a: 1, b: 2})).toEqual({a: 2, b: 4})
55
})
6+
7+
test('pMapValues - with symulated async work', async () => {
8+
const delay = async (milliseconds: number) => {
9+
return new Promise((resolve) => {
10+
setTimeout(resolve, milliseconds)
11+
});
12+
}
13+
const input = {a: 1, b: 2};
14+
15+
const mapped = await pMapValues(async (value: number) => {
16+
await delay((2 - value) * 100);
17+
return value * 2;
18+
}, input);
19+
20+
const expected= {a: 2, b: 4};
21+
22+
expect(Object.keys(mapped)).toStrictEqual(Object.keys(expected));
23+
expect(mapped).toStrictEqual(expected);
24+
})

p-map-values/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ export default async function pMapValue<K extends string | number | symbol, V, U
33
obj: Record<K, V>
44
): Promise<Record<K, U>> {
55
const result: Record<K, U> = {} as Record<K, U>
6+
const entries = Object.entries(obj) as [K, V][];
67
await Promise.all(
7-
Object.entries(obj).map(async ([key, value]: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any
8+
entries.map(async ([key, value]) => {
9+
// initialize property in the resulting object to guarantee key order
10+
result[key] = undefined as unknown as U;
811
result[key] = await mapper(value, key, obj)
912
})
1013
)

0 commit comments

Comments
 (0)