map-anything
Advanced tools
Comparing version 2.1.2 to 2.2.0
@@ -7,2 +7,8 @@ /** | ||
}; | ||
/** | ||
* Map each value of an object with provided function, just like `Array.map` | ||
*/ | ||
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<{ | ||
[key in keyof T]: Awaited<ReturnType<typeof mapFunction>>; | ||
}>; | ||
type KeyOfMap<M extends Map<unknown, unknown>> = M extends Map<infer K, unknown> ? K : never; | ||
@@ -15,2 +21,2 @@ type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never; | ||
export { KeyOfMap, ValueOfMap, mapMap, mapObject }; | ||
export { type KeyOfMap, type ValueOfMap, mapMap, mapObject, mapObjectAsync }; |
@@ -7,2 +7,14 @@ function mapObject(target, mapFunction) { | ||
} | ||
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) { | ||
@@ -15,2 +27,2 @@ return [...target.entries()].reduce((carry, [key, value], index, array) => { | ||
export { mapMap, mapObject }; | ||
export { mapMap, mapObject, mapObjectAsync }; |
{ | ||
"name": "map-anything", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "Array.map but for objects with good TypeScript support. A small and simple integration.", | ||
@@ -35,15 +35,15 @@ "type": "module", | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^5.59.2", | ||
"@typescript-eslint/parser": "^5.59.2", | ||
"del-cli": "^5.0.0", | ||
"eslint": "^8.40.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-tree-shaking": "^1.10.0", | ||
"np": "^7.7.0", | ||
"prettier": "^2.8.8", | ||
"rollup": "^3.23.0", | ||
"rollup-plugin-dts": "^5.3.0", | ||
"rollup-plugin-esbuild": "^5.0.0", | ||
"typescript": "^4.9.5", | ||
"vitest": "^0.31.0" | ||
"@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" | ||
}, | ||
@@ -50,0 +50,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
11451
72