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 { mapKeys } from "./map_keys.ts";
|
|
|
|
|
|
|
|
function mapKeysTest<T>(
|
2021-08-10 08:46:14 +00:00
|
|
|
input: [Record<string, T>, (key: string) => string],
|
2021-07-15 21:37:53 +00:00
|
|
|
expected: Record<string, T>,
|
|
|
|
message?: string,
|
|
|
|
) {
|
|
|
|
const actual = mapKeys(...input);
|
|
|
|
assertEquals(actual, expected, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapKeys() handles no mutation",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
const object = { a: 5, b: true };
|
|
|
|
mapKeys(object, (it) => `${it}a`);
|
|
|
|
|
|
|
|
assertEquals(object, { a: 5, b: true });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapKeys() handles empty input",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapKeysTest(
|
|
|
|
[{}, (it) => it],
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapKeys() handles identity",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapKeysTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
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: "mapKeys() handles to constant key",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapKeysTest(
|
|
|
|
[
|
|
|
|
{ test: "foo", "": [] },
|
|
|
|
() => "a",
|
|
|
|
],
|
|
|
|
{ a: [] },
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapKeys() handles overlapping keys",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapKeysTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"Anna": 22,
|
|
|
|
"Kim": 24,
|
|
|
|
"Karen": 33,
|
|
|
|
"Claudio": 11,
|
|
|
|
"Karl": 45,
|
|
|
|
},
|
|
|
|
(name) => name.charAt(0),
|
|
|
|
],
|
|
|
|
{
|
|
|
|
"A": 22,
|
|
|
|
"K": 45,
|
|
|
|
"C": 11,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
mapKeysTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"ad04": "foo",
|
|
|
|
"ad28": "bar",
|
|
|
|
"100f": "dino",
|
|
|
|
},
|
2022-12-12 06:21:59 +00:00
|
|
|
(it) => it.slice(0, 2),
|
2021-07-15 21:37:53 +00:00
|
|
|
],
|
|
|
|
{
|
|
|
|
"ad": "bar",
|
|
|
|
"10": "dino",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapKeys() handles empty key",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapKeysTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"ab": 22,
|
|
|
|
"a": 24,
|
|
|
|
"bcd": 33,
|
|
|
|
"d": 11,
|
|
|
|
},
|
2022-12-12 06:21:59 +00:00
|
|
|
(key) => key.slice(1),
|
2021-07-15 21:37:53 +00:00
|
|
|
],
|
|
|
|
{
|
|
|
|
"b": 22,
|
|
|
|
"": 11,
|
|
|
|
"cd": 33,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapKeys() handles normal mappers",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapKeysTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"/home/deno/food.txt": "Plants, preferably fruit",
|
|
|
|
"/home/deno/other-dinos.txt": "Noderaptor, Pythonoctorus",
|
|
|
|
},
|
2024-02-02 06:01:54 +00:00
|
|
|
(path) => (path.split("/").slice(-1) as [string])[0],
|
2021-07-15 21:37:53 +00:00
|
|
|
],
|
|
|
|
{
|
|
|
|
"food.txt": "Plants, preferably fruit",
|
|
|
|
"other-dinos.txt": "Noderaptor, Pythonoctorus",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
mapKeysTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"EUR": 1200,
|
|
|
|
"USD": 1417,
|
|
|
|
"JPY": 1563,
|
|
|
|
},
|
|
|
|
(currencyCode) =>
|
|
|
|
({ EUR: "Euro", USD: "US Dollar", JPY: "Japanese Yen" })[
|
|
|
|
currencyCode
|
|
|
|
] ?? "_",
|
|
|
|
],
|
|
|
|
{
|
|
|
|
"Euro": 1200,
|
|
|
|
"US Dollar": 1417,
|
|
|
|
"Japanese Yen": 1563,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|