typedash
Version:
modern, type-safe collection of utility functions
1 lines • 2.4 kB
Source Map (JSON)
{"version":3,"file":"partition-Dy-peAI7.cjs","names":[],"sources":["../src/functions/partition/partition.ts"],"sourcesContent":["import type { Maybe } from '../../types';\n\n/**\n * Partitions an array into two arrays based on a predicate function.\n * @param array The input array to partition.\n * @param predicate A function that takes an array item and returns a boolean indicating whether the item should be included in the \"equals\" array or the \"notEquals\" array.\n * @returns A tuple containing two arrays: the \"equals\" array and the \"notEquals\" array.\n */\nexport function partition<T, S extends T>(\n array: Maybe<readonly T[]>,\n predicate: (item: T) => item is S\n): readonly [equals: S[], notEquals: Exclude<T, S>[]];\n/**\n * Partitions an array into two arrays based on a predicate function.\n * @param array The input array to partition.\n * @param predicate A function that takes an array item and returns a boolean indicating whether the item should be included in the \"equals\" array or the \"notEquals\" array.\n * @returns A tuple containing two arrays: the \"equals\" array and the \"notEquals\" array.\n */\nexport function partition<T>(\n array: Maybe<readonly T[]>,\n predicate: (item: T) => boolean\n): readonly [equals: T[], notEquals: T[]];\n/**\n * Implementation of all overloads.\n * @param array The input array to partition.\n * @param predicate A function that takes an array item and returns a boolean indicating whether the item should be included in the \"equals\" array or the \"notEquals\" array.\n * @returns A tuple containing two arrays: the \"equals\" array and the \"notEquals\" array.\n */\nexport function partition<T, S extends T>(\n array: Maybe<readonly T[]>,\n predicate: ((item: T) => item is S) | ((item: T) => boolean)\n): readonly [S[], Exclude<T, S>[]] {\n if (array == null) {\n return [[], []];\n }\n\n const equals: S[] = [];\n const notEquals: Exclude<T, S>[] = [];\n\n for (const item of array) {\n if (predicate(item)) {\n equals.push(item as S);\n } else {\n notEquals.push(item as Exclude<T, S>);\n }\n }\n\n return [equals, notEquals];\n}\n"],"mappings":";;;;;;;;AA4BA,SAAgB,UACd,OACA,WACiC;AACjC,KAAI,SAAS,KACX,QAAO,CAAC,EAAE,EAAE,EAAE,CAAC;CAGjB,MAAM,SAAc,EAAE;CACtB,MAAM,YAA6B,EAAE;AAErC,MAAK,MAAM,QAAQ,MACjB,KAAI,UAAU,KAAK,CACjB,QAAO,KAAK,KAAU;KAEtB,WAAU,KAAK,KAAsB;AAIzC,QAAO,CAAC,QAAQ,UAAU"}