{"version":3,"sources":["../../../../src/vanilla/helpers/filters.ts"],"sourcesContent":["import type { MapResult, Result } from \"../types\";\n\n/**\n * Filters and sorts an array of results based on a maximum score and a limit.\n *\n * @template T - The type of the result data.\n * @param results - An array of results to filter and sort. Each result must have a `score` property.\n * @param maxScore - The maximum score threshold. Results with a score greater than this value will be excluded.\n * @param limit - The maximum number of results to return after filtering and sorting.\n * @returns A new array of results that are filtered by the maximum score, sorted in ascending order of score, and limited to the specified number of items.\n */\nexport const filterResults = <T>(\n\tresults: Result<T>[],\n\tmaxScore: number,\n\tlimit: number,\n): Result<T>[] => {\n\treturn results\n\t\t.filter((result) => result.score <= maxScore)\n\t\t.sort((a, b) => a.score - b.score)\n\t\t.slice(0, limit);\n};\n\n/**\n * Transforms an array of `Result<T>` objects into an array of `Result<U>` objects\n * by applying an optional mapping function to the `item` property of each result.\n *\n * @template T - The type of the input items in the results.\n * @template U - The type of the output items in the results after mapping.\n *\n * @param results - An array of `Result<T>` objects to be transformed.\n * @param mapResult - An optional function that maps an item of type `T` to type `U`.\n *                         If not provided, the original results are returned as `Result<U>[]`.\n *\n * @returns An array of `Result<U>` objects. If `mapResult` is not provided, the original\n *          results are returned with their type cast to `Result<U>[]`.\n */\nexport const getMapResultItem = <T, U>(\n\tresults: Result<T>[],\n\tmapResult?: MapResult<T, U>,\n): Result<U>[] => {\n\t// if mapResult is not provided, return the original results\n\tif (!mapResult) {\n\t\treturn results as unknown as Result<U>[];\n\t}\n\n\t// otherwise, map the results using the provided function\n\treturn results.map(({ item, ...rest }, i, array) => ({\n\t\t...rest,\n\t\titem: mapResult(item, i, array),\n\t}));\n};\n\nexport const parseResults = <T, U>(\n\tresults: Result<T>[],\n\tmaxScore: number,\n\tlimit: number,\n\tmapResult?: MapResult<T, U>,\n): Result<U>[] => {\n\tconst filteredResults = filterResults(results, maxScore, limit);\n\n\treturn getMapResultItem(filteredResults, mapResult);\n};\n"],"mappings":";AAWO,IAAM,gBAAgB,CAC5B,SACA,UACA,UACiB;AACjB,SAAO,QACL,OAAO,CAAC,WAAW,OAAO,SAAS,QAAQ,EAC3C,KAAK,CAAC,GAAG,MAAM,EAAE,QAAQ,EAAE,KAAK,EAChC,MAAM,GAAG,KAAK;AACjB;AAgBO,IAAM,mBAAmB,CAC/B,SACA,cACiB;AAEjB,MAAI,CAAC,WAAW;AACf,WAAO;AAAA,EACR;AAGA,SAAO,QAAQ,IAAI,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,GAAG,WAAW;AAAA,IACpD,GAAG;AAAA,IACH,MAAM,UAAU,MAAM,GAAG,KAAK;AAAA,EAC/B,EAAE;AACH;AAEO,IAAM,eAAe,CAC3B,SACA,UACA,OACA,cACiB;AACjB,QAAM,kBAAkB,cAAc,SAAS,UAAU,KAAK;AAE9D,SAAO,iBAAiB,iBAAiB,SAAS;AACnD;","names":[]}