@helpers4/map 3.1.9 → 3.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/lib/index.js +11 -22
- package/lib/index.js.map +1 -1
- package/llms.txt +1 -1
- package/meta/api.json +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -49,6 +49,7 @@ A set of helpers for working with URLs.
|
|
|
49
49
|
- [number](../number)
|
|
50
50
|
- [object](../object)
|
|
51
51
|
- [observable](../observable)
|
|
52
|
+
- [package](../package)
|
|
52
53
|
- [promise](../promise)
|
|
53
54
|
- [set](../set)
|
|
54
55
|
- [string](../string)
|
|
@@ -65,7 +66,7 @@ A set of helpers for working with URLs.
|
|
|
65
66
|
|------|---------|-------------|-------------|
|
|
66
67
|
| animation | [@helpers4/animation](https://www.npmjs.com/package/@helpers4/animation) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/animation) | Timing and interpolation for animations |
|
|
67
68
|
| array | [@helpers4/array](https://www.npmjs.com/package/@helpers4/array) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/array) | Array manipulation utilities |
|
|
68
|
-
| binary | [@helpers4/binary](https://www.npmjs.com/package/@helpers4/binary) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/binary) | Read and
|
|
69
|
+
| binary | [@helpers4/binary](https://www.npmjs.com/package/@helpers4/binary) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/binary) | Read, write and identify binary formats |
|
|
69
70
|
| browser | [@helpers4/browser](https://www.npmjs.com/package/@helpers4/browser) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/browser) | Browser-specific utilities |
|
|
70
71
|
| ci | [@helpers4/ci](https://www.npmjs.com/package/@helpers4/ci) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/ci) | CI/CD workflow status utilities |
|
|
71
72
|
| color | [@helpers4/color](https://www.npmjs.com/package/@helpers4/color) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/color) | Color conversion and validation utilities |
|
|
@@ -85,6 +86,7 @@ A set of helpers for working with URLs.
|
|
|
85
86
|
| number | [@helpers4/number](https://www.npmjs.com/package/@helpers4/number) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/number) | Number utility functions and validation |
|
|
86
87
|
| object | [@helpers4/object](https://www.npmjs.com/package/@helpers4/object) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/object) | Object manipulation utilities |
|
|
87
88
|
| observable | [@helpers4/observable](https://www.npmjs.com/package/@helpers4/observable) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/observable) | Observable utilities and combinators |
|
|
89
|
+
| package | [@helpers4/package](https://www.npmjs.com/package/@helpers4/package) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/package) | Package metadata parsers |
|
|
88
90
|
| promise | [@helpers4/promise](https://www.npmjs.com/package/@helpers4/promise) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/promise) | Promise utilities and error handling |
|
|
89
91
|
| set | [@helpers4/set](https://www.npmjs.com/package/@helpers4/set) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/set) | Native Set manipulation utilities |
|
|
90
92
|
| string | [@helpers4/string](https://www.npmjs.com/package/@helpers4/string) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/string) | String manipulation and formatting utilities |
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// helpers/map/countBy.ts
|
|
2
2
|
/**
|
|
3
3
|
* This file is part of helpers4.
|
|
4
4
|
* Copyright (C) 2025 baxyz
|
|
@@ -22,8 +22,7 @@ function countBy(map, fn) {
|
|
|
22
22
|
}
|
|
23
23
|
return counts;
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
//#region helpers/map/every.ts
|
|
25
|
+
// helpers/map/every.ts
|
|
27
26
|
/**
|
|
28
27
|
* This file is part of helpers4.
|
|
29
28
|
* Copyright (C) 2025 baxyz
|
|
@@ -43,8 +42,7 @@ function every(map, predicate) {
|
|
|
43
42
|
for (const [key, value] of map) if (!predicate(value, key)) return false;
|
|
44
43
|
return true;
|
|
45
44
|
}
|
|
46
|
-
|
|
47
|
-
//#region helpers/map/filter.ts
|
|
45
|
+
// helpers/map/filter.ts
|
|
48
46
|
/**
|
|
49
47
|
* This file is part of helpers4.
|
|
50
48
|
* Copyright (C) 2025 baxyz
|
|
@@ -65,8 +63,7 @@ function filter(map, predicate) {
|
|
|
65
63
|
for (const [key, value] of map) if (predicate(value, key)) result.set(key, value);
|
|
66
64
|
return result;
|
|
67
65
|
}
|
|
68
|
-
|
|
69
|
-
//#region helpers/map/findKey.ts
|
|
66
|
+
// helpers/map/findKey.ts
|
|
70
67
|
/**
|
|
71
68
|
* This file is part of helpers4.
|
|
72
69
|
* Copyright (C) 2025 baxyz
|
|
@@ -85,8 +82,7 @@ function filter(map, predicate) {
|
|
|
85
82
|
function findKey(map, predicate) {
|
|
86
83
|
for (const [key, value] of map) if (predicate(value, key)) return key;
|
|
87
84
|
}
|
|
88
|
-
|
|
89
|
-
//#region helpers/map/findValue.ts
|
|
85
|
+
// helpers/map/findValue.ts
|
|
90
86
|
/**
|
|
91
87
|
* This file is part of helpers4.
|
|
92
88
|
* Copyright (C) 2025 baxyz
|
|
@@ -105,8 +101,7 @@ function findKey(map, predicate) {
|
|
|
105
101
|
function findValue(map, predicate) {
|
|
106
102
|
for (const [key, value] of map) if (predicate(value, key)) return value;
|
|
107
103
|
}
|
|
108
|
-
|
|
109
|
-
//#region helpers/map/hasValue.ts
|
|
104
|
+
// helpers/map/hasValue.ts
|
|
110
105
|
/**
|
|
111
106
|
* This file is part of helpers4.
|
|
112
107
|
* Copyright (C) 2025 baxyz
|
|
@@ -127,8 +122,7 @@ function hasValue(map, value) {
|
|
|
127
122
|
for (const v of map.values()) if (Object.is(v, value)) return true;
|
|
128
123
|
return false;
|
|
129
124
|
}
|
|
130
|
-
|
|
131
|
-
//#region helpers/map/mapKeys.ts
|
|
125
|
+
// helpers/map/mapKeys.ts
|
|
132
126
|
/**
|
|
133
127
|
* This file is part of helpers4.
|
|
134
128
|
* Copyright (C) 2025 baxyz
|
|
@@ -150,8 +144,7 @@ function mapKeys(map, fn) {
|
|
|
150
144
|
for (const [key, value] of map) result.set(fn(key, value), value);
|
|
151
145
|
return result;
|
|
152
146
|
}
|
|
153
|
-
|
|
154
|
-
//#region helpers/map/mapValues.ts
|
|
147
|
+
// helpers/map/mapValues.ts
|
|
155
148
|
/**
|
|
156
149
|
* This file is part of helpers4.
|
|
157
150
|
* Copyright (C) 2025 baxyz
|
|
@@ -172,8 +165,7 @@ function mapValues(map, fn) {
|
|
|
172
165
|
for (const [key, value] of map) result.set(key, fn(value, key));
|
|
173
166
|
return result;
|
|
174
167
|
}
|
|
175
|
-
|
|
176
|
-
//#region helpers/map/reduce.ts
|
|
168
|
+
// helpers/map/reduce.ts
|
|
177
169
|
/**
|
|
178
170
|
* This file is part of helpers4.
|
|
179
171
|
* Copyright (C) 2025 baxyz
|
|
@@ -195,8 +187,7 @@ function reduce(map, fn, initial) {
|
|
|
195
187
|
for (const [key, value] of map) accumulator = fn(accumulator, value, key);
|
|
196
188
|
return accumulator;
|
|
197
189
|
}
|
|
198
|
-
|
|
199
|
-
//#region helpers/map/some.ts
|
|
190
|
+
// helpers/map/some.ts
|
|
200
191
|
/**
|
|
201
192
|
* This file is part of helpers4.
|
|
202
193
|
* Copyright (C) 2025 baxyz
|
|
@@ -216,8 +207,7 @@ function some(map, predicate) {
|
|
|
216
207
|
for (const [key, value] of map) if (predicate(value, key)) return true;
|
|
217
208
|
return false;
|
|
218
209
|
}
|
|
219
|
-
|
|
220
|
-
//#region helpers/map/toMapByKey.ts
|
|
210
|
+
// helpers/map/toMapByKey.ts
|
|
221
211
|
/**
|
|
222
212
|
* This file is part of helpers4.
|
|
223
213
|
* Copyright (C) 2025 baxyz
|
|
@@ -243,7 +233,6 @@ function toMapByKey(items, fn) {
|
|
|
243
233
|
for (const item of items) result.set(fn(item), item);
|
|
244
234
|
return result;
|
|
245
235
|
}
|
|
246
|
-
//#endregion
|
|
247
236
|
export { countBy, every, filter, findKey, findValue, hasValue, mapKeys, mapValues, reduce, some, toMapByKey };
|
|
248
237
|
|
|
249
238
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../helpers/map/countBy.ts","../../../helpers/map/every.ts","../../../helpers/map/filter.ts","../../../helpers/map/findKey.ts","../../../helpers/map/findValue.ts","../../../helpers/map/hasValue.ts","../../../helpers/map/mapKeys.ts","../../../helpers/map/mapValues.ts","../../../helpers/map/reduce.ts","../../../helpers/map/some.ts","../../../helpers/map/toMapByKey.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Groups the entries of a Map by a derived key and counts how many fall into each group.\n * @param map - The Map to summarize\n * @param fn - Function deriving the grouping key from (value, key)\n * @returns A Map from derived key to the number of entries in that group\n * @example\n * countBy(new Map([['a', 1], ['b', 2], ['c', 3]]), value => value % 2 === 0 ? 'even' : 'odd')\n * // => Map(2) { 'odd' => 2, 'even' => 1 }\n * @since 3.0.3\n */\nexport function countBy<K, V, R>(\n map: ReadonlyMap<K, V>,\n fn: (value: V, key: K) => R,\n): Map<R, number> {\n const counts = new Map<R, number>();\n for (const [key, value] of map) {\n const group = fn(value, key);\n counts.set(group, (counts.get(group) ?? 0) + 1);\n }\n return counts;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks if every entry of a Map satisfies the predicate. Short-circuits on the first mismatch.\n * @param map - The Map to check\n * @param predicate - Function invoked with (value, key)\n * @returns `true` if all entries match (vacuously `true` for an empty map), `false` otherwise\n * @example\n * every(new Map([['a', 1], ['b', 2]]), value => value > 0)\n * // => true\n * @since 3.0.3\n */\nexport function every<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): boolean {\n for (const [key, value] of map) {\n if (!predicate(value, key)) return false;\n }\n return true;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Creates a new Map containing only the entries for which the predicate returns true.\n * @param map - The Map to filter\n * @param predicate - Function invoked with (value, key) — return true to keep the entry\n * @returns A new Map with only the matching entries\n * @example\n * filter(new Map([['a', 1], ['b', 2], ['c', 3]]), value => value % 2 === 0)\n * // => Map(1) { 'b' => 2 }\n * @since 3.0.3\n */\nexport function filter<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): Map<K, V> {\n const result = new Map<K, V>();\n for (const [key, value] of map) {\n if (predicate(value, key)) {\n result.set(key, value);\n }\n }\n return result;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Returns the first key of a Map whose entry satisfies the predicate, in insertion order.\n * @param map - The Map to search\n * @param predicate - Function invoked with (value, key)\n * @returns The matching key, or `undefined` if none matches\n * @example\n * findKey(new Map([['a', 1], ['b', 2]]), value => value > 1)\n * // => 'b'\n * @since 3.0.3\n */\nexport function findKey<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): K | undefined {\n for (const [key, value] of map) {\n if (predicate(value, key)) return key;\n }\n return undefined;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Returns the first value of a Map whose entry satisfies the predicate, in insertion order.\n * @param map - The Map to search\n * @param predicate - Function invoked with (value, key)\n * @returns The matching value, or `undefined` if none matches\n * @example\n * findValue(new Map([['a', 1], ['b', 2]]), value => value > 1)\n * // => 2\n * @since 3.0.3\n */\nexport function findValue<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): V | undefined {\n for (const [key, value] of map) {\n if (predicate(value, key)) return value;\n }\n return undefined;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks whether a value exists anywhere in a Map (`Map.prototype.has` checks keys, not values).\n * Uses `Object.is`-style equality (via `===`, with `NaN` matching `NaN`).\n * @param map - The Map to search\n * @param value - The value to look for\n * @returns `true` if the value is found in at least one entry\n * @example\n * hasValue(new Map([['a', 1], ['b', 2]]), 2)\n * // => true\n * @since 3.0.3\n */\nexport function hasValue<K, V>(map: ReadonlyMap<K, V>, value: V): boolean {\n for (const v of map.values()) {\n if (Object.is(v, value)) return true;\n }\n return false;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Creates a new Map with the same values but with each key transformed by a function.\n * If two entries derive the same new key, the later one (in insertion order) wins.\n * @param map - The Map to transform\n * @param fn - Function invoked with (key, value) that returns the new key\n * @returns A new Map with transformed keys\n * @example\n * mapKeys(new Map([['a', 1], ['b', 2]]), key => key.toUpperCase())\n * // => Map(2) { 'A' => 1, 'B' => 2 }\n * @since 3.0.3\n */\nexport function mapKeys<K, V, R>(\n map: ReadonlyMap<K, V>,\n fn: (key: K, value: V) => R,\n): Map<R, V> {\n const result = new Map<R, V>();\n for (const [key, value] of map) {\n result.set(fn(key, value), value);\n }\n return result;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Creates a new Map with the same keys but with each value transformed by a function.\n * @param map - The Map to transform\n * @param fn - Function invoked with (value, key) that returns the new value\n * @returns A new Map with transformed values\n * @example\n * mapValues(new Map([['a', 1], ['b', 2]]), value => value * 10)\n * // => Map(2) { 'a' => 10, 'b' => 20 }\n * @since 3.0.3\n */\nexport function mapValues<K, V, R>(\n map: ReadonlyMap<K, V>,\n fn: (value: V, key: K) => R,\n): Map<K, R> {\n const result = new Map<K, R>();\n for (const [key, value] of map) {\n result.set(key, fn(value, key));\n }\n return result;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Reduces a Map to a single value by applying a function to each entry, in insertion order.\n * @param map - The Map to reduce\n * @param fn - Function invoked with (accumulator, value, key)\n * @param initial - Initial accumulator value\n * @returns The final accumulator value\n * @example\n * reduce(new Map([['a', 1], ['b', 2]]), (acc, value) => acc + value, 0)\n * // => 3\n * @since 3.0.3\n */\nexport function reduce<K, V, R>(\n map: ReadonlyMap<K, V>,\n fn: (accumulator: R, value: V, key: K) => R,\n initial: R,\n): R {\n let accumulator = initial;\n for (const [key, value] of map) {\n accumulator = fn(accumulator, value, key);\n }\n return accumulator;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks if at least one entry of a Map satisfies the predicate. Short-circuits on the first match.\n * @param map - The Map to check\n * @param predicate - Function invoked with (value, key)\n * @returns `true` if at least one entry matches, `false` otherwise (including for an empty map)\n * @example\n * some(new Map([['a', 1], ['b', 2]]), value => value > 1)\n * // => true\n * @since 3.0.3\n */\nexport function some<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): boolean {\n for (const [key, value] of map) {\n if (predicate(value, key)) return true;\n }\n return false;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Builds a Map from an iterable of items, keyed by a derived key. When two items derive the\n * same key, the later item wins (last-write-wins, matching `Map.prototype.set` semantics).\n *\n * Named `toMapByKey`, not `keyBy` (lodash's name for the same idea) — lodash's `_.keyBy`\n * returns a plain object, not a `Map`; the `to<Type>` prefix (matching `toSorted`/`toReversed`)\n * makes the actual return type unambiguous.\n * @param items - The items to index\n * @param fn - Function deriving the key for each item\n * @returns A Map from derived key to item\n * @example\n * toMapByKey([{ id: 'a', n: 1 }, { id: 'b', n: 2 }], item => item.id)\n * // => Map(2) { 'a' => { id: 'a', n: 1 }, 'b' => { id: 'b', n: 2 } }\n * @since 3.0.3\n */\nexport function toMapByKey<T, K>(items: Iterable<T>, fn: (item: T) => K): Map<K, T> {\n const result = new Map<K, T>();\n for (const item of items) {\n result.set(fn(item), item);\n }\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,SAAgB,QACd,KACA,IACgB;CAChB,MAAM,yBAAS,IAAI,IAAe;CAClC,KAAK,MAAM,CAAC,KAAK,UAAU,KAAK;EAC9B,MAAM,QAAQ,GAAG,OAAO,GAAG;EAC3B,OAAO,IAAI,QAAQ,OAAO,IAAI,KAAK,KAAK,KAAK,CAAC;CAChD;CACA,OAAO;AACT;;;;;;;;;;;;;;;;;;ACVA,SAAgB,MACd,KACA,WACS;CACT,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,CAAC,UAAU,OAAO,GAAG,GAAG,OAAO;CAErC,OAAO;AACT;;;;;;;;;;;;;;;;;;ACRA,SAAgB,OACd,KACA,WACW;CACX,MAAM,yBAAS,IAAI,IAAU;CAC7B,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,UAAU,OAAO,GAAG,GACtB,OAAO,IAAI,KAAK,KAAK;CAGzB,OAAO;AACT;;;;;;;;;;;;;;;;;;ACXA,SAAgB,QACd,KACA,WACe;CACf,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,UAAU,OAAO,GAAG,GAAG,OAAO;AAGtC;;;;;;;;;;;;;;;;;;ACRA,SAAgB,UACd,KACA,WACe;CACf,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,UAAU,OAAO,GAAG,GAAG,OAAO;AAGtC;;;;;;;;;;;;;;;;;;;ACPA,SAAgB,SAAe,KAAwB,OAAmB;CACxE,KAAK,MAAM,KAAK,IAAI,OAAO,GACzB,IAAI,OAAO,GAAG,GAAG,KAAK,GAAG,OAAO;CAElC,OAAO;AACT;;;;;;;;;;;;;;;;;;;ACLA,SAAgB,QACd,KACA,IACW;CACX,MAAM,yBAAS,IAAI,IAAU;CAC7B,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,OAAO,IAAI,GAAG,KAAK,KAAK,GAAG,KAAK;CAElC,OAAO;AACT;;;;;;;;;;;;;;;;;;ACVA,SAAgB,UACd,KACA,IACW;CACX,MAAM,yBAAS,IAAI,IAAU;CAC7B,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,OAAO,IAAI,KAAK,GAAG,OAAO,GAAG,CAAC;CAEhC,OAAO;AACT;;;;;;;;;;;;;;;;;;;ACRA,SAAgB,OACd,KACA,IACA,SACG;CACH,IAAI,cAAc;CAClB,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,cAAc,GAAG,aAAa,OAAO,GAAG;CAE1C,OAAO;AACT;;;;;;;;;;;;;;;;;;ACXA,SAAgB,KACd,KACA,WACS;CACT,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,UAAU,OAAO,GAAG,GAAG,OAAO;CAEpC,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;ACHA,SAAgB,WAAiB,OAAoB,IAA+B;CAClF,MAAM,yBAAS,IAAI,IAAU;CAC7B,KAAK,MAAM,QAAQ,OACjB,OAAO,IAAI,GAAG,IAAI,GAAG,IAAI;CAE3B,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../helpers/map/countBy.ts","../../../helpers/map/every.ts","../../../helpers/map/filter.ts","../../../helpers/map/findKey.ts","../../../helpers/map/findValue.ts","../../../helpers/map/hasValue.ts","../../../helpers/map/mapKeys.ts","../../../helpers/map/mapValues.ts","../../../helpers/map/reduce.ts","../../../helpers/map/some.ts","../../../helpers/map/toMapByKey.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Groups the entries of a Map by a derived key and counts how many fall into each group.\n * @param map - The Map to summarize\n * @param fn - Function deriving the grouping key from (value, key)\n * @returns A Map from derived key to the number of entries in that group\n * @example\n * countBy(new Map([['a', 1], ['b', 2], ['c', 3]]), value => value % 2 === 0 ? 'even' : 'odd')\n * // => Map(2) { 'odd' => 2, 'even' => 1 }\n * @since 3.0.3\n */\nexport function countBy<K, V, R>(\n map: ReadonlyMap<K, V>,\n fn: (value: V, key: K) => R,\n): Map<R, number> {\n const counts = new Map<R, number>();\n for (const [key, value] of map) {\n const group = fn(value, key);\n counts.set(group, (counts.get(group) ?? 0) + 1);\n }\n return counts;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks if every entry of a Map satisfies the predicate. Short-circuits on the first mismatch.\n * @param map - The Map to check\n * @param predicate - Function invoked with (value, key)\n * @returns `true` if all entries match (vacuously `true` for an empty map), `false` otherwise\n * @example\n * every(new Map([['a', 1], ['b', 2]]), value => value > 0)\n * // => true\n * @since 3.0.3\n */\nexport function every<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): boolean {\n for (const [key, value] of map) {\n if (!predicate(value, key)) return false;\n }\n return true;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Creates a new Map containing only the entries for which the predicate returns true.\n * @param map - The Map to filter\n * @param predicate - Function invoked with (value, key) — return true to keep the entry\n * @returns A new Map with only the matching entries\n * @example\n * filter(new Map([['a', 1], ['b', 2], ['c', 3]]), value => value % 2 === 0)\n * // => Map(1) { 'b' => 2 }\n * @since 3.0.3\n */\nexport function filter<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): Map<K, V> {\n const result = new Map<K, V>();\n for (const [key, value] of map) {\n if (predicate(value, key)) {\n result.set(key, value);\n }\n }\n return result;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Returns the first key of a Map whose entry satisfies the predicate, in insertion order.\n * @param map - The Map to search\n * @param predicate - Function invoked with (value, key)\n * @returns The matching key, or `undefined` if none matches\n * @example\n * findKey(new Map([['a', 1], ['b', 2]]), value => value > 1)\n * // => 'b'\n * @since 3.0.3\n */\nexport function findKey<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): K | undefined {\n for (const [key, value] of map) {\n if (predicate(value, key)) return key;\n }\n return undefined;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Returns the first value of a Map whose entry satisfies the predicate, in insertion order.\n * @param map - The Map to search\n * @param predicate - Function invoked with (value, key)\n * @returns The matching value, or `undefined` if none matches\n * @example\n * findValue(new Map([['a', 1], ['b', 2]]), value => value > 1)\n * // => 2\n * @since 3.0.3\n */\nexport function findValue<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): V | undefined {\n for (const [key, value] of map) {\n if (predicate(value, key)) return value;\n }\n return undefined;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks whether a value exists anywhere in a Map (`Map.prototype.has` checks keys, not values).\n * Uses `Object.is`-style equality (via `===`, with `NaN` matching `NaN`).\n * @param map - The Map to search\n * @param value - The value to look for\n * @returns `true` if the value is found in at least one entry\n * @example\n * hasValue(new Map([['a', 1], ['b', 2]]), 2)\n * // => true\n * @since 3.0.3\n */\nexport function hasValue<K, V>(map: ReadonlyMap<K, V>, value: V): boolean {\n for (const v of map.values()) {\n if (Object.is(v, value)) return true;\n }\n return false;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Creates a new Map with the same values but with each key transformed by a function.\n * If two entries derive the same new key, the later one (in insertion order) wins.\n * @param map - The Map to transform\n * @param fn - Function invoked with (key, value) that returns the new key\n * @returns A new Map with transformed keys\n * @example\n * mapKeys(new Map([['a', 1], ['b', 2]]), key => key.toUpperCase())\n * // => Map(2) { 'A' => 1, 'B' => 2 }\n * @since 3.0.3\n */\nexport function mapKeys<K, V, R>(\n map: ReadonlyMap<K, V>,\n fn: (key: K, value: V) => R,\n): Map<R, V> {\n const result = new Map<R, V>();\n for (const [key, value] of map) {\n result.set(fn(key, value), value);\n }\n return result;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Creates a new Map with the same keys but with each value transformed by a function.\n * @param map - The Map to transform\n * @param fn - Function invoked with (value, key) that returns the new value\n * @returns A new Map with transformed values\n * @example\n * mapValues(new Map([['a', 1], ['b', 2]]), value => value * 10)\n * // => Map(2) { 'a' => 10, 'b' => 20 }\n * @since 3.0.3\n */\nexport function mapValues<K, V, R>(\n map: ReadonlyMap<K, V>,\n fn: (value: V, key: K) => R,\n): Map<K, R> {\n const result = new Map<K, R>();\n for (const [key, value] of map) {\n result.set(key, fn(value, key));\n }\n return result;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Reduces a Map to a single value by applying a function to each entry, in insertion order.\n * @param map - The Map to reduce\n * @param fn - Function invoked with (accumulator, value, key)\n * @param initial - Initial accumulator value\n * @returns The final accumulator value\n * @example\n * reduce(new Map([['a', 1], ['b', 2]]), (acc, value) => acc + value, 0)\n * // => 3\n * @since 3.0.3\n */\nexport function reduce<K, V, R>(\n map: ReadonlyMap<K, V>,\n fn: (accumulator: R, value: V, key: K) => R,\n initial: R,\n): R {\n let accumulator = initial;\n for (const [key, value] of map) {\n accumulator = fn(accumulator, value, key);\n }\n return accumulator;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks if at least one entry of a Map satisfies the predicate. Short-circuits on the first match.\n * @param map - The Map to check\n * @param predicate - Function invoked with (value, key)\n * @returns `true` if at least one entry matches, `false` otherwise (including for an empty map)\n * @example\n * some(new Map([['a', 1], ['b', 2]]), value => value > 1)\n * // => true\n * @since 3.0.3\n */\nexport function some<K, V>(\n map: ReadonlyMap<K, V>,\n predicate: (value: V, key: K) => boolean,\n): boolean {\n for (const [key, value] of map) {\n if (predicate(value, key)) return true;\n }\n return false;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Builds a Map from an iterable of items, keyed by a derived key. When two items derive the\n * same key, the later item wins (last-write-wins, matching `Map.prototype.set` semantics).\n *\n * Named `toMapByKey`, not `keyBy` (lodash's name for the same idea) — lodash's `_.keyBy`\n * returns a plain object, not a `Map`; the `to<Type>` prefix (matching `toSorted`/`toReversed`)\n * makes the actual return type unambiguous.\n * @param items - The items to index\n * @param fn - Function deriving the key for each item\n * @returns A Map from derived key to item\n * @example\n * toMapByKey([{ id: 'a', n: 1 }, { id: 'b', n: 2 }], item => item.id)\n * // => Map(2) { 'a' => { id: 'a', n: 1 }, 'b' => { id: 'b', n: 2 } }\n * @since 3.0.3\n */\nexport function toMapByKey<T, K>(items: Iterable<T>, fn: (item: T) => K): Map<K, T> {\n const result = new Map<K, T>();\n for (const item of items) {\n result.set(fn(item), item);\n }\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,SAAgB,QACd,KACA,IACgB;CAChB,MAAM,yBAAS,IAAI,IAAe;CAClC,KAAK,MAAM,CAAC,KAAK,UAAU,KAAK;EAC9B,MAAM,QAAQ,GAAG,OAAO,GAAG;EAC3B,OAAO,IAAI,QAAQ,OAAO,IAAI,KAAK,KAAK,KAAK,CAAC;CAChD;CACA,OAAO;AACT;;;;;;;;;;;;;;;;;ACVA,SAAgB,MACd,KACA,WACS;CACT,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,CAAC,UAAU,OAAO,GAAG,GAAG,OAAO;CAErC,OAAO;AACT;;;;;;;;;;;;;;;;;ACRA,SAAgB,OACd,KACA,WACW;CACX,MAAM,yBAAS,IAAI,IAAU;CAC7B,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,UAAU,OAAO,GAAG,GACtB,OAAO,IAAI,KAAK,KAAK;CAGzB,OAAO;AACT;;;;;;;;;;;;;;;;;ACXA,SAAgB,QACd,KACA,WACe;CACf,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,UAAU,OAAO,GAAG,GAAG,OAAO;AAGtC;;;;;;;;;;;;;;;;;ACRA,SAAgB,UACd,KACA,WACe;CACf,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,UAAU,OAAO,GAAG,GAAG,OAAO;AAGtC;;;;;;;;;;;;;;;;;;ACPA,SAAgB,SAAe,KAAwB,OAAmB;CACxE,KAAK,MAAM,KAAK,IAAI,OAAO,GACzB,IAAI,OAAO,GAAG,GAAG,KAAK,GAAG,OAAO;CAElC,OAAO;AACT;;;;;;;;;;;;;;;;;;ACLA,SAAgB,QACd,KACA,IACW;CACX,MAAM,yBAAS,IAAI,IAAU;CAC7B,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,OAAO,IAAI,GAAG,KAAK,KAAK,GAAG,KAAK;CAElC,OAAO;AACT;;;;;;;;;;;;;;;;;ACVA,SAAgB,UACd,KACA,IACW;CACX,MAAM,yBAAS,IAAI,IAAU;CAC7B,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,OAAO,IAAI,KAAK,GAAG,OAAO,GAAG,CAAC;CAEhC,OAAO;AACT;;;;;;;;;;;;;;;;;;ACRA,SAAgB,OACd,KACA,IACA,SACG;CACH,IAAI,cAAc;CAClB,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,cAAc,GAAG,aAAa,OAAO,GAAG;CAE1C,OAAO;AACT;;;;;;;;;;;;;;;;;ACXA,SAAgB,KACd,KACA,WACS;CACT,KAAK,MAAM,CAAC,KAAK,UAAU,KACzB,IAAI,UAAU,OAAO,GAAG,GAAG,OAAO;CAEpC,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;ACHA,SAAgB,WAAiB,OAAoB,IAA+B;CAClF,MAAM,yBAAS,IAAI,IAAU;CAC7B,KAAK,MAAM,QAAQ,OACjB,OAAO,IAAI,GAAG,IAAI,GAAG,IAAI;CAE3B,OAAO;AACT"}
|
package/llms.txt
CHANGED
package/meta/api.json
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/map",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.10",
|
|
4
4
|
"description": "A set of helpers in TS/JS, compatible with tree-shaking, for map.",
|
|
5
5
|
"author": "baxyz <craft@baxyz.dev>",
|
|
6
|
-
"license": "LGPL-3.0",
|
|
6
|
+
"license": "LGPL-3.0-or-later",
|
|
7
7
|
"homepage": "https://helpers4.dev/typescript/categories/map/",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|