map-anything 1.0.2 → 1.0.6
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 +6 -3
- package/dist/{index.cjs.js → index.cjs} +1 -2
- package/dist/{index.esm.js → index.es.js} +1 -2
- package/{types → dist/types}/index.d.ts +0 -0
- package/package.json +47 -33
- package/.eslintignore +0 -9
- package/.github/FUNDING.yml +0 -12
- package/.github/typescript-support.png +0 -0
- package/.prettierrc +0 -9
- package/build/rollup.js +0 -59
- package/src/index.ts +0 -17
- package/test/index.ts +0 -89
- package/tsconfig.json +0 -14
package/README.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# Map anything 🗺
|
|
2
2
|
|
|
3
|
+
<a href="https://www.npmjs.com/package/map-anything"><img src="https://img.shields.io/npm/v/map-anything.svg" alt="Total Downloads"></a>
|
|
4
|
+
<a href="https://www.npmjs.com/package/map-anything"><img src="https://img.shields.io/npm/dw/map-anything.svg" alt="Latest Stable Version"></a>
|
|
5
|
+
|
|
3
6
|
```
|
|
4
7
|
npm i map-anything
|
|
5
8
|
```
|
|
6
9
|
|
|
7
|
-
Array.map but for objects. A small and simple integration.
|
|
10
|
+
Array.map but for objects with good TypeScript support. A small and simple integration.
|
|
8
11
|
|
|
9
12
|
## Motivation
|
|
10
13
|
|
|
@@ -25,7 +28,7 @@ Object.entries(someObject).reduce((carry, [key, value], index, array) => {
|
|
|
25
28
|
|
|
26
29
|
So I made a wrapper function for that. 😃
|
|
27
30
|
|
|
28
|
-
`map-anything` has very good #
|
|
31
|
+
`map-anything` has very good [#TypeScript](#typescript) support as well.
|
|
29
32
|
|
|
30
33
|
## Meet the family
|
|
31
34
|
|
|
@@ -84,7 +87,7 @@ addIds ===
|
|
|
84
87
|
|
|
85
88
|
Without having to specify the return type in the reducer, I've set `map-anything` up so it automatically detects that type for you!
|
|
86
89
|
|
|
87
|
-

|
|
88
91
|
|
|
89
92
|
## Source code
|
|
90
93
|
|
|
@@ -11,8 +11,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
11
11
|
* @returns {Record<string, any>}
|
|
12
12
|
*/
|
|
13
13
|
function mapObject(target, mapFunction) {
|
|
14
|
-
return Object.entries(target).reduce(
|
|
15
|
-
var key = _a[0], value = _a[1];
|
|
14
|
+
return Object.entries(target).reduce((carry, [key, value], index, array) => {
|
|
16
15
|
carry[key] = mapFunction(value, key, array);
|
|
17
16
|
return carry;
|
|
18
17
|
}, {});
|
|
@@ -7,8 +7,7 @@
|
|
|
7
7
|
* @returns {Record<string, any>}
|
|
8
8
|
*/
|
|
9
9
|
function mapObject(target, mapFunction) {
|
|
10
|
-
return Object.entries(target).reduce(
|
|
11
|
-
var key = _a[0], value = _a[1];
|
|
10
|
+
return Object.entries(target).reduce((carry, [key, value], index, array) => {
|
|
12
11
|
carry[key] = mapFunction(value, key, array);
|
|
13
12
|
return carry;
|
|
14
13
|
}, {});
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,37 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "map-anything",
|
|
3
3
|
"sideEffects": false,
|
|
4
|
-
"version": "1.0.
|
|
5
|
-
"description": "Array.map but for objects. A small and simple integration.",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"types": "types/index.d.ts",
|
|
4
|
+
"version": "1.0.6",
|
|
5
|
+
"description": "Array.map but for objects with good TypeScript support. A small and simple integration.",
|
|
6
|
+
"module": "./dist/index.es.js",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.es.js",
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"types": "./dist/types/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
9
19
|
"scripts": {
|
|
10
|
-
"test": "
|
|
11
|
-
"lint": "eslint
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"release": "npm run build && np"
|
|
20
|
+
"test": "vitest run",
|
|
21
|
+
"lint": "tsc --noEmit && eslint ./src --ext .ts",
|
|
22
|
+
"build": "rollup -c ./scripts/build.js",
|
|
23
|
+
"release": "npm run lint && del dist && npm run build && np"
|
|
15
24
|
},
|
|
16
25
|
"devDependencies": {
|
|
17
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
18
|
-
"@typescript-eslint/parser": "^
|
|
19
|
-
"
|
|
20
|
-
"eslint": "^7.
|
|
21
|
-
"eslint-config-prettier": "^8.
|
|
22
|
-
"eslint-plugin-tree-shaking": "^1.
|
|
23
|
-
"np": "^7.
|
|
24
|
-
"prettier": "^2.
|
|
25
|
-
"rollup": "^2.
|
|
26
|
-
"rollup-plugin-typescript2": "^0.
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"typescript": "^4.2.3"
|
|
26
|
+
"@typescript-eslint/eslint-plugin": "^5.10.1",
|
|
27
|
+
"@typescript-eslint/parser": "^5.10.1",
|
|
28
|
+
"del-cli": "^4.0.1",
|
|
29
|
+
"eslint": "^8.7.0",
|
|
30
|
+
"eslint-config-prettier": "^8.3.0",
|
|
31
|
+
"eslint-plugin-tree-shaking": "^1.10.0",
|
|
32
|
+
"np": "^7.6.0",
|
|
33
|
+
"prettier": "^2.5.1",
|
|
34
|
+
"rollup": "^2.66.0",
|
|
35
|
+
"rollup-plugin-typescript2": "^0.31.1",
|
|
36
|
+
"typescript": "^4.5.5",
|
|
37
|
+
"vitest": "^0.2.1"
|
|
30
38
|
},
|
|
31
39
|
"keywords": [
|
|
32
|
-
"map-object"
|
|
40
|
+
"map-object",
|
|
41
|
+
"object-map",
|
|
42
|
+
"object-to-object",
|
|
43
|
+
"mapping",
|
|
44
|
+
"transform",
|
|
45
|
+
"object-mapper",
|
|
46
|
+
"compose",
|
|
47
|
+
"map-reduce"
|
|
33
48
|
],
|
|
34
49
|
"author": "Luca Ban - Mesqueeb",
|
|
50
|
+
"funding": "https://github.com/sponsors/mesqueeb",
|
|
35
51
|
"license": "MIT",
|
|
36
52
|
"bugs": {
|
|
37
53
|
"url": "https://github.com/mesqueeb/map-anything/issues"
|
|
@@ -41,20 +57,17 @@
|
|
|
41
57
|
"type": "git",
|
|
42
58
|
"url": "git+https://github.com/mesqueeb/map-anything.git"
|
|
43
59
|
},
|
|
44
|
-
"ava": {
|
|
45
|
-
"extensions": [
|
|
46
|
-
"ts"
|
|
47
|
-
],
|
|
48
|
-
"require": [
|
|
49
|
-
"tsconfig-paths/register",
|
|
50
|
-
"ts-node/register"
|
|
51
|
-
]
|
|
52
|
-
},
|
|
53
60
|
"np": {
|
|
54
61
|
"yarn": false,
|
|
55
62
|
"branch": "production"
|
|
56
63
|
},
|
|
57
64
|
"eslintConfig": {
|
|
65
|
+
"ignorePatterns": [
|
|
66
|
+
"node_modules",
|
|
67
|
+
"dist",
|
|
68
|
+
"scripts",
|
|
69
|
+
"test"
|
|
70
|
+
],
|
|
58
71
|
"root": true,
|
|
59
72
|
"parser": "@typescript-eslint/parser",
|
|
60
73
|
"plugins": [
|
|
@@ -68,6 +81,7 @@
|
|
|
68
81
|
"prettier"
|
|
69
82
|
],
|
|
70
83
|
"rules": {
|
|
84
|
+
"@typescript-eslint/no-empty-function": "off",
|
|
71
85
|
"@typescript-eslint/no-explicit-any": "off",
|
|
72
86
|
"@typescript-eslint/ban-ts-ignore": "off",
|
|
73
87
|
"tree-shaking/no-side-effects-in-initialization": "error",
|
package/.eslintignore
DELETED
package/.github/FUNDING.yml
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
# These are supported funding model platforms
|
|
2
|
-
|
|
3
|
-
github: mesqueeb
|
|
4
|
-
patreon: # Replace with a single Patreon username
|
|
5
|
-
open_collective: # Replace with a single Open Collective username
|
|
6
|
-
ko_fi: # Replace with a single Ko-fi username
|
|
7
|
-
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
|
8
|
-
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
9
|
-
liberapay: # Replace with a single Liberapay username
|
|
10
|
-
issuehunt: # Replace with a single IssueHunt username
|
|
11
|
-
otechie: # Replace with a single Otechie username
|
|
12
|
-
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
|
Binary file
|
package/.prettierrc
DELETED
package/build/rollup.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
|
|
3
|
-
// npm install rollup-plugin-typescript2 typescript --save-dev
|
|
4
|
-
import typescript from 'rollup-plugin-typescript2'
|
|
5
|
-
// import { terser } from 'rollup-plugin-terser'
|
|
6
|
-
// import resolve from 'rollup-plugin-node-resolve'
|
|
7
|
-
|
|
8
|
-
// ------------------------------------------------------------------------------------------
|
|
9
|
-
// formats
|
|
10
|
-
// ------------------------------------------------------------------------------------------
|
|
11
|
-
// amd – Asynchronous Module Definition, used with module loaders like RequireJS
|
|
12
|
-
// cjs – CommonJS, suitable for Node and Browserify/Webpack
|
|
13
|
-
// esm – Keep the bundle as an ES module file
|
|
14
|
-
// iife – A self-executing function, suitable for inclusion as a <script> tag. (If you want to create a bundle for your application, you probably want to use this, because it leads to smaller file sizes.)
|
|
15
|
-
// umd – Universal Module Definition, works as amd, cjs and iife all in one
|
|
16
|
-
// system – Native format of the SystemJS loader
|
|
17
|
-
|
|
18
|
-
// ------------------------------------------------------------------------------------------
|
|
19
|
-
// setup
|
|
20
|
-
// ------------------------------------------------------------------------------------------
|
|
21
|
-
const pkg = require('../package.json')
|
|
22
|
-
const name = pkg.name
|
|
23
|
-
const className = name.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase())
|
|
24
|
-
const external = Object.keys(pkg.dependencies || [])
|
|
25
|
-
const plugins = [
|
|
26
|
-
typescript({ useTsconfigDeclarationDir: true, tsconfigOverride: { exclude: ['test/**/*'] } }),
|
|
27
|
-
]
|
|
28
|
-
|
|
29
|
-
// ------------------------------------------------------------------------------------------
|
|
30
|
-
// Builds
|
|
31
|
-
// ------------------------------------------------------------------------------------------
|
|
32
|
-
function defaults (config) {
|
|
33
|
-
// defaults
|
|
34
|
-
const defaults = {
|
|
35
|
-
plugins,
|
|
36
|
-
external,
|
|
37
|
-
}
|
|
38
|
-
// defaults.output
|
|
39
|
-
config.output = config.output.map(output => {
|
|
40
|
-
return Object.assign(
|
|
41
|
-
{
|
|
42
|
-
sourcemap: false,
|
|
43
|
-
name: className,
|
|
44
|
-
},
|
|
45
|
-
output
|
|
46
|
-
)
|
|
47
|
-
})
|
|
48
|
-
return Object.assign(defaults, config)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export default [
|
|
52
|
-
defaults({
|
|
53
|
-
input: 'src/index.ts',
|
|
54
|
-
output: [
|
|
55
|
-
{ file: 'dist/index.cjs.js', format: 'cjs' },
|
|
56
|
-
{ file: 'dist/index.esm.js', format: 'esm' },
|
|
57
|
-
],
|
|
58
|
-
}),
|
|
59
|
-
]
|
package/src/index.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Map each value of an object with provided function, just like `Array.map`
|
|
3
|
-
*
|
|
4
|
-
* @template T
|
|
5
|
-
* @param {T} target
|
|
6
|
-
* @param {(value: T, propName: keyof T, array: T[keyof T][]) => any} mapFunction
|
|
7
|
-
* @returns {Record<string, any>}
|
|
8
|
-
*/
|
|
9
|
-
export function mapObject<
|
|
10
|
-
T extends Record<string, any>,
|
|
11
|
-
MapFunction extends (value: T[keyof T], propName: keyof T, array: T[keyof T][]) => any
|
|
12
|
-
>(target: T, mapFunction: MapFunction): { [key in keyof T]: ReturnType<typeof mapFunction> } {
|
|
13
|
-
return Object.entries(target).reduce((carry, [key, value], index, array) => {
|
|
14
|
-
carry[key as keyof T] = mapFunction(value, key, array as T[keyof T][])
|
|
15
|
-
return carry
|
|
16
|
-
}, {} as { [key in keyof T]: ReturnType<typeof mapFunction> })
|
|
17
|
-
}
|
package/test/index.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import test from 'ava'
|
|
2
|
-
import { mapObject } from '../src/index'
|
|
3
|
-
|
|
4
|
-
test('mapObjectWithObjects', (t) => {
|
|
5
|
-
type Pokemon = { name: string; level: number }
|
|
6
|
-
|
|
7
|
-
const target: { [id in string]: Pokemon } = {
|
|
8
|
-
'001': { name: 'Bulbasaur', level: 10 },
|
|
9
|
-
'004': { name: 'Charmander', level: 8 },
|
|
10
|
-
'007': { name: 'Squirtle', level: 11 },
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const res = mapObject(target, (pkmn) => {
|
|
14
|
-
return { ...pkmn, level: pkmn.level + 1 }
|
|
15
|
-
})
|
|
16
|
-
t.deepEqual(res, {
|
|
17
|
-
'001': { name: 'Bulbasaur', level: 11 },
|
|
18
|
-
'004': { name: 'Charmander', level: 9 },
|
|
19
|
-
'007': { name: 'Squirtle', level: 12 },
|
|
20
|
-
})
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
test('with types', (t) => {
|
|
24
|
-
type Pokemon = { name: string; level: number }
|
|
25
|
-
|
|
26
|
-
const target: { [id in string]: Pokemon } = {
|
|
27
|
-
'001': { name: 'Bulbasaur', level: 10 },
|
|
28
|
-
'004': { name: 'Charmander', level: 8 },
|
|
29
|
-
'007': { name: 'Squirtle', level: 11 },
|
|
30
|
-
}
|
|
31
|
-
const mapFn = (pkmn: Pokemon): Pokemon => ({ ...pkmn, level: pkmn.level + 1 })
|
|
32
|
-
|
|
33
|
-
const res = mapObject(target, mapFn)
|
|
34
|
-
t.deepEqual(res, {
|
|
35
|
-
'001': { name: 'Bulbasaur', level: 11 },
|
|
36
|
-
'004': { name: 'Charmander', level: 9 },
|
|
37
|
-
'007': { name: 'Squirtle', level: 12 },
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('mapObjectWithNumbers', (t) => {
|
|
42
|
-
const target = {
|
|
43
|
-
'001': 1,
|
|
44
|
-
'004': 2,
|
|
45
|
-
'007': 3,
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const res = mapObject(target, (val, a, b) => val + 1)
|
|
49
|
-
|
|
50
|
-
t.deepEqual(res, {
|
|
51
|
-
'001': 2,
|
|
52
|
-
'004': 3,
|
|
53
|
-
'007': 4,
|
|
54
|
-
})
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
test('set to propname + test type inside of second arg', (t) => {
|
|
58
|
-
type Pokemon = { name: string; level: number }
|
|
59
|
-
|
|
60
|
-
const target: { [id in string]: Pokemon } = {
|
|
61
|
-
'001': { name: 'Bulbasaur', level: 10 },
|
|
62
|
-
'004': { name: 'Charmander', level: 8 },
|
|
63
|
-
'007': { name: 'Squirtle', level: 11 },
|
|
64
|
-
}
|
|
65
|
-
const res = mapObject(target, (pkmn, propName) => propName)
|
|
66
|
-
t.deepEqual(res, {
|
|
67
|
-
'001': '001',
|
|
68
|
-
'004': '004',
|
|
69
|
-
'007': '007',
|
|
70
|
-
})
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
test('replace objects with numbers', (t) => {
|
|
74
|
-
type Pokemon = { name: string; level: number }
|
|
75
|
-
|
|
76
|
-
const target: { [id in string]: Pokemon } = {
|
|
77
|
-
'001': { name: 'Bulbasaur', level: 10 },
|
|
78
|
-
'004': { name: 'Charmander', level: 8 },
|
|
79
|
-
'007': { name: 'Squirtle', level: 11 },
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const mappedNames = mapObject(target, (pkmn: Pokemon) => pkmn.name)
|
|
83
|
-
|
|
84
|
-
t.deepEqual(mappedNames, {
|
|
85
|
-
'001': 'Bulbasaur',
|
|
86
|
-
'004': 'Charmander',
|
|
87
|
-
'007': 'Squirtle',
|
|
88
|
-
})
|
|
89
|
-
})
|
package/tsconfig.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"baseUrl": ".",
|
|
4
|
-
"outDir": "./dist",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"declarationDir": "./types/",
|
|
7
|
-
"target": "es5",
|
|
8
|
-
"lib": ["es5", "es2015", "es2016", "es2017", "dom"]
|
|
9
|
-
},
|
|
10
|
-
"include": [
|
|
11
|
-
"src/**/*",
|
|
12
|
-
"test/**/*"
|
|
13
|
-
]
|
|
14
|
-
}
|