2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-05-07 06:53:59 +00:00
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2023-05-07 06:53:59 +00:00
|
|
|
import { partitionEntries } from "./partition_entries.ts";
|
|
|
|
|
|
|
|
function partitionEntriesTest<T>(
|
|
|
|
input: [Record<string, T>, (entry: [string, T]) => boolean],
|
|
|
|
expected: [match: Record<string, T>, rest: Record<string, T>],
|
|
|
|
message?: string,
|
|
|
|
) {
|
|
|
|
const actual = partitionEntries(...input);
|
|
|
|
assertEquals(actual, expected, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partitionEntries() handles no mutation",
|
2023-05-07 06:53:59 +00:00
|
|
|
fn() {
|
|
|
|
const object = { a: 5, b: true };
|
|
|
|
partitionEntries(object, ([key]) => key !== "a");
|
|
|
|
|
|
|
|
assertEquals(object, { a: 5, b: true });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partitionEntries() handles empty input",
|
2023-05-07 06:53:59 +00:00
|
|
|
fn() {
|
|
|
|
partitionEntriesTest(
|
|
|
|
[{}, () => true],
|
|
|
|
[{}, {}],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partitionEntries() handles identity",
|
2023-05-07 06:53:59 +00:00
|
|
|
fn() {
|
|
|
|
partitionEntriesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
foo: true,
|
|
|
|
bar: "lorem",
|
|
|
|
1: -5,
|
|
|
|
},
|
|
|
|
() => true,
|
|
|
|
],
|
|
|
|
[{
|
|
|
|
foo: true,
|
|
|
|
bar: "lorem",
|
|
|
|
1: -5,
|
|
|
|
}, {}],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partitionEntries() handles clean object",
|
2023-05-07 06:53:59 +00:00
|
|
|
fn() {
|
|
|
|
partitionEntriesTest(
|
|
|
|
[
|
|
|
|
{ test: "foo", "": [] },
|
|
|
|
() => false,
|
|
|
|
],
|
|
|
|
[{}, { test: "foo", "": [] }],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "partitionEntries() handles filters",
|
2023-05-07 06:53:59 +00:00
|
|
|
fn() {
|
|
|
|
partitionEntriesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"Anna": 22,
|
|
|
|
"Kim": 24,
|
|
|
|
"Karen": 33,
|
|
|
|
"Claudio": 11,
|
|
|
|
"Karl": 45,
|
|
|
|
},
|
|
|
|
([name, age]) => name.startsWith("K") && age > 30,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"Karen": 33,
|
|
|
|
"Karl": 45,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Anna": 22,
|
|
|
|
"Kim": 24,
|
|
|
|
"Claudio": 11,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
partitionEntriesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"A": true,
|
|
|
|
"b": "foo",
|
|
|
|
"C": 5,
|
|
|
|
"d": -2,
|
|
|
|
"": false,
|
|
|
|
},
|
|
|
|
([key]) => key.toUpperCase() === key,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"A": true,
|
|
|
|
"C": 5,
|
|
|
|
"": false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"b": "foo",
|
|
|
|
"d": -2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|