map-anything 1.0.3 → 2.0.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.
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
 
@@ -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(function (carry, _a, index, array) {
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(function (carry, _a, index, array) {
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,58 @@
1
1
  {
2
2
  "name": "map-anything",
3
3
  "sideEffects": false,
4
- "version": "1.0.3",
5
- "description": "Array.map but for objects. A small and simple integration.",
6
- "main": "dist/index.cjs.js",
7
- "module": "dist/index.esm.js",
8
- "types": "types/index.d.ts",
4
+ "type": "module",
5
+ "version": "2.0.0",
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": "ava",
11
- "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
12
- "rollup": "rollup -c build/rollup.js",
13
- "build": "npm run lint && npm run test && npm run rollup",
14
- "release": "npm run build && np"
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"
15
29
  },
16
30
  "devDependencies": {
17
- "@typescript-eslint/eslint-plugin": "^4.17.0",
18
- "@typescript-eslint/parser": "^4.17.0",
19
- "ava": "^3.15.0",
20
- "eslint": "^7.21.0",
21
- "eslint-config-prettier": "^8.1.0",
22
- "eslint-plugin-tree-shaking": "^1.8.0",
23
- "np": "^7.4.0",
24
- "prettier": "^2.2.1",
25
- "rollup": "^2.41.2",
26
- "rollup-plugin-typescript2": "^0.30.0",
27
- "ts-node": "^9.1.1",
28
- "tsconfig-paths": "^3.9.0",
29
- "typescript": "^4.2.3"
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"
30
43
  },
31
44
  "keywords": [
32
- "map-object"
45
+ "map-object",
46
+ "object-map",
47
+ "object-to-object",
48
+ "mapping",
49
+ "transform",
50
+ "object-mapper",
51
+ "compose",
52
+ "map-reduce"
33
53
  ],
34
54
  "author": "Luca Ban - Mesqueeb",
55
+ "funding": "https://github.com/sponsors/mesqueeb",
35
56
  "license": "MIT",
36
57
  "bugs": {
37
58
  "url": "https://github.com/mesqueeb/map-anything/issues"
@@ -41,20 +62,17 @@
41
62
  "type": "git",
42
63
  "url": "git+https://github.com/mesqueeb/map-anything.git"
43
64
  },
44
- "ava": {
45
- "extensions": [
46
- "ts"
47
- ],
48
- "require": [
49
- "tsconfig-paths/register",
50
- "ts-node/register"
51
- ]
52
- },
53
65
  "np": {
54
66
  "yarn": false,
55
67
  "branch": "production"
56
68
  },
57
69
  "eslintConfig": {
70
+ "ignorePatterns": [
71
+ "node_modules",
72
+ "dist",
73
+ "scripts",
74
+ "test"
75
+ ],
58
76
  "root": true,
59
77
  "parser": "@typescript-eslint/parser",
60
78
  "plugins": [
@@ -68,6 +86,7 @@
68
86
  "prettier"
69
87
  ],
70
88
  "rules": {
89
+ "@typescript-eslint/no-empty-function": "off",
71
90
  "@typescript-eslint/no-explicit-any": "off",
72
91
  "@typescript-eslint/ban-ts-ignore": "off",
73
92
  "tree-shaking/no-side-effects-in-initialization": "error",
package/.eslintignore DELETED
@@ -1,9 +0,0 @@
1
- # don't ever lint node_modules
2
- node_modules
3
- # don't lint build output (make sure it's set to your correct build folder name)
4
- dist
5
- # don't lint nyc coverage output
6
- coverage
7
-
8
- test
9
- .eslintrc.js
@@ -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
@@ -1,9 +0,0 @@
1
- {
2
- "printWidth": 100,
3
- "tabWidth": 2,
4
- "singleQuote": true,
5
- "trailingComma": "es5",
6
- "semi": false,
7
- "bracketSpacing": true,
8
- "quoteProps": "consistent"
9
- }
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
- }