map-anything 2.1.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,18 @@ function mapObject(target, mapFunction) {
6
6
  return carry;
7
7
  }, {});
8
8
  }
9
+ async function mapObjectAsync(target, mapFunction) {
10
+ const entries = Object.entries(target);
11
+ const promises = entries.map(async ([key, value]) => {
12
+ const newValue = await mapFunction(value, key, target);
13
+ return [key, newValue];
14
+ });
15
+ const results = await Promise.all(promises);
16
+ return results.reduce((carry, [key, value]) => {
17
+ carry[key] = value;
18
+ return carry;
19
+ }, {});
20
+ }
9
21
  function mapMap(target, mapFunction) {
10
22
  return [...target.entries()].reduce((carry, [key, value], index, array) => {
11
23
  carry.set(key, mapFunction(value, key, array));
@@ -15,3 +27,4 @@ function mapMap(target, mapFunction) {
15
27
 
16
28
  exports.mapMap = mapMap;
17
29
  exports.mapObject = mapObject;
30
+ exports.mapObjectAsync = mapObjectAsync;
@@ -4,6 +4,12 @@
4
4
  declare function mapObject<T extends Record<string | number | symbol, unknown>, MapFunction extends (value: T[keyof T], propName: keyof T, array: T[keyof T][]) => any>(target: T, mapFunction: MapFunction): {
5
5
  [key in keyof T]: ReturnType<typeof mapFunction>;
6
6
  };
7
+ /**
8
+ * Map each value of an object with provided function, just like `Array.map`
9
+ */
10
+ declare function mapObjectAsync<T extends Record<string | number | symbol, unknown>, MapFunction extends (value: T[keyof T], propName: keyof T, array: T[keyof T][]) => Promise<any>>(target: T, mapFunction: MapFunction): Promise<{
11
+ [key in keyof T]: Awaited<ReturnType<typeof mapFunction>>;
12
+ }>;
7
13
  type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never;
8
14
  type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never;
9
15
  /**
@@ -11,4 +17,4 @@ type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer
11
17
  */
12
18
  declare function mapMap<T extends Map<unknown, unknown>, MapFunction extends (value: ValueOfMap<T>, propName: KeyOfMap<T>, array: ValueOfMap<T>[]) => any>(target: T, mapFunction: MapFunction): Map<KeyOfMap<T>, ReturnType<typeof mapFunction>>;
13
19
 
14
- export { KeyOfMap, ValueOfMap, mapMap, mapObject };
20
+ export { type KeyOfMap, type ValueOfMap, mapMap, mapObject, mapObjectAsync };
package/dist/index.d.ts CHANGED
@@ -4,6 +4,12 @@
4
4
  declare function mapObject<T extends Record<string | number | symbol, unknown>, MapFunction extends (value: T[keyof T], propName: keyof T, array: T[keyof T][]) => any>(target: T, mapFunction: MapFunction): {
5
5
  [key in keyof T]: ReturnType<typeof mapFunction>;
6
6
  };
7
+ /**
8
+ * Map each value of an object with provided function, just like `Array.map`
9
+ */
10
+ declare function mapObjectAsync<T extends Record<string | number | symbol, unknown>, MapFunction extends (value: T[keyof T], propName: keyof T, array: T[keyof T][]) => Promise<any>>(target: T, mapFunction: MapFunction): Promise<{
11
+ [key in keyof T]: Awaited<ReturnType<typeof mapFunction>>;
12
+ }>;
7
13
  type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never;
8
14
  type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never;
9
15
  /**
@@ -11,4 +17,4 @@ type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer
11
17
  */
12
18
  declare function mapMap<T extends Map<unknown, unknown>, MapFunction extends (value: ValueOfMap<T>, propName: KeyOfMap<T>, array: ValueOfMap<T>[]) => any>(target: T, mapFunction: MapFunction): Map<KeyOfMap<T>, ReturnType<typeof mapFunction>>;
13
19
 
14
- export { KeyOfMap, ValueOfMap, mapMap, mapObject };
20
+ export { type KeyOfMap, type ValueOfMap, mapMap, mapObject, mapObjectAsync };
package/dist/index.js CHANGED
@@ -4,6 +4,18 @@ function mapObject(target, mapFunction) {
4
4
  return carry;
5
5
  }, {});
6
6
  }
7
+ async function mapObjectAsync(target, mapFunction) {
8
+ const entries = Object.entries(target);
9
+ const promises = entries.map(async ([key, value]) => {
10
+ const newValue = await mapFunction(value, key, target);
11
+ return [key, newValue];
12
+ });
13
+ const results = await Promise.all(promises);
14
+ return results.reduce((carry, [key, value]) => {
15
+ carry[key] = value;
16
+ return carry;
17
+ }, {});
18
+ }
7
19
  function mapMap(target, mapFunction) {
8
20
  return [...target.entries()].reduce((carry, [key, value], index, array) => {
9
21
  carry.set(key, mapFunction(value, key, array));
@@ -11,4 +23,4 @@ function mapMap(target, mapFunction) {
11
23
  }, /* @__PURE__ */ new Map());
12
24
  }
13
25
 
14
- export { mapMap, mapObject };
26
+ export { mapMap, mapObject, mapObjectAsync };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "map-anything",
3
- "version": "2.1.2",
3
+ "version": "2.2.0",
4
4
  "description": "Array.map but for objects with good TypeScript support. A small and simple integration.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -32,19 +32,19 @@
32
32
  "release": "npm run lint && del dist && npm run build && np"
33
33
  },
34
34
  "devDependencies": {
35
- "@typescript-eslint/eslint-plugin": "^5.59.2",
36
- "@typescript-eslint/parser": "^5.59.2",
37
- "del-cli": "^5.0.0",
38
- "eslint": "^8.40.0",
39
- "eslint-config-prettier": "^8.8.0",
40
- "eslint-plugin-tree-shaking": "^1.10.0",
41
- "np": "^7.7.0",
42
- "prettier": "^2.8.8",
43
- "rollup": "^3.23.0",
44
- "rollup-plugin-dts": "^5.3.0",
45
- "rollup-plugin-esbuild": "^5.0.0",
46
- "typescript": "^4.9.5",
47
- "vitest": "^0.31.0"
35
+ "@typescript-eslint/eslint-plugin": "^6.12.0",
36
+ "@typescript-eslint/parser": "^6.12.0",
37
+ "del-cli": "^5.1.0",
38
+ "eslint": "^8.54.0",
39
+ "eslint-config-prettier": "^9.0.0",
40
+ "eslint-plugin-tree-shaking": "^1.12.0",
41
+ "np": "^8.0.4",
42
+ "prettier": "^3.1.0",
43
+ "rollup": "^4.5.2",
44
+ "rollup-plugin-dts": "^6.1.0",
45
+ "rollup-plugin-esbuild": "^6.1.0",
46
+ "typescript": "^5.3.2",
47
+ "vitest": "^0.34.6"
48
48
  },
49
49
  "keywords": [
50
50
  "map-object",