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 { distinctBy } from "./distinct_by.ts";
|
|
|
|
|
|
|
|
function distinctByTest<I>(
|
|
|
|
array: Array<I>,
|
|
|
|
selector: (element: I) => unknown,
|
|
|
|
expected: Array<I>,
|
|
|
|
message?: string,
|
|
|
|
) {
|
|
|
|
const actual = distinctBy(array, selector);
|
|
|
|
assertEquals(actual, expected, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "distinctBy() handles identities on empty array",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
distinctByTest(
|
|
|
|
[],
|
|
|
|
() => {},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "distinctBy() gets head on noop selector",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
distinctByTest(
|
|
|
|
[25, "asdf", true],
|
|
|
|
() => {},
|
|
|
|
[25],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "distinctBy() removes duplicates and preserves order on identity",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
distinctByTest(
|
|
|
|
[true, "asdf", 4, "asdf", true],
|
|
|
|
(it) => it,
|
|
|
|
[true, "asdf", 4],
|
|
|
|
);
|
|
|
|
distinctByTest(
|
|
|
|
[null, undefined, null, "foo", undefined],
|
|
|
|
(it) => it,
|
|
|
|
[null, undefined, "foo"],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "distinctBy() does not check for deep equality on identity",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
const objects = [{ foo: "bar" }, { foo: "bar" }];
|
|
|
|
distinctByTest(
|
|
|
|
objects,
|
|
|
|
(it) => it,
|
|
|
|
objects,
|
|
|
|
);
|
|
|
|
|
|
|
|
const arrays = [[], []];
|
|
|
|
distinctByTest(
|
|
|
|
arrays,
|
|
|
|
(it) => it,
|
|
|
|
arrays,
|
|
|
|
);
|
|
|
|
|
|
|
|
const nans = [NaN, NaN];
|
|
|
|
distinctByTest(
|
|
|
|
nans,
|
|
|
|
(it) => it,
|
|
|
|
[nans[0]],
|
|
|
|
);
|
|
|
|
|
|
|
|
const noops = [() => {}, () => {}];
|
|
|
|
distinctByTest(
|
|
|
|
noops,
|
|
|
|
(it) => it,
|
|
|
|
noops,
|
|
|
|
);
|
|
|
|
|
|
|
|
const sets = [new Set(), new Set()];
|
|
|
|
distinctByTest(
|
|
|
|
sets,
|
|
|
|
(it) => it,
|
|
|
|
sets,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "distinctBy() handles distincts by selected value and preserves order",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
const kim = { name: "Kim", age: 22 };
|
|
|
|
const arthur = { name: "Arthur", age: 22 };
|
|
|
|
const anna = { name: "Anna", age: 48 };
|
|
|
|
const karl = { name: "Karl", age: 48 };
|
|
|
|
const people = [kim, arthur, anna, karl];
|
|
|
|
|
|
|
|
distinctByTest(
|
|
|
|
people,
|
|
|
|
(it) => it.name.charAt(0),
|
|
|
|
[kim, arthur],
|
|
|
|
);
|
|
|
|
distinctByTest(
|
|
|
|
people,
|
|
|
|
(it) => it.age,
|
|
|
|
[kim, anna],
|
|
|
|
);
|
|
|
|
distinctByTest(
|
|
|
|
people,
|
|
|
|
(it) => it.name.length,
|
|
|
|
[kim, arthur, anna],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|