1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x6
x6
x6
 
 
 
x24
x24
 
x24
x58
 
x58
x58
 
x24
x24 |
|
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
/**
* Applies the given transformer to all values in the given record and returns a
* new record containing the resulting keys associated to the last value that
* produced them.
*
* @typeParam T The type of the values in the input record.
* @typeParam O The type of the values in the output record.
* @typeParam K The type of the keys in the input and output records.
*
* @param record The record to map values from.
* @param transformer The function to transform each value.
*
* @returns A new record with all values transformed by the given transformer.
*
* @example Basic usage
* ```ts
* import { mapValues } from "@std/collections/map-values";
* import { assertEquals } from "@std/assert";
*
* const usersById = {
* a5ec: { name: "Mischa" },
* de4f: { name: "Kim" },
* };
* const namesById = mapValues(usersById, (user) => user.name);
*
* assertEquals(
* namesById,
* {
* a5ec: "Mischa",
* de4f: "Kim",
* },
* );
* ```
*/
export function mapValues<T, O, K extends string>(
record: Readonly<Record<K, T>>,
transformer: (value: T, key: K) => O,
): Record<K, O>;
/**
* Applies the given transformer to all values in the given record and returns a
* new record containing the resulting keys associated to the last value that
* produced them.
*
* @typeParam T The type of the values in the input record.
* @typeParam O The type of the values in the output record.
* @typeParam K The type of the keys in the input and output records.
*
* @param record The record to map values from.
* @param transformer The function to transform each value.
*
* @returns A new record with all values transformed by the given transformer.
*
* @example Basic usage
* ```ts
* import { mapValues } from "@std/collections/map-values";
* import { assertEquals } from "@std/assert";
*
* const usersById = {
* "a5ec": { name: "Mischa" },
* "de4f": { name: "Kim" },
* };
* const namesById = mapValues(usersById, (user) => user.name);
*
* assertEquals(
* namesById,
* {
* "a5ec": "Mischa",
* "de4f": "Kim",
* },
* );
* ```
*/
export function mapValues<T, O, K extends string>(
record: Readonly<Partial<Record<K, T>>>,
transformer: (value: T, key: K) => O,
): Partial<Record<K, O>>;
export function mapValues<T, O, K extends string>(
record: Record<K, T>,
transformer: (value: T, key: K) => O,
// deno-lint-ignore no-explicit-any
): any {
// deno-lint-ignore no-explicit-any
const result: any = {};
const entries = Object.entries<T>(record);
for (const [key, value] of entries) {
const mappedValue = transformer(value, key as K);
result[key] = mappedValue;
}
return result;
}
|