30 lines
1.2 KiB
JavaScript
30 lines
1.2 KiB
JavaScript
import { identity } from "../../function/identity.mjs";
|
|
import { mapValues as mapValues$1 } from "../../object/mapValues.mjs";
|
|
import { iteratee } from "../util/iteratee.mjs";
|
|
//#region src/compat/object/mapValues.ts
|
|
/**
|
|
* Creates a new object with the same keys as the given object, but with values generated
|
|
* by running each own enumerable property of the object through the iteratee function.
|
|
*
|
|
* @template T - The type of the object.
|
|
* @template K - The type of the keys in the object.
|
|
* @template V - The type of the new values generated by the iteratee function.
|
|
*
|
|
* @param {T} object - The object to iterate over.
|
|
* @param {(value: T[K], key: K, object: T) => V | PropertyKey | readonly PropertyKey[] | null | undefined} [getNewValue] -
|
|
* The function invoked per own enumerable property, or a path to generate new values.
|
|
* @returns {Record<K, V>} - Returns the new mapped object.
|
|
*
|
|
* @example
|
|
* // Example usage:
|
|
* const obj = { a: 1, b: 2 };
|
|
* const result = mapValues(obj, (value) => value * 2);
|
|
* console.log(result); // { a: 2, b: 4 }
|
|
*/
|
|
function mapValues(object, getNewValue = identity) {
|
|
if (object == null) return {};
|
|
return mapValues$1(object, iteratee(getNewValue));
|
|
}
|
|
//#endregion
|
|
export { mapValues };
|