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 { mapValues } from "./map_values.ts";
|
|
|
|
|
|
|
|
function mapValuesTest<T, O>(
|
2021-08-10 08:46:14 +00:00
|
|
|
input: [Record<string, T>, (value: T) => O],
|
2021-07-15 21:37:53 +00:00
|
|
|
expected: Record<string, O>,
|
|
|
|
message?: string,
|
|
|
|
) {
|
|
|
|
const actual = mapValues(...input);
|
|
|
|
assertEquals(actual, expected, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapValues() handles no mutation",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
const object = { a: 5, b: true };
|
|
|
|
mapValues(object, (it) => it ?? "nothing");
|
|
|
|
|
|
|
|
assertEquals(object, { a: 5, b: true });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapValues() handles empty input",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapValuesTest(
|
|
|
|
[{}, (it) => it],
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapValues() handles identity",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapValuesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
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: "mapValues() handles falsy values",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapValuesTest<number, null | undefined | string | boolean>(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"something": 0,
|
|
|
|
"yetanotherkey": 1,
|
|
|
|
"andonemore": 2,
|
|
|
|
"lastone": 3,
|
|
|
|
},
|
|
|
|
(it) => [null, undefined, "", false][it],
|
|
|
|
],
|
|
|
|
{
|
|
|
|
"something": null,
|
|
|
|
"yetanotherkey": undefined,
|
|
|
|
"andonemore": "",
|
|
|
|
"lastone": false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapValues() handles normal mappers",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
mapValuesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"FoodFile": "/home/deno/food.txt",
|
|
|
|
"CalendarFile": "/home/deno/weekend.cal",
|
|
|
|
},
|
2022-12-12 06:21:59 +00:00
|
|
|
(path) => path.slice(path.lastIndexOf(".")),
|
2021-07-15 21:37:53 +00:00
|
|
|
],
|
|
|
|
{
|
|
|
|
"FoodFile": ".txt",
|
|
|
|
"CalendarFile": ".cal",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
mapValuesTest(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"Curry": 20,
|
|
|
|
"Beans": 2,
|
|
|
|
"Rice": 3.5,
|
|
|
|
},
|
|
|
|
(price) => price.toFixed(2),
|
|
|
|
],
|
|
|
|
{
|
|
|
|
"Curry": "20.00",
|
|
|
|
"Beans": "2.00",
|
|
|
|
"Rice": "3.50",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2023-12-18 10:12:37 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapValues() preserves key type (Record)",
|
2023-12-18 10:12:37 +00:00
|
|
|
fn() {
|
|
|
|
type Variants = "a" | "b";
|
|
|
|
const input: Record<Variants, string> = { a: "a", b: "b" };
|
|
|
|
const actual = mapValues(
|
|
|
|
input,
|
|
|
|
(_: string) => 1,
|
|
|
|
);
|
|
|
|
const expected = { a: 1, b: 1 };
|
|
|
|
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "mapValues() preserves key type (Partial Record)",
|
2023-12-18 10:12:37 +00:00
|
|
|
fn() {
|
|
|
|
type Variants = "a" | "b";
|
|
|
|
const input: Partial<Record<Variants, string>> = { a: "a" };
|
|
|
|
const actual = mapValues(
|
|
|
|
input,
|
|
|
|
(_: string) => 1,
|
|
|
|
);
|
|
|
|
const expected = { a: 1 };
|
|
|
|
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
},
|
|
|
|
});
|
2024-01-24 10:27:04 +00:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "mapValues() pass key to transformer",
|
|
|
|
fn() {
|
|
|
|
const key = "key";
|
|
|
|
const actual = mapValues(
|
|
|
|
{ [key]: "value" },
|
|
|
|
(_, k) => k,
|
|
|
|
);
|
|
|
|
const expected = { [key]: key };
|
|
|
|
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
},
|
|
|
|
});
|