remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 4.38 kB
Source Map (JSON)
{"version":3,"file":"find.cjs","names":["purry","toSingle","SKIP_ITEM"],"sources":["../src/find.ts"],"sourcesContent":["import { toSingle } from \"./internal/toSingle\";\nimport type { LazyEvaluator } from \"./internal/types/LazyEvaluator\";\nimport { SKIP_ITEM } from \"./internal/utilityEvaluators\";\nimport { purry } from \"./purry\";\n\n/**\n * Returns the first element in the provided array that satisfies the provided\n * testing function. If no values satisfy the testing function, `undefined` is\n * returned.\n *\n * Similar functions:\n * * `findLast` - If you need the last element that satisfies the provided testing function.\n * * `findIndex` - If you need the index of the found element in the array.\n * * `indexOf` - If you need to find the index of a value.\n * * `includes` - If you need to find if a value exists in an array.\n * * `some` - If you need to find if any element satisfies the provided testing function.\n * * `filter` - If you need to find all elements that satisfy the provided testing function.\n *\n * @param data - The items to search in.\n * @param predicate - A function to execute for each element in the array. It\n * should return `true` to indicate a matching element has been found, and\n * `false` otherwise. A type-predicate can also be used to narrow the result.\n * @returns The first element in the array that satisfies the provided testing\n * function. Otherwise, `undefined` is returned.\n * @signature\n * R.find(data, predicate)\n * @example\n * R.find([1, 3, 4, 6], n => n % 2 === 0) // => 4\n * @dataFirst\n * @lazy\n * @category Array\n */\nexport function find<T, S extends T>(\n data: ReadonlyArray<T>,\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => value is S,\n): S | undefined;\nexport function find<T>(\n data: ReadonlyArray<T>,\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,\n): T | undefined;\n\n/**\n * Returns the first element in the provided array that satisfies the provided\n * testing function. If no values satisfy the testing function, `undefined` is\n * returned.\n *\n * Similar functions:\n * * `findLast` - If you need the last element that satisfies the provided testing function.\n * * `findIndex` - If you need the index of the found element in the array.\n * * `indexOf` - If you need to find the index of a value.\n * * `includes` - If you need to find if a value exists in an array.\n * * `some` - If you need to find if any element satisfies the provided testing function.\n * * `filter` - If you need to find all elements that satisfy the provided testing function.\n *\n * @param predicate - A function to execute for each element in the array. It\n * should return `true` to indicate a matching element has been found, and\n * `false` otherwise. A type-predicate can also be used to narrow the result.\n * @returns The first element in the array that satisfies the provided testing\n * function. Otherwise, `undefined` is returned.\n * @signature\n * R.find(predicate)(data)\n * @example\n * R.pipe(\n * [1, 3, 4, 6],\n * R.find(n => n % 2 === 0)\n * ) // => 4\n * @dataLast\n * @lazy\n * @category Array\n */\nexport function find<T, S extends T>(\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => value is S,\n): (data: ReadonlyArray<T>) => S | undefined;\nexport function find<T>(\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,\n): (data: ReadonlyArray<T>) => T | undefined;\n\nexport function find(...args: ReadonlyArray<unknown>): unknown {\n return purry(findImplementation, args, toSingle(lazyImplementation));\n}\n\nconst findImplementation = <T, S extends T>(\n data: ReadonlyArray<T>,\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => value is S,\n): S | undefined => data.find(predicate);\n\nconst lazyImplementation =\n <T, S extends T>(\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => value is S,\n ): LazyEvaluator<T, S> =>\n (value, index, data) =>\n predicate(value, index, data)\n ? { done: true, hasNext: true, next: value }\n : SKIP_ITEM;\n"],"mappings":"2HA6EA,SAAgB,EAAK,GAAG,EAAuC,CAC7D,OAAOA,EAAAA,EAAM,EAAoB,EAAMC,EAAAA,EAAS,EAAmB,CAAC,CAGtE,MAAM,GACJ,EACA,IACkB,EAAK,KAAK,EAAU,CAElC,EAEF,IAED,EAAO,EAAO,IACb,EAAU,EAAO,EAAO,EAAK,CACzB,CAAE,KAAM,GAAM,QAAS,GAAM,KAAM,EAAO,CAC1CC,EAAAA"}