UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

50 lines (49 loc) 1.96 kB
import { identity } from "../../function/identity.mjs"; import { property } from "../object/property.mjs"; import { matches } from "../predicate/matches.mjs"; import { matchesProperty } from "../predicate/matchesProperty.mjs"; //#region src/compat/util/iteratee.ts /** * Creates a function that returns a value from an element in a collection. * * You can call `iteratee` with the following types of arguments: * * - **Function**: Returns the function as-is, which will be called with the element from the collection. * - **Property name**: Returns the value of the specified property from the element. * - **Property-value pair**: Returns a boolean indicating whether the element's property matches the given value. * - **Partial object**: Returns a boolean indicating whether the element matches the properties of the partial object. * * If you don't provide any arguments or pass `null`, this function will return a function that simply returns its input unchanged. * * @param value - The value to convert to an iteratee. * @returns Returns the new iteratee function. * @example * const func = iteratee(); * [{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [{ a: 1 }, { a: 2 }, { a: 3 }] * * const func = iteratee((object) => object.a); * [{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3] * * const func = iteratee('a'); * [{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3] * * const func = iteratee({ a: 1 }); * [{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 } * * const func = iteratee(['a', 1]); * [{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 } */ function iteratee(value) { if (value == null) return identity; switch (typeof value) { case "function": return value; case "object": if (Array.isArray(value) && value.length === 2) return matchesProperty(value[0], value[1]); return matches(value); case "string": case "symbol": case "number": return property(value); } } //#endregion export { iteratee };