2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-07-15 21:37:53 +00:00
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2021-07-15 21:37:53 +00:00
|
|
|
import { partition } from "./partition.ts";
|
|
|
|
|
|
|
|
function partitionTest<I>(
|
|
|
|
input: [Array<I>, (element: I) => boolean],
|
|
|
|
expected: [Array<I>, Array<I>],
|
|
|
|
message?: string,
|
|
|
|
) {
|
|
|
|
const actual = partition(...input);
|
|
|
|
assertEquals(actual, expected, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partition() handles no mutation",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
const array = [1, 2, 3];
|
|
|
|
partition(array, (it) => it % 2 === 0);
|
|
|
|
|
|
|
|
assertEquals(array, [1, 2, 3]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partition() handles empty input",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
partitionTest(
|
|
|
|
[[], () => true],
|
|
|
|
[[], []],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partition() handles all match",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
partitionTest(
|
|
|
|
[[2, 4, 6], (it) => it % 2 === 0],
|
|
|
|
[[2, 4, 6], []],
|
|
|
|
);
|
|
|
|
partitionTest(
|
|
|
|
[["foo", "bar"], (it) => it.length > 0],
|
|
|
|
[["foo", "bar"], []],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partition() handles none match",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
partitionTest(
|
|
|
|
[[3, 7, 5], (it) => it % 2 === 0],
|
|
|
|
[[], [3, 7, 5]],
|
|
|
|
);
|
|
|
|
partitionTest(
|
|
|
|
[["foo", "bar"], (it) => it.startsWith("z")],
|
|
|
|
[[], ["foo", "bar"]],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partition() handles some match",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
partitionTest(
|
|
|
|
[[13, 4, 13, 8], (it) => it % 2 === 0],
|
|
|
|
[[4, 8], [13, 13]],
|
|
|
|
);
|
|
|
|
partitionTest(
|
|
|
|
[["foo", "bar", ""], (it) => it.length > 0],
|
|
|
|
[["foo", "bar"], [""]],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|