map-anything
Advanced tools
Comparing version 2.2.0 to 3.0.0
/** | ||
* Map each value of an object with provided function, just like `Array.map` | ||
*/ | ||
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): { | ||
export declare function mapObject<T extends { | ||
[key in string | number | symbol]: unknown; | ||
}, MapFunction extends (value: T[keyof T], propName: keyof T, array: T[keyof T][]) => any>(target: T, mapFunction: MapFunction): { | ||
[key in keyof T]: ReturnType<typeof mapFunction>; | ||
@@ -10,12 +12,12 @@ }; | ||
*/ | ||
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<{ | ||
export declare function mapObjectAsync<T extends { | ||
[key in string | number | symbol]: unknown; | ||
}, MapFunction extends (value: T[keyof T], propName: keyof T, array: T[keyof T][]) => Promise<any>>(target: T, mapFunction: MapFunction): Promise<{ | ||
[key in keyof T]: Awaited<ReturnType<typeof mapFunction>>; | ||
}>; | ||
type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never; | ||
type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never; | ||
export type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never; | ||
export type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never; | ||
/** | ||
* Map each value of a map with provided function, just like `Array.map` | ||
*/ | ||
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>>; | ||
export { type KeyOfMap, type ValueOfMap, mapMap, mapObject, mapObjectAsync }; | ||
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>>; |
@@ -1,26 +0,33 @@ | ||
function mapObject(target, mapFunction) { | ||
return Object.entries(target).reduce((carry, [key, value], index, array) => { | ||
carry[key] = mapFunction(value, key, array); | ||
return carry; | ||
}, {}); | ||
/** | ||
* Map each value of an object with provided function, just like `Array.map` | ||
*/ | ||
export function mapObject(target, mapFunction) { | ||
return Object.entries(target).reduce((carry, [key, value], index, array) => { | ||
carry[key] = mapFunction(value, key, array); | ||
return carry; | ||
}, {}); | ||
} | ||
async function mapObjectAsync(target, mapFunction) { | ||
const entries = Object.entries(target); | ||
const promises = entries.map(async ([key, value]) => { | ||
const newValue = await mapFunction(value, key, target); | ||
return [key, newValue]; | ||
}); | ||
const results = await Promise.all(promises); | ||
return results.reduce((carry, [key, value]) => { | ||
carry[key] = value; | ||
return carry; | ||
}, {}); | ||
/** | ||
* Map each value of an object with provided function, just like `Array.map` | ||
*/ | ||
export async function mapObjectAsync(target, mapFunction) { | ||
const entries = Object.entries(target); | ||
const promises = entries.map(async ([key, value]) => { | ||
const newValue = await mapFunction(value, key, target); | ||
return [key, newValue]; | ||
}); | ||
const results = await Promise.all(promises); | ||
return results.reduce((carry, [key, value]) => { | ||
carry[key] = value; | ||
return carry; | ||
}, {}); | ||
} | ||
function mapMap(target, mapFunction) { | ||
return [...target.entries()].reduce((carry, [key, value], index, array) => { | ||
carry.set(key, mapFunction(value, key, array)); | ||
return carry; | ||
}, /* @__PURE__ */ new Map()); | ||
/** | ||
* Map each value of a map with provided function, just like `Array.map` | ||
*/ | ||
export function mapMap(target, mapFunction) { | ||
return [...target.entries()].reduce((carry, [key, value], index, array) => { | ||
carry.set(key, mapFunction(value, key, array)); | ||
return carry; | ||
}, new Map()); | ||
} | ||
export { mapMap, mapObject, mapObjectAsync }; |
{ | ||
"name": "map-anything", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "Array.map but for objects with good TypeScript support. A small and simple integration.", | ||
"type": "module", | ||
"sideEffects": false, | ||
"types": "./dist/index.d.ts", | ||
"module": "./dist/index.js", | ||
"main": "./dist/index.js", | ||
"exports": { | ||
@@ -22,29 +19,21 @@ ".": { | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=12.13" | ||
"node": ">=18" | ||
}, | ||
"scripts": { | ||
"test": "vitest run", | ||
"lint": "tsc --noEmit && eslint ./src --ext .ts", | ||
"build": "rollup -c ./rollup.config.js", | ||
"release": "npm run lint && del dist && npm run build && np" | ||
"lint": "tsc --noEmit && eslint ./src", | ||
"build": "del-cli dist && tsc", | ||
"release": "npm run lint && npm run build && np" | ||
}, | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^6.12.0", | ||
"@typescript-eslint/parser": "^6.12.0", | ||
"del-cli": "^5.1.0", | ||
"eslint": "^8.54.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-tree-shaking": "^1.12.0", | ||
"np": "^8.0.4", | ||
"prettier": "^3.1.0", | ||
"rollup": "^4.5.2", | ||
"rollup-plugin-dts": "^6.1.0", | ||
"rollup-plugin-esbuild": "^6.1.0", | ||
"typescript": "^5.3.2", | ||
"vitest": "^0.34.6" | ||
"np": "^10.0.5", | ||
"vitest": "^1.6.0", | ||
"@cycraft/eslint": "^0.3.0", | ||
"@cycraft/tsconfig": "^0.1.2" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
@@ -60,44 +49,11 @@ "map-object", | ||
], | ||
"author": "Luca Ban - Mesqueeb", | ||
"author": "Luca Ban - Mesqueeb (https://cycraft.co)", | ||
"funding": "https://github.com/sponsors/mesqueeb", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mesqueeb/map-anything/issues" | ||
}, | ||
"homepage": "https://github.com/mesqueeb/map-anything#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mesqueeb/map-anything.git" | ||
"url": "https://github.com/mesqueeb/map-anything.git" | ||
}, | ||
"np": { | ||
"yarn": false, | ||
"branch": "production" | ||
}, | ||
"eslintConfig": { | ||
"ignorePatterns": [ | ||
"node_modules", | ||
"dist", | ||
"scripts", | ||
"test" | ||
], | ||
"root": true, | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": [ | ||
"@typescript-eslint", | ||
"tree-shaking" | ||
], | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier" | ||
], | ||
"rules": { | ||
"@typescript-eslint/no-empty-function": "off", | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"@typescript-eslint/ban-ts-ignore": "off", | ||
"tree-shaking/no-side-effects-in-initialization": "error", | ||
"@typescript-eslint/ban-ts-comment": "off" | ||
} | ||
} | ||
"homepage": "https://github.com/mesqueeb/map-anything#readme", | ||
"bugs": "https://github.com/mesqueeb/map-anything/issues" | ||
} |
@@ -17,3 +17,3 @@ # Map anything 🗺 | ||
```js | ||
someObject.map(val => someFunction) | ||
someObject.map((val) => someFunction) | ||
``` | ||
@@ -34,19 +34,12 @@ | ||
## Meet the family (more tiny utils with TS support) | ||
## Usage | ||
- [is-what 🙉](https://github.com/mesqueeb/is-what) | ||
- [is-where 🙈](https://github.com/mesqueeb/is-where) | ||
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything) | ||
- [check-anything 👁](https://github.com/mesqueeb/check-anything) | ||
- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything) | ||
- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything) | ||
- [map-anything 🗺](https://github.com/mesqueeb/map-anything) | ||
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything) | ||
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything) | ||
- [case-anything 🐫](https://github.com/mesqueeb/case-anything) | ||
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything) | ||
- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything) | ||
Provided Functions: | ||
## Usage | ||
- `mapObject` takes an object and maps over the values of each key | ||
- `mapObjectAsync` takes an object and maps a promise over the values of each key, after which you can just do a single await | ||
- `mapMap` takes a map and maps over the values of each key | ||
### Basic Usage | ||
```js | ||
@@ -65,2 +58,3 @@ import { mapObject } from 'map-anything' | ||
// results in: | ||
levelUp === | ||
@@ -81,2 +75,8 @@ { | ||
const pokemon = { | ||
'001': { name: 'Bulbasaur', level: 10 }, | ||
'004': { name: 'Charmander', level: 8 }, | ||
'007': { name: 'Squirtle', level: 11 }, | ||
} | ||
const addIds = mapObject(pokemon, (pkmn, propName) => { | ||
@@ -87,10 +87,35 @@ const id = propName | ||
// results in: | ||
addIds === | ||
{ | ||
'001': { name: 'Bulbasaur', level: 10, id: '001' }, | ||
'004': { name: 'Charmander', level: 8, id: '004' }, | ||
'007': { name: 'Squirtle', level: 11, id: '007' }, | ||
'001': { name: 'Bulbasaur', level: 10, id: '001' }, | ||
'004': { name: 'Charmander', level: 8, id: '004' }, | ||
'007': { name: 'Squirtle', level: 11, id: '007' }, | ||
} | ||
``` | ||
### Map Object Async | ||
```ts | ||
const pokemon = { | ||
'001': { name: 'Bulbasaur', level: 10 }, | ||
'004': { name: 'Charmander', level: 8 }, | ||
'007': { name: 'Squirtle', level: 11 }, | ||
} | ||
const result = await mapObjectAsync(pokemon, async (pkmn, propName) => { | ||
const id = propName | ||
const data = await fetchData(id) // hypothetical API call | ||
return { ...pkmn, data } | ||
}) | ||
// results in: | ||
result === | ||
{ | ||
'001': { name: 'Bulbasaur', level: 10, data: '...' }, // some fetched data | ||
'004': { name: 'Charmander', level: 8, data: '...' }, | ||
'007': { name: 'Squirtle', level: 11, data: '...' }, | ||
} | ||
``` | ||
## TypeScript | ||
@@ -102,2 +127,17 @@ | ||
## Meet the family (more tiny utils with TS support) | ||
- [is-what 🙉](https://github.com/mesqueeb/is-what) | ||
- [is-where 🙈](https://github.com/mesqueeb/is-where) | ||
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything) | ||
- [check-anything 👁](https://github.com/mesqueeb/check-anything) | ||
- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything) | ||
- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything) | ||
- [map-anything 🗺](https://github.com/mesqueeb/map-anything) | ||
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything) | ||
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything) | ||
- [case-anything 🐫](https://github.com/mesqueeb/case-anything) | ||
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything) | ||
- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything) | ||
## Source code | ||
@@ -104,0 +144,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5
151
3
9313
5
55
1