UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 4.07 kB
{"version":3,"file":"dropFirstBy.cjs","names":["purryOrderRulesWithArgument","heapMaybeInsert"],"sources":["../src/dropFirstBy.ts"],"sourcesContent":["import { heapify, heapMaybeInsert } from \"./internal/heap\";\nimport {\n purryOrderRulesWithArgument,\n type OrderRule,\n} from \"./internal/purryOrderRules\";\nimport type { CompareFunction } from \"./internal/types/CompareFunction\";\nimport type { NonEmptyArray } from \"./internal/types/NonEmptyArray\";\n\n/**\n * Drop the first `n` items from `data` based on the provided ordering criteria. This allows you to avoid sorting the array before dropping the items. The complexity of this function is *O(Nlogn)* where `N` is the length of the array.\n *\n * For the opposite operation (to keep `n` elements) see `takeFirstBy`.\n *\n * @param data - The input array.\n * @param n - The number of items to drop. If `n` is non-positive no items would be dropped and a *clone* of the input would be returned, if `n` is bigger then data.length no items would be returned.\n * @param rules - A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, \"desc\"]` for descending order.\n * @returns A subset of the input array.\n * @signature\n * dropFirstBy(data, n, ...rules);\n * @example\n * dropFirstBy(['aa', 'aaaa', 'a', 'aaa'], 2, x => x.length); // => ['aaaa', 'aaa']\n * @dataFirst\n * @category Array\n */\nexport function dropFirstBy<T>(\n data: readonly T[],\n n: number,\n ...rules: Readonly<NonEmptyArray<OrderRule<T>>>\n): T[];\n\n/**\n * Drop the first `n` items from `data` based on the provided ordering criteria. This allows you to avoid sorting the array before dropping the items. The complexity of this function is *O(Nlogn)* where `N` is the length of the array.\n *\n * For the opposite operation (to keep `n` elements) see `takeFirstBy`.\n *\n * @param n - The number of items to drop. If `n` is non-positive no items would be dropped and a *clone* of the input would be returned, if `n` is bigger then data.length no items would be returned.\n * @param rules - A variadic array of order rules defining the sorting criteria. Each order rule is a projection function that extracts a comparable value from the data. Sorting is based on these extracted values using the native `<` and `>` operators. Earlier rules take precedence over later ones. Use the syntax `[projection, \"desc\"]` for descending order.\n * @returns A subset of the input array.\n * @signature\n * dropFirstBy(n, ...rules)(data);\n * @example\n * pipe(['aa', 'aaaa', 'a', 'aaa'], dropFirstBy(2, x => x.length)); // => ['aaaa', 'aaa']\n * @dataLast\n * @category Array\n */\nexport function dropFirstBy<T>(\n n: number,\n ...rules: Readonly<NonEmptyArray<OrderRule<T>>>\n): (data: readonly T[]) => T[];\n\nexport function dropFirstBy(...args: readonly unknown[]): unknown {\n return purryOrderRulesWithArgument(dropFirstByImplementation, args);\n}\n\nfunction dropFirstByImplementation<T>(\n data: readonly T[],\n compareFn: CompareFunction<T>,\n n: number,\n): T[] {\n if (n >= data.length) {\n return [];\n }\n\n if (n <= 0) {\n return [...data];\n }\n\n const heap = data.slice(0, n);\n heapify(heap, compareFn);\n\n const out = [];\n\n const rest = data.slice(n);\n for (const item of rest) {\n const previousHead = heapMaybeInsert(heap, compareFn, item);\n out.push(previousHead ?? item);\n }\n\n return out;\n}\n"],"mappings":"sJAkDA,SAAgB,EAAY,GAAG,EAAmC,CAChE,OAAOA,EAAAA,EAA4B,EAA2B,CAAI,CACpE,CAEA,SAAS,EACP,EACA,EACA,EACK,CACL,GAAI,GAAK,EAAK,OACZ,MAAO,CAAC,EAGV,GAAI,GAAK,EACP,MAAO,CAAC,GAAG,CAAI,EAGjB,IAAM,EAAO,EAAK,MAAM,EAAG,CAAC,EAC5B,EAAA,EAAQ,EAAM,CAAS,EAEvB,IAAM,EAAM,CAAC,EAEP,EAAO,EAAK,MAAM,CAAC,EACzB,IAAK,IAAM,KAAQ,EAAM,CACvB,IAAM,EAAeC,EAAAA,EAAgB,EAAM,EAAW,CAAI,EAC1D,EAAI,KAAK,GAAgB,CAAI,CAC/B,CAEA,OAAO,CACT"}