2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-09-18 18:29:29 +00:00
|
|
|
// This module is browser compatible.
|
2021-07-15 21:37:53 +00:00
|
|
|
|
|
|
|
/**
|
2022-11-25 11:40:23 +00:00
|
|
|
* Returns a tuple of two arrays with the first one containing all elements in
|
|
|
|
* the given array that match the given predicate and the second one containing
|
|
|
|
* all that do not.
|
2021-07-15 21:37:53 +00:00
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the elements in the array.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param array The array to partition.
|
|
|
|
* @param predicate The predicate function to determine which array an element
|
|
|
|
* belongs to.
|
|
|
|
*
|
|
|
|
* @returns A tuple of two arrays. The first array contains all elements that
|
|
|
|
* match the predicate, the second contains all elements that do not.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2021-07-30 14:49:46 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { partition } from "@std/collections/partition";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2021-07-30 14:49:46 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* const numbers = [5, 6, 7, 8, 9];
|
2023-08-25 09:04:43 +00:00
|
|
|
* const [even, odd] = partition(numbers, (it) => it % 2 === 0);
|
2021-07-15 21:37:53 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* assertEquals(even, [6, 8]);
|
|
|
|
* assertEquals(odd, [5, 7, 9]);
|
2021-07-15 21:37:53 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function partition<T>(
|
2023-05-30 23:48:21 +00:00
|
|
|
array: Iterable<T>,
|
2021-08-10 08:46:14 +00:00
|
|
|
predicate: (el: T) => boolean,
|
2023-11-21 12:37:43 +00:00
|
|
|
): [T[], T[]];
|
|
|
|
/**
|
|
|
|
* Returns a tuple of two arrays with the first one containing all elements in
|
|
|
|
* the given array that match the given predicate and the second one containing
|
|
|
|
* all that do not.
|
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* This version of the function is a type-guard version of the function. It
|
|
|
|
* allows you to specify a type-guard predicate function that narrows the type
|
|
|
|
* of the elements in the array.
|
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the elements in the array.
|
|
|
|
* @typeParam U The type of the elements that match the predicate.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param array The array to partition.
|
|
|
|
* @param predicate The type-guard predicate function to determine which array
|
|
|
|
* an element belongs to.
|
|
|
|
*
|
|
|
|
* @returns A tuple of two arrays. The first array contains all elements that
|
|
|
|
* match the predicate, the second contains all elements that do not.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { partition } from "@std/collections/partition";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
|
|
|
* const numbers = [5, 6, 7, 8, 9];
|
|
|
|
* const [even, odd] = partition(numbers, (it) => it % 2 === 0);
|
|
|
|
*
|
|
|
|
* assertEquals(even, [6, 8]);
|
|
|
|
* assertEquals(odd, [5, 7, 9]);
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function partition<T, U extends T>(
|
|
|
|
array: Iterable<T>,
|
|
|
|
predicate: (el: T) => el is U,
|
|
|
|
): [U[], Exclude<T, U>[]];
|
|
|
|
export function partition(
|
|
|
|
array: Iterable<unknown>,
|
|
|
|
predicate: (el: unknown) => boolean,
|
|
|
|
): [unknown[], unknown[]] {
|
|
|
|
const matches: Array<unknown> = [];
|
|
|
|
const rest: Array<unknown> = [];
|
2021-07-15 21:37:53 +00:00
|
|
|
|
|
|
|
for (const element of array) {
|
|
|
|
if (predicate(element)) {
|
|
|
|
matches.push(element);
|
|
|
|
} else {
|
|
|
|
rest.push(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [matches, rest];
|
|
|
|
}
|