deno.land / std@0.166.0 / collections / partition.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
/** * 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 * * Example: * * ```ts * import { partition } from "https://deno.land/std@$STD_VERSION/collections/partition.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * 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: readonly T[], predicate: (el: T) => el is U,): [U[], Exclude<T, U>[]];export function partition<T>( array: readonly T[], predicate: (el: T) => boolean,): [T[], T[]];export function partition( array: readonly unknown[], predicate: (el: unknown) => boolean,): [unknown[], unknown[]] { const matches: Array<unknown> = []; const rest: Array<unknown> = [];
for (const element of array) { if (predicate(element)) { matches.push(element); } else { rest.push(element); } }
return [matches, rest];}
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago