map-anything 2.2.0 → 3.0.1

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.
package/README.md CHANGED
@@ -14,7 +14,7 @@ Array.map but for objects with good TypeScript support. A small and simple integ
14
14
  I always want to do:
15
15
 
16
16
  ```js
17
- someObject.map(val => someFunction)
17
+ someObject.map((val) => someFunction)
18
18
  ```
19
19
 
20
20
  But this doesn't exist for objects, you need to do this instead:
@@ -30,22 +30,15 @@ So I made a wrapper function for that. 😃
30
30
 
31
31
  `map-anything` has very good [#TypeScript](#typescript) support as well.
32
32
 
33
- ## Meet the family (more tiny utils with TS support)
33
+ ## Usage
34
34
 
35
- - [is-what 🙉](https://github.com/mesqueeb/is-what)
36
- - [is-where 🙈](https://github.com/mesqueeb/is-where)
37
- - [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
38
- - [check-anything 👁](https://github.com/mesqueeb/check-anything)
39
- - [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)
40
- - [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)
41
- - [map-anything 🗺](https://github.com/mesqueeb/map-anything)
42
- - [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
43
- - [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
44
- - [case-anything 🐫](https://github.com/mesqueeb/case-anything)
45
- - [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
46
- - [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)
35
+ Provided Functions:
47
36
 
48
- ## Usage
37
+ - `mapObject` takes an object and maps over the values of each key
38
+ - `mapObjectAsync` takes an object and maps a promise over the values of each key, after which you can just do a single await
39
+ - `mapMap` takes a map and maps over the values of each key
40
+
41
+ ### Basic Usage
49
42
 
50
43
  ```js
51
44
  import { mapObject } from 'map-anything'
@@ -60,6 +53,7 @@ const levelUp = mapObject(pokemon, (pkmn) => {
60
53
  return { ...pkmn, level: pkmn.level + 1 }
61
54
  })
62
55
 
56
+ // results in:
63
57
  levelUp ===
64
58
  {
65
59
  '001': { name: 'Bulbasaur', level: 11 },
@@ -75,16 +69,47 @@ A function passed to `Array.map` will get the value as first argument and an **i
75
69
  ```js
76
70
  import { mapObject } from 'map-anything'
77
71
 
72
+ const pokemon = {
73
+ '001': { name: 'Bulbasaur', level: 10 },
74
+ '004': { name: 'Charmander', level: 8 },
75
+ '007': { name: 'Squirtle', level: 11 },
76
+ }
77
+
78
78
  const addIds = mapObject(pokemon, (pkmn, propName) => {
79
79
  const id = propName
80
80
  return { ...pkmn, id }
81
81
  })
82
82
 
83
+ // results in:
83
84
  addIds ===
84
85
  {
85
- '001': { name: 'Bulbasaur', level: 10, id: '001' },
86
- '004': { name: 'Charmander', level: 8, id: '004' },
87
- '007': { name: 'Squirtle', level: 11, id: '007' },
86
+ '001': { name: 'Bulbasaur', level: 10, id: '001' },
87
+ '004': { name: 'Charmander', level: 8, id: '004' },
88
+ '007': { name: 'Squirtle', level: 11, id: '007' },
89
+ }
90
+ ```
91
+
92
+ ### Map Object Async
93
+
94
+ ```ts
95
+ const pokemon = {
96
+ '001': { name: 'Bulbasaur', level: 10 },
97
+ '004': { name: 'Charmander', level: 8 },
98
+ '007': { name: 'Squirtle', level: 11 },
99
+ }
100
+
101
+ const result = await mapObjectAsync(pokemon, async (pkmn, propName) => {
102
+ const id = propName
103
+ const data = await fetchData(id) // hypothetical API call
104
+ return { ...pkmn, data }
105
+ })
106
+
107
+ // results in:
108
+ result ===
109
+ {
110
+ '001': { name: 'Bulbasaur', level: 10, data: '...' }, // some fetched data
111
+ '004': { name: 'Charmander', level: 8, data: '...' },
112
+ '007': { name: 'Squirtle', level: 11, data: '...' },
88
113
  }
89
114
  ```
90
115
 
@@ -94,6 +119,21 @@ Without having to specify the return type in the reducer, I've set `map-anything
94
119
 
95
120
  ![typescript support](https://raw.githubusercontent.com/mesqueeb/map-anything/master/.github/typescript-support.png)
96
121
 
122
+ ## Meet the family (more tiny utils with TS support)
123
+
124
+ - [is-what 🙉](https://github.com/mesqueeb/is-what)
125
+ - [is-where 🙈](https://github.com/mesqueeb/is-where)
126
+ - [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
127
+ - [check-anything 👁](https://github.com/mesqueeb/check-anything)
128
+ - [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)
129
+ - [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)
130
+ - [map-anything 🗺](https://github.com/mesqueeb/map-anything)
131
+ - [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
132
+ - [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
133
+ - [case-anything 🐫](https://github.com/mesqueeb/case-anything)
134
+ - [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
135
+ - [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)
136
+
97
137
  ## Source code
98
138
 
99
139
  The source code is rather simple, it's doing something like the snippet show here below.
package/dist/index.d.ts CHANGED
@@ -1,20 +1,22 @@
1
1
  /**
2
2
  * Map each value of an object with provided function, just like `Array.map`
3
3
  */
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): {
4
+ export declare function mapObject<T extends {
5
+ [key in string | number | symbol]: unknown;
6
+ }, MapFunction extends (value: T[keyof T], propName: keyof T, array: T[keyof T][]) => any>(target: T, mapFunction: MapFunction): {
5
7
  [key in keyof T]: ReturnType<typeof mapFunction>;
6
8
  };
7
9
  /**
8
10
  * Map each value of an object with provided function, just like `Array.map`
9
11
  */
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<{
12
+ export declare function mapObjectAsync<T extends {
13
+ [key in string | number | symbol]: unknown;
14
+ }, MapFunction extends (value: T[keyof T], propName: keyof T, array: T[keyof T][]) => Promise<any>>(target: T, mapFunction: MapFunction): Promise<{
11
15
  [key in keyof T]: Awaited<ReturnType<typeof mapFunction>>;
12
16
  }>;
13
- type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never;
14
- type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never;
17
+ export type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never;
18
+ export type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never;
15
19
  /**
16
20
  * Map each value of a map with provided function, just like `Array.map`
17
21
  */
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>>;
19
-
20
- export { type KeyOfMap, type ValueOfMap, mapMap, mapObject, mapObjectAsync };
22
+ export 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>>;
package/dist/index.js CHANGED
@@ -1,26 +1,33 @@
1
- function mapObject(target, mapFunction) {
2
- return Object.entries(target).reduce((carry, [key, value], index, array) => {
3
- carry[key] = mapFunction(value, key, array);
4
- return carry;
5
- }, {});
1
+ /**
2
+ * Map each value of an object with provided function, just like `Array.map`
3
+ */
4
+ export function mapObject(target, mapFunction) {
5
+ return Object.entries(target).reduce((carry, [key, value], index, array) => {
6
+ carry[key] = mapFunction(value, key, array);
7
+ return carry;
8
+ }, {});
6
9
  }
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
- }, {});
10
+ /**
11
+ * Map each value of an object with provided function, just like `Array.map`
12
+ */
13
+ export async function mapObjectAsync(target, mapFunction) {
14
+ const entries = Object.entries(target);
15
+ const promises = entries.map(async ([key, value]) => {
16
+ const newValue = await mapFunction(value, key, target);
17
+ return [key, newValue];
18
+ });
19
+ const results = await Promise.all(promises);
20
+ return results.reduce((carry, [key, value]) => {
21
+ carry[key] = value;
22
+ return carry;
23
+ }, {});
18
24
  }
19
- function mapMap(target, mapFunction) {
20
- return [...target.entries()].reduce((carry, [key, value], index, array) => {
21
- carry.set(key, mapFunction(value, key, array));
22
- return carry;
23
- }, /* @__PURE__ */ new Map());
25
+ /**
26
+ * Map each value of a map with provided function, just like `Array.map`
27
+ */
28
+ export function mapMap(target, mapFunction) {
29
+ return [...target.entries()].reduce((carry, [key, value], index, array) => {
30
+ carry.set(key, mapFunction(value, key, array));
31
+ return carry;
32
+ }, new Map());
24
33
  }
25
-
26
- export { mapMap, mapObject, mapObjectAsync };
package/package.json CHANGED
@@ -1,51 +1,31 @@
1
1
  {
2
2
  "name": "map-anything",
3
- "version": "2.2.0",
3
+ "version": "3.0.1",
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,
7
- "types": "./dist/index.d.ts",
8
- "module": "./dist/index.js",
9
- "main": "./dist/index.js",
10
7
  "exports": {
11
- ".": {
12
- "require": {
13
- "types": "./dist/cjs/index.d.cts",
14
- "default": "./dist/cjs/index.cjs"
15
- },
16
- "import": {
17
- "types": "./dist/index.d.ts",
18
- "default": "./dist/index.js"
19
- }
20
- }
8
+ ".": "./dist/index.js"
21
9
  },
22
- "files": [
23
- "dist"
24
- ],
25
10
  "engines": {
26
- "node": ">=12.13"
11
+ "node": ">=18"
27
12
  },
28
13
  "scripts": {
29
14
  "test": "vitest run",
30
- "lint": "tsc --noEmit && eslint ./src --ext .ts",
31
- "build": "rollup -c ./rollup.config.js",
32
- "release": "npm run lint && del dist && npm run build && np"
15
+ "lint": "tsc --noEmit && eslint ./src",
16
+ "build": "del-cli dist && tsc",
17
+ "release": "npm run lint && npm run build && np"
33
18
  },
34
19
  "devDependencies": {
35
- "@typescript-eslint/eslint-plugin": "^6.12.0",
36
- "@typescript-eslint/parser": "^6.12.0",
37
20
  "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"
21
+ "np": "^10.0.5",
22
+ "vitest": "^1.6.0",
23
+ "@cycraft/eslint": "^0.3.0",
24
+ "@cycraft/tsconfig": "^0.1.2"
48
25
  },
26
+ "files": [
27
+ "dist"
28
+ ],
49
29
  "keywords": [
50
30
  "map-object",
51
31
  "object-map",
@@ -56,46 +36,13 @@
56
36
  "compose",
57
37
  "map-reduce"
58
38
  ],
59
- "author": "Luca Ban - Mesqueeb",
39
+ "author": "Luca Ban - Mesqueeb (https://cycraft.co)",
60
40
  "funding": "https://github.com/sponsors/mesqueeb",
61
41
  "license": "MIT",
62
- "bugs": {
63
- "url": "https://github.com/mesqueeb/map-anything/issues"
64
- },
65
- "homepage": "https://github.com/mesqueeb/map-anything#readme",
66
42
  "repository": {
67
43
  "type": "git",
68
- "url": "git+https://github.com/mesqueeb/map-anything.git"
44
+ "url": "https://github.com/mesqueeb/map-anything.git"
69
45
  },
70
- "np": {
71
- "yarn": false,
72
- "branch": "production"
73
- },
74
- "eslintConfig": {
75
- "ignorePatterns": [
76
- "node_modules",
77
- "dist",
78
- "scripts",
79
- "test"
80
- ],
81
- "root": true,
82
- "parser": "@typescript-eslint/parser",
83
- "plugins": [
84
- "@typescript-eslint",
85
- "tree-shaking"
86
- ],
87
- "extends": [
88
- "eslint:recommended",
89
- "plugin:@typescript-eslint/eslint-recommended",
90
- "plugin:@typescript-eslint/recommended",
91
- "prettier"
92
- ],
93
- "rules": {
94
- "@typescript-eslint/no-empty-function": "off",
95
- "@typescript-eslint/no-explicit-any": "off",
96
- "@typescript-eslint/ban-ts-ignore": "off",
97
- "tree-shaking/no-side-effects-in-initialization": "error",
98
- "@typescript-eslint/ban-ts-comment": "off"
99
- }
100
- }
46
+ "homepage": "https://github.com/mesqueeb/map-anything#readme",
47
+ "bugs": "https://github.com/mesqueeb/map-anything/issues"
101
48
  }
@@ -1,30 +0,0 @@
1
- 'use strict';
2
-
3
- function mapObject(target, mapFunction) {
4
- return Object.entries(target).reduce((carry, [key, value], index, array) => {
5
- carry[key] = mapFunction(value, key, array);
6
- return carry;
7
- }, {});
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
- }
21
- function mapMap(target, mapFunction) {
22
- return [...target.entries()].reduce((carry, [key, value], index, array) => {
23
- carry.set(key, mapFunction(value, key, array));
24
- return carry;
25
- }, /* @__PURE__ */ new Map());
26
- }
27
-
28
- exports.mapMap = mapMap;
29
- exports.mapObject = mapObject;
30
- exports.mapObjectAsync = mapObjectAsync;
@@ -1,20 +0,0 @@
1
- /**
2
- * Map each value of an object with provided function, just like `Array.map`
3
- */
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
- [key in keyof T]: ReturnType<typeof mapFunction>;
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
- }>;
13
- type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never;
14
- type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never;
15
- /**
16
- * Map each value of a map with provided function, just like `Array.map`
17
- */
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>>;
19
-
20
- export { type KeyOfMap, type ValueOfMap, mapMap, mapObject, mapObjectAsync };