map-anything 1.0.1 → 1.0.5
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 +13 -2
- 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 +74 -29
- package/.eslintignore +0 -9
- package/.eslintrc.js +0 -17
- package/.prettierrc +0 -9
- package/build/rollup.js +0 -59
- package/src/index.ts +0 -17
- package/test/index.ts +0 -84
- 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
|
|
|
@@ -14,7 +17,7 @@ I always want to do:
|
|
|
14
17
|
someObject.map(val => someFunction)
|
|
15
18
|
```
|
|
16
19
|
|
|
17
|
-
But this doesn't exist for objects,
|
|
20
|
+
But this doesn't exist for objects, you need to do this instead:
|
|
18
21
|
|
|
19
22
|
```js
|
|
20
23
|
Object.entries(someObject).reduce((carry, [key, value], index, array) => {
|
|
@@ -25,6 +28,8 @@ Object.entries(someObject).reduce((carry, [key, value], index, array) => {
|
|
|
25
28
|
|
|
26
29
|
So I made a wrapper function for that. 😃
|
|
27
30
|
|
|
31
|
+
`map-anything` has very good [#TypeScript](#typescript) support as well.
|
|
32
|
+
|
|
28
33
|
## Meet the family
|
|
29
34
|
|
|
30
35
|
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
|
|
@@ -78,6 +83,12 @@ addIds ===
|
|
|
78
83
|
}
|
|
79
84
|
```
|
|
80
85
|
|
|
86
|
+
## TypeScript
|
|
87
|
+
|
|
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!
|
|
89
|
+
|
|
90
|
+

|
|
91
|
+
|
|
81
92
|
## Source code
|
|
82
93
|
|
|
83
94
|
The source code is rather simple, it's doing something like the snippet show here below.
|
|
@@ -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,35 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "map-anything",
|
|
3
3
|
"sideEffects": false,
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"module": "dist/index.
|
|
8
|
-
"
|
|
4
|
+
"type": "module",
|
|
5
|
+
"version": "1.0.5",
|
|
6
|
+
"description": "Array.map but for objects with good TypeScript support. A small and simple integration.",
|
|
7
|
+
"module": "./dist/index.es.js",
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"types": "./dist/types/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.es.js",
|
|
13
|
+
"require": "./dist/index.cjs",
|
|
14
|
+
"types": "./dist/types/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=12.13",
|
|
22
|
+
"npm": ">=7"
|
|
23
|
+
},
|
|
9
24
|
"scripts": {
|
|
10
|
-
"test": "
|
|
11
|
-
"lint": "eslint
|
|
12
|
-
"
|
|
13
|
-
"
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"lint": "tsc --noEmit && eslint ./src --ext .ts",
|
|
27
|
+
"build": "rollup -c ./scripts/build.js",
|
|
28
|
+
"release": "npm run lint && del dist && npm run build && np"
|
|
14
29
|
},
|
|
15
|
-
"dependencies": {},
|
|
16
30
|
"devDependencies": {
|
|
17
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
18
|
-
"@typescript-eslint/parser": "^
|
|
19
|
-
"
|
|
20
|
-
"eslint": "^7.
|
|
21
|
-
"eslint-config-prettier": "^
|
|
22
|
-
"eslint-plugin-tree-shaking": "^1.
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"typescript": "^4.
|
|
31
|
+
"@typescript-eslint/eslint-plugin": "^5.10.1",
|
|
32
|
+
"@typescript-eslint/parser": "^5.10.1",
|
|
33
|
+
"del-cli": "^4.0.1",
|
|
34
|
+
"eslint": "^8.7.0",
|
|
35
|
+
"eslint-config-prettier": "^8.3.0",
|
|
36
|
+
"eslint-plugin-tree-shaking": "^1.10.0",
|
|
37
|
+
"np": "^7.6.0",
|
|
38
|
+
"prettier": "^2.5.1",
|
|
39
|
+
"rollup": "^2.66.0",
|
|
40
|
+
"rollup-plugin-typescript2": "^0.31.1",
|
|
41
|
+
"typescript": "^4.5.5",
|
|
42
|
+
"vitest": "^0.2.1"
|
|
28
43
|
},
|
|
29
44
|
"keywords": [
|
|
30
|
-
"map-object"
|
|
45
|
+
"map-object",
|
|
46
|
+
"object-map",
|
|
47
|
+
"object-to-object",
|
|
48
|
+
"mapping",
|
|
49
|
+
"transform",
|
|
50
|
+
"object-mapper",
|
|
51
|
+
"compose",
|
|
52
|
+
"map-reduce"
|
|
31
53
|
],
|
|
32
54
|
"author": "Luca Ban - Mesqueeb",
|
|
55
|
+
"funding": "https://github.com/sponsors/mesqueeb",
|
|
33
56
|
"license": "MIT",
|
|
34
57
|
"bugs": {
|
|
35
58
|
"url": "https://github.com/mesqueeb/map-anything/issues"
|
|
@@ -39,13 +62,35 @@
|
|
|
39
62
|
"type": "git",
|
|
40
63
|
"url": "git+https://github.com/mesqueeb/map-anything.git"
|
|
41
64
|
},
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
|
|
65
|
+
"np": {
|
|
66
|
+
"yarn": false,
|
|
67
|
+
"branch": "production"
|
|
68
|
+
},
|
|
69
|
+
"eslintConfig": {
|
|
70
|
+
"ignorePatterns": [
|
|
71
|
+
"node_modules",
|
|
72
|
+
"dist",
|
|
73
|
+
"scripts",
|
|
74
|
+
"test"
|
|
75
|
+
],
|
|
76
|
+
"root": true,
|
|
77
|
+
"parser": "@typescript-eslint/parser",
|
|
78
|
+
"plugins": [
|
|
79
|
+
"@typescript-eslint",
|
|
80
|
+
"tree-shaking"
|
|
81
|
+
],
|
|
82
|
+
"extends": [
|
|
83
|
+
"eslint:recommended",
|
|
84
|
+
"plugin:@typescript-eslint/eslint-recommended",
|
|
85
|
+
"plugin:@typescript-eslint/recommended",
|
|
86
|
+
"prettier"
|
|
45
87
|
],
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
|
|
88
|
+
"rules": {
|
|
89
|
+
"@typescript-eslint/no-empty-function": "off",
|
|
90
|
+
"@typescript-eslint/no-explicit-any": "off",
|
|
91
|
+
"@typescript-eslint/ban-ts-ignore": "off",
|
|
92
|
+
"tree-shaking/no-side-effects-in-initialization": "error",
|
|
93
|
+
"@typescript-eslint/ban-ts-comment": "off"
|
|
94
|
+
}
|
|
50
95
|
}
|
|
51
96
|
}
|
package/.eslintignore
DELETED
package/.eslintrc.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// npm i -D @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-prettier eslint-plugin-tree-shaking
|
|
2
|
-
module.exports = {
|
|
3
|
-
root: true,
|
|
4
|
-
parser: '@typescript-eslint/parser',
|
|
5
|
-
plugins: ['@typescript-eslint', 'tree-shaking'],
|
|
6
|
-
extends: [
|
|
7
|
-
'eslint:recommended',
|
|
8
|
-
'plugin:@typescript-eslint/eslint-recommended',
|
|
9
|
-
'plugin:@typescript-eslint/recommended',
|
|
10
|
-
'prettier/@typescript-eslint',
|
|
11
|
-
],
|
|
12
|
-
rules: {
|
|
13
|
-
'@typescript-eslint/no-explicit-any': 'off',
|
|
14
|
-
'@typescript-eslint/ban-ts-ignore': 'off',
|
|
15
|
-
'tree-shaking/no-side-effects-in-initialization': 'error',
|
|
16
|
-
},
|
|
17
|
-
}
|
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,84 +0,0 @@
|
|
|
1
|
-
import test from 'ava'
|
|
2
|
-
import { mapObject } from '../src/index'
|
|
3
|
-
|
|
4
|
-
test('mapObjectWithObjects', (t) => {
|
|
5
|
-
const target = {
|
|
6
|
-
'001': { name: 'Bulbasaur', level: 10 },
|
|
7
|
-
'004': { name: 'Charmander', level: 8 },
|
|
8
|
-
'007': { name: 'Squirtle', level: 11 },
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const res = mapObject(target, (pkmn) => {
|
|
12
|
-
return { ...pkmn, level: pkmn.level + 1 }
|
|
13
|
-
})
|
|
14
|
-
t.deepEqual(res, {
|
|
15
|
-
'001': { name: 'Bulbasaur', level: 11 },
|
|
16
|
-
'004': { name: 'Charmander', level: 9 },
|
|
17
|
-
'007': { name: 'Squirtle', level: 12 },
|
|
18
|
-
})
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
test('with types', (t) => {
|
|
22
|
-
type Pokemon = { name: string, level: number }
|
|
23
|
-
const target: { [id in string]: Pokemon } = {
|
|
24
|
-
'001': { name: 'Bulbasaur', level: 10 },
|
|
25
|
-
'004': { name: 'Charmander', level: 8 },
|
|
26
|
-
'007': { name: 'Squirtle', level: 11 },
|
|
27
|
-
}
|
|
28
|
-
const mapFn = (pkmn: Pokemon): Pokemon => ({ ...pkmn, level: pkmn.level + 1 })
|
|
29
|
-
|
|
30
|
-
const res = mapObject(target, mapFn)
|
|
31
|
-
t.deepEqual(res, {
|
|
32
|
-
'001': { name: 'Bulbasaur', level: 11 },
|
|
33
|
-
'004': { name: 'Charmander', level: 9 },
|
|
34
|
-
'007': { name: 'Squirtle', level: 12 },
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
test('mapObjectWithNumbers', (t) => {
|
|
39
|
-
const target = {
|
|
40
|
-
'001': 1,
|
|
41
|
-
'004': 2,
|
|
42
|
-
'007': 3,
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const res = mapObject(target, (val, a, b) => val + 1)
|
|
46
|
-
|
|
47
|
-
t.deepEqual(res, {
|
|
48
|
-
'001': 2,
|
|
49
|
-
'004': 3,
|
|
50
|
-
'007': 4,
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
test('set to propname + test type inside of second arg', (t) => {
|
|
55
|
-
type Pokemon = { name: string, level: number }
|
|
56
|
-
const target: { [id in string]: Pokemon } = {
|
|
57
|
-
'001': { name: 'Bulbasaur', level: 10 },
|
|
58
|
-
'004': { name: 'Charmander', level: 8 },
|
|
59
|
-
'007': { name: 'Squirtle', level: 11 },
|
|
60
|
-
}
|
|
61
|
-
const res = mapObject(target, (pkmn, propName) => propName)
|
|
62
|
-
t.deepEqual(res, {
|
|
63
|
-
'001': '001',
|
|
64
|
-
'004': '004',
|
|
65
|
-
'007': '007',
|
|
66
|
-
})
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('replace objects with numbers', (t) => {
|
|
70
|
-
type Pokemon = { name: string, level: number }
|
|
71
|
-
const target: { [id in string]: Pokemon } = {
|
|
72
|
-
'001': { name: 'Bulbasaur', level: 10 },
|
|
73
|
-
'004': { name: 'Charmander', level: 8 },
|
|
74
|
-
'007': { name: 'Squirtle', level: 11 },
|
|
75
|
-
}
|
|
76
|
-
const mapFn = (pkmn: Pokemon) => pkmn.name
|
|
77
|
-
|
|
78
|
-
const res = mapObject(target, mapFn)
|
|
79
|
-
t.deepEqual(res, {
|
|
80
|
-
'001': 'Bulbasaur',
|
|
81
|
-
'004': 'Charmander',
|
|
82
|
-
'007': 'Squirtle',
|
|
83
|
-
})
|
|
84
|
-
})
|
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
|
-
}
|