Co-authored-by: Alex van Andel <me@alexvanandel.com> Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Co-authored-by: Morgan Vernay <morgan@cal.com> Co-authored-by: Omar López <zomars@me.com>
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
|
|
import { uniqueBy } from "./array";
|
|
|
|
describe("uniqueBy", () => {
|
|
it("should remove duplicates based on single key", () => {
|
|
const input = [
|
|
{ id: 1, name: "John" },
|
|
{ id: 1, name: "Jane" },
|
|
{ id: 2, name: "Doe" },
|
|
];
|
|
|
|
const result = uniqueBy(input, ["id"]);
|
|
expect(result).toHaveLength(2);
|
|
expect(result).toEqual([
|
|
{ id: 1, name: "John" },
|
|
{ id: 2, name: "Doe" },
|
|
]);
|
|
});
|
|
|
|
it("should remove duplicates based on multiple keys", () => {
|
|
const input = [
|
|
{ id: 1, type: "A", value: "first" },
|
|
{ id: 1, type: "A", value: "second" },
|
|
{ id: 1, type: "B", value: "third" },
|
|
{ id: 2, type: "A", value: "fourth" },
|
|
];
|
|
|
|
const result = uniqueBy(input, ["id", "type"]);
|
|
expect(result).toHaveLength(3);
|
|
expect(result).toEqual([
|
|
{ id: 1, type: "A", value: "first" },
|
|
{ id: 1, type: "B", value: "third" },
|
|
{ id: 2, type: "A", value: "fourth" },
|
|
]);
|
|
});
|
|
|
|
it("should handle empty array", () => {
|
|
const input: Array<{ id: number }> = [];
|
|
const result = uniqueBy(input, ["id"]);
|
|
expect(result).toEqual([]);
|
|
});
|
|
|
|
it("should handle array with single item", () => {
|
|
const input = [{ id: 1, name: "John" }];
|
|
const result = uniqueBy(input, ["id"]);
|
|
expect(result).toEqual(input);
|
|
});
|
|
});
|