@maddimathon/utility-typescript
Version:
TypeScript utilities (types, functions, classes) to use in various projects.
32 lines (31 loc) • 711 B
JavaScript
/**
* @since 2.0.0-beta.2
*
* @packageDocumentation
*/
/*!
* @maddimathon/utility-typescript@2.0.0-beta.5
* @license MIT
*/
/**
* A utility to map the values of an object using a callback function.
*
* @param obj The object to map.
* @param callback The callback function used to define new values.
*
* @category Functions – Object
*
* @since 2.0.0-beta.2
*/
export function objectMap(obj, callback) {
// returns
if (typeof obj !== 'object' || !obj) {
return obj;
}
const entries = Object.entries(obj);
const mappedEntries = entries.map(([key, value]) => [
key,
callback([key, value]),
]);
return Object.fromEntries(mappedEntries);
}