deno.land / std@0.224.0 / collections / map_values.ts

نووسراو ببینە
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
// Copyright 2018-2024 the Deno authors. All rights reserved. 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. * * @example * ```ts * import { mapValues } from "https://deno.land/std@$STD_VERSION/collections/map_values.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts"; * * const usersById = { * "a5ec": { name: "Mischa" }, * "de4f": { name: "Kim" }, * }; * const namesById = mapValues(usersById, (it) => it.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. * * @example * ```ts * import { mapValues } from "https://deno.land/std@$STD_VERSION/collections/map_values.ts"; * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts"; * * const usersById = { * "a5ec": { name: "Mischa" }, * "de4f": { name: "Kim" }, * }; * const namesById = mapValues(usersById, (it) => it.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 ret: any = {}; const entries = Object.entries<T>(record);
for (const [key, value] of entries) { const mappedValue = transformer(value, key as K);
ret[key] = mappedValue; }
return ret;}
std

Version Info

Tagged at
3 weeks ago