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 { mapEntries } from "./map_entries.ts";
|
|
|
|
|
|
|
|
function mapEntriesTest<T, O>(
|
2021-08-10 08:46:14 +00:00
|
|
|
input: [Record<string, T>, (entry: [string, T]) => [string, O]],
|
2021-07-15 21:37:53 +00:00
|
|
|
expected: Record<string, O>,
|
|
|
|
message?: string,
|
|
|
|
) {
|
|
|
|
const actual = mapEntries(...input);
|
|
|
|
assertEquals(actual, expected, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapEntries() handles no mutation",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
const object = { a: 5, b: true };
|
|
|
|
mapEntries(object, ([key, value]) => [`${key}a`, value]);
|
|
|
|
|
|
|
|
assertEquals(object, { a: 5, b: true });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapEntries() handles empty input",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapEntriesTest(
|
|
|
|
[{}, (it) => it],
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapEntries() handles identity",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapEntriesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
foo: true,
|
|
|
|
bar: "lorem",
|
|
|
|
1: -5,
|
|
|
|
},
|
|
|
|
(it) => it,
|
|
|
|
],
|
|
|
|
{
|
|
|
|
foo: true,
|
|
|
|
bar: "lorem",
|
|
|
|
1: -5,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapEntries() handles to constant entry",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapEntriesTest(
|
|
|
|
[
|
|
|
|
{ test: "foo", "": [] },
|
|
|
|
() => ["b", 5],
|
|
|
|
],
|
|
|
|
{ b: 5 },
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapEntries() handles overlapping keys",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapEntriesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"Anna": 22,
|
|
|
|
"Kim": 24,
|
|
|
|
"Karen": 33,
|
|
|
|
"Claudio": 11,
|
|
|
|
"Karl": 45,
|
|
|
|
},
|
|
|
|
([name, age]) => [name.charAt(0), age - 10],
|
|
|
|
],
|
|
|
|
{
|
|
|
|
"A": 12,
|
|
|
|
"K": 35,
|
|
|
|
"C": 1,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
mapEntriesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"ad04": "Kim",
|
|
|
|
"e5f1": "Marija",
|
|
|
|
"10a8": "Kim",
|
|
|
|
},
|
|
|
|
([key, value]) => [value, key],
|
|
|
|
],
|
|
|
|
{
|
|
|
|
"Kim": "10a8",
|
|
|
|
"Marija": "e5f1",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapEntries() handles normal mappers",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapEntriesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"Mine": "Aliens",
|
|
|
|
"Umse": "Feiert das",
|
|
|
|
"ZM": "36 Grad",
|
|
|
|
},
|
|
|
|
([artist, track]) => [artist, `${artist} - ${track}`],
|
|
|
|
],
|
|
|
|
{
|
|
|
|
"Mine": "Mine - Aliens",
|
|
|
|
"Umse": "Umse - Feiert das",
|
|
|
|
"ZM": "ZM - 36 Grad",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
mapEntriesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"ad04": { name: "Object A", type: "Asset" },
|
|
|
|
"e5f1": { name: "Onboarding", type: "Process" },
|
|
|
|
"10a8": { name: "Marija", type: "Employee" },
|
|
|
|
},
|
|
|
|
([_, value]) => [value.type, value],
|
|
|
|
],
|
|
|
|
{
|
|
|
|
"Asset": { name: "Object A", type: "Asset" },
|
|
|
|
"Process": { name: "Onboarding", type: "Process" },
|
|
|
|
"Employee": { name: "Marija", type: "Employee" },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|