@monstermann/map 0.0.0

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.
Files changed (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +9 -0
  3. package/dist/Map/clone.d.ts +32 -0
  4. package/dist/Map/clone.js +33 -0
  5. package/dist/Map/compact.d.ts +40 -0
  6. package/dist/Map/compact.js +38 -0
  7. package/dist/Map/filter.d.ts +45 -0
  8. package/dist/Map/filter.js +44 -0
  9. package/dist/Map/forEach.d.ts +39 -0
  10. package/dist/Map/forEach.js +37 -0
  11. package/dist/Map/get.d.ts +50 -0
  12. package/dist/Map/get.js +52 -0
  13. package/dist/Map/getOr.d.ts +72 -0
  14. package/dist/Map/getOr.js +71 -0
  15. package/dist/Map/getOrElse.d.ts +72 -0
  16. package/dist/Map/getOrElse.js +71 -0
  17. package/dist/Map/getOrThrow.d.ts +69 -0
  18. package/dist/Map/getOrThrow.js +70 -0
  19. package/dist/Map/has.d.ts +50 -0
  20. package/dist/Map/has.js +52 -0
  21. package/dist/Map/hasAll.d.ts +54 -0
  22. package/dist/Map/hasAll.js +57 -0
  23. package/dist/Map/hasAny.d.ts +50 -0
  24. package/dist/Map/hasAny.js +53 -0
  25. package/dist/Map/hasNone.d.ts +50 -0
  26. package/dist/Map/hasNone.js +53 -0
  27. package/dist/Map/index.d.ts +35 -0
  28. package/dist/Map/index.js +61 -0
  29. package/dist/Map/internals/types.d.ts +14 -0
  30. package/dist/Map/isEmpty.d.ts +24 -0
  31. package/dist/Map/isEmpty.js +26 -0
  32. package/dist/Map/isMap.d.ts +26 -0
  33. package/dist/Map/isMap.js +28 -0
  34. package/dist/Map/isShallowEqual.d.ts +66 -0
  35. package/dist/Map/isShallowEqual.js +80 -0
  36. package/dist/Map/map.d.ts +55 -0
  37. package/dist/Map/map.js +59 -0
  38. package/dist/Map/mapEach.d.ts +39 -0
  39. package/dist/Map/mapEach.js +44 -0
  40. package/dist/Map/mapOr.d.ts +59 -0
  41. package/dist/Map/mapOr.js +63 -0
  42. package/dist/Map/mapOrElse.d.ts +67 -0
  43. package/dist/Map/mapOrElse.js +71 -0
  44. package/dist/Map/mapOrThrow.d.ts +57 -0
  45. package/dist/Map/mapOrThrow.js +61 -0
  46. package/dist/Map/reject.d.ts +45 -0
  47. package/dist/Map/reject.js +44 -0
  48. package/dist/Map/remove.d.ts +52 -0
  49. package/dist/Map/remove.js +56 -0
  50. package/dist/Map/removeAll.d.ts +56 -0
  51. package/dist/Map/removeAll.js +63 -0
  52. package/dist/Map/removeOr.d.ts +52 -0
  53. package/dist/Map/removeOr.js +58 -0
  54. package/dist/Map/removeOrElse.d.ts +55 -0
  55. package/dist/Map/removeOrElse.js +58 -0
  56. package/dist/Map/removeOrThrow.d.ts +50 -0
  57. package/dist/Map/removeOrThrow.js +56 -0
  58. package/dist/Map/set.d.ts +54 -0
  59. package/dist/Map/set.js +58 -0
  60. package/dist/index.d.ts +2 -0
  61. package/dist/index.js +3 -0
  62. package/package.json +39 -0
@@ -0,0 +1,61 @@
1
+ import { clone } from "./clone.js";
2
+ import { filter } from "./filter.js";
3
+ import { compact } from "./compact.js";
4
+ import { forEach } from "./forEach.js";
5
+ import { get } from "./get.js";
6
+ import { getOr } from "./getOr.js";
7
+ import { getOrElse } from "./getOrElse.js";
8
+ import { getOrThrow } from "./getOrThrow.js";
9
+ import { has } from "./has.js";
10
+ import { hasAll } from "./hasAll.js";
11
+ import { hasAny } from "./hasAny.js";
12
+ import { hasNone } from "./hasNone.js";
13
+ import { isEmpty } from "./isEmpty.js";
14
+ import { isMap } from "./isMap.js";
15
+ import { isShallowEqual } from "./isShallowEqual.js";
16
+ import { map } from "./map.js";
17
+ import { mapEach } from "./mapEach.js";
18
+ import { mapOr } from "./mapOr.js";
19
+ import { mapOrElse } from "./mapOrElse.js";
20
+ import { mapOrThrow } from "./mapOrThrow.js";
21
+ import { reject } from "./reject.js";
22
+ import { remove } from "./remove.js";
23
+ import { removeAll } from "./removeAll.js";
24
+ import { removeOr } from "./removeOr.js";
25
+ import { removeOrElse } from "./removeOrElse.js";
26
+ import { removeOrThrow } from "./removeOrThrow.js";
27
+ import { set } from "./set.js";
28
+
29
+ //#region src/Map/index.js
30
+ const Map = {
31
+ clone,
32
+ compact,
33
+ filter,
34
+ forEach,
35
+ get,
36
+ getOr,
37
+ getOrElse,
38
+ getOrThrow,
39
+ has,
40
+ hasAll,
41
+ hasAny,
42
+ hasNone,
43
+ isEmpty,
44
+ isMap,
45
+ isShallowEqual,
46
+ map,
47
+ mapEach,
48
+ mapOr,
49
+ mapOrElse,
50
+ mapOrThrow,
51
+ reject,
52
+ remove,
53
+ removeAll,
54
+ removeOr,
55
+ removeOrElse,
56
+ removeOrThrow,
57
+ set
58
+ };
59
+
60
+ //#endregion
61
+ export { Map };
@@ -0,0 +1,14 @@
1
+ //#region src/Map/internals/types.d.ts
2
+ type NonNil<T> = Exclude<T, null | undefined>;
3
+ interface MapGuard<K, V, U extends V> {
4
+ (value: NoInfer<V>, key: NoInfer<K>, target: ReadonlyMap<K, V>): value is U;
5
+ }
6
+ interface MapMap<K, V, U = V> {
7
+ (value: NoInfer<V>, key: NoInfer<K>, target: ReadonlyMap<K, V>): U;
8
+ }
9
+ type MapPredicate<K, V> = MapMap<K, V, boolean>;
10
+ interface OrElse<K, V, U> {
11
+ (target: ReadonlyMap<K, V>): U;
12
+ }
13
+ //#endregion
14
+ export { MapGuard, MapMap, MapPredicate, NonNil, OrElse };
@@ -0,0 +1,24 @@
1
+ //#region src/Map/isEmpty.d.ts
2
+ /**
3
+ * `Map.isEmpty(map)`
4
+ *
5
+ * Checks if `map` is empty (has no entries).
6
+ *
7
+ * ## Example
8
+ *
9
+ * ```ts
10
+ * Map.isEmpty(Map.create()); // true
11
+ * Map.isEmpty(Map.create([["a", 1]])); // false
12
+ * ```
13
+ *
14
+ * ```ts
15
+ * pipe(Map.create(), Map.isEmpty()); // true
16
+ * pipe(Map.create([["a", 1]]), Map.isEmpty()); // false
17
+ * ```
18
+ */
19
+ declare const isEmpty: {
20
+ (): <T, U>(target: ReadonlyMap<T, U>) => boolean;
21
+ <T, U>(target: ReadonlyMap<T, U>): boolean;
22
+ };
23
+ //#endregion
24
+ export { isEmpty };
@@ -0,0 +1,26 @@
1
+ import { dfdlT } from "@monstermann/dfdl";
2
+
3
+ //#region src/Map/isEmpty.ts
4
+ /**
5
+ * `Map.isEmpty(map)`
6
+ *
7
+ * Checks if `map` is empty (has no entries).
8
+ *
9
+ * ## Example
10
+ *
11
+ * ```ts
12
+ * Map.isEmpty(Map.create()); // true
13
+ * Map.isEmpty(Map.create([["a", 1]])); // false
14
+ * ```
15
+ *
16
+ * ```ts
17
+ * pipe(Map.create(), Map.isEmpty()); // true
18
+ * pipe(Map.create([["a", 1]]), Map.isEmpty()); // false
19
+ * ```
20
+ */
21
+ const isEmpty = dfdlT((target) => {
22
+ return target.size === 0;
23
+ }, 1);
24
+
25
+ //#endregion
26
+ export { isEmpty };
@@ -0,0 +1,26 @@
1
+ //#region src/Map/isMap.d.ts
2
+ /**
3
+ * `Map.isMap(target)`
4
+ *
5
+ * Checks if `target` is a Map instance.
6
+ *
7
+ * ## Example
8
+ *
9
+ * ```ts
10
+ * Map.isMap(Map.create()); // true
11
+ * Map.isMap({}); // false
12
+ * Map.isMap([]); // false
13
+ * ```
14
+ *
15
+ * ```ts
16
+ * pipe(Map.create(), Map.isMap()); // true
17
+ * pipe({}, Map.isMap()); // false
18
+ * pipe([], Map.isMap()); // false
19
+ * ```
20
+ */
21
+ declare const isMap: {
22
+ (): (target: unknown) => target is Map<unknown, unknown>;
23
+ (target: unknown): target is Map<unknown, unknown>;
24
+ };
25
+ //#endregion
26
+ export { isMap };
@@ -0,0 +1,28 @@
1
+ import { dfdlT } from "@monstermann/dfdl";
2
+
3
+ //#region src/Map/isMap.ts
4
+ /**
5
+ * `Map.isMap(target)`
6
+ *
7
+ * Checks if `target` is a Map instance.
8
+ *
9
+ * ## Example
10
+ *
11
+ * ```ts
12
+ * Map.isMap(Map.create()); // true
13
+ * Map.isMap({}); // false
14
+ * Map.isMap([]); // false
15
+ * ```
16
+ *
17
+ * ```ts
18
+ * pipe(Map.create(), Map.isMap()); // true
19
+ * pipe({}, Map.isMap()); // false
20
+ * pipe([], Map.isMap()); // false
21
+ * ```
22
+ */
23
+ const isMap = dfdlT((target) => {
24
+ return target instanceof Map;
25
+ }, 1);
26
+
27
+ //#endregion
28
+ export { isMap };
@@ -0,0 +1,66 @@
1
+ //#region src/Map/isShallowEqual.d.ts
2
+ /**
3
+ * `Map.isShallowEqual(map, source)`
4
+ *
5
+ * Checks if `map` is shallow equal to `source` by comparing their keys and values using strict equality.
6
+ *
7
+ * ## Example
8
+ *
9
+ * ```ts
10
+ * Map.isShallowEqual(
11
+ * Map.create([
12
+ * ["a", 1],
13
+ * ["b", 2],
14
+ * ]),
15
+ * Map.create([
16
+ * ["a", 1],
17
+ * ["b", 2],
18
+ * ]),
19
+ * ); // true
20
+ *
21
+ * Map.isShallowEqual(
22
+ * Map.create([
23
+ * ["a", 1],
24
+ * ["b", 2],
25
+ * ]),
26
+ * Map.create([
27
+ * ["a", 1],
28
+ * ["b", 3],
29
+ * ]),
30
+ * ); // false
31
+ * ```
32
+ *
33
+ * ```ts
34
+ * pipe(
35
+ * Map.create([
36
+ * ["a", 1],
37
+ * ["b", 2],
38
+ * ]),
39
+ * Map.isShallowEqual(
40
+ * Map.create([
41
+ * ["a", 1],
42
+ * ["b", 2],
43
+ * ]),
44
+ * ),
45
+ * ); // true
46
+ *
47
+ * pipe(
48
+ * Map.create([
49
+ * ["a", 1],
50
+ * ["b", 2],
51
+ * ]),
52
+ * Map.isShallowEqual(
53
+ * Map.create([
54
+ * ["a", 1],
55
+ * ["b", 3],
56
+ * ]),
57
+ * ),
58
+ * ); // false
59
+ * ```
60
+ */
61
+ declare const isShallowEqual: {
62
+ <K, V>(source: ReadonlyMap<NoInfer<K>, NoInfer<V>>): (target: ReadonlyMap<K, V>) => boolean;
63
+ <K, V>(target: ReadonlyMap<K, V>, source: ReadonlyMap<NoInfer<K>, NoInfer<V>>): boolean;
64
+ };
65
+ //#endregion
66
+ export { isShallowEqual };
@@ -0,0 +1,80 @@
1
+ import { dfdlT } from "@monstermann/dfdl";
2
+
3
+ //#region src/Map/isShallowEqual.ts
4
+ /**
5
+ * `Map.isShallowEqual(map, source)`
6
+ *
7
+ * Checks if `map` is shallow equal to `source` by comparing their keys and values using strict equality.
8
+ *
9
+ * ## Example
10
+ *
11
+ * ```ts
12
+ * Map.isShallowEqual(
13
+ * Map.create([
14
+ * ["a", 1],
15
+ * ["b", 2],
16
+ * ]),
17
+ * Map.create([
18
+ * ["a", 1],
19
+ * ["b", 2],
20
+ * ]),
21
+ * ); // true
22
+ *
23
+ * Map.isShallowEqual(
24
+ * Map.create([
25
+ * ["a", 1],
26
+ * ["b", 2],
27
+ * ]),
28
+ * Map.create([
29
+ * ["a", 1],
30
+ * ["b", 3],
31
+ * ]),
32
+ * ); // false
33
+ * ```
34
+ *
35
+ * ```ts
36
+ * pipe(
37
+ * Map.create([
38
+ * ["a", 1],
39
+ * ["b", 2],
40
+ * ]),
41
+ * Map.isShallowEqual(
42
+ * Map.create([
43
+ * ["a", 1],
44
+ * ["b", 2],
45
+ * ]),
46
+ * ),
47
+ * ); // true
48
+ *
49
+ * pipe(
50
+ * Map.create([
51
+ * ["a", 1],
52
+ * ["b", 2],
53
+ * ]),
54
+ * Map.isShallowEqual(
55
+ * Map.create([
56
+ * ["a", 1],
57
+ * ["b", 3],
58
+ * ]),
59
+ * ),
60
+ * ); // false
61
+ * ```
62
+ */
63
+ const isShallowEqual = dfdlT((target, source) => {
64
+ if (target.size !== source.size) return false;
65
+ const seen = /* @__PURE__ */ new Set();
66
+ for (const k of target.keys()) {
67
+ seen.add(k);
68
+ if (!source.has(k)) return false;
69
+ if (target.get(k) !== source.get(k)) return false;
70
+ }
71
+ for (const k of source.keys()) {
72
+ if (seen.has(k)) continue;
73
+ if (!target.has(k)) return false;
74
+ if (target.get(k) !== source.get(k)) return false;
75
+ }
76
+ return true;
77
+ }, 2);
78
+
79
+ //#endregion
80
+ export { isShallowEqual };
@@ -0,0 +1,55 @@
1
+ import { MapMap } from "./internals/types.js";
2
+
3
+ //#region src/Map/map.d.ts
4
+
5
+ /**
6
+ * `Map.map(map, key, transform)`
7
+ *
8
+ * Transforms the value at `key` in `map` using `transform`, returning a new map if the value changes.
9
+ *
10
+ * ## Example
11
+ *
12
+ * ```ts
13
+ * Map.map(
14
+ * Map.create([
15
+ * ["a", 1],
16
+ * ["b", 2],
17
+ * ]),
18
+ * "a",
19
+ * (value) => value * 2,
20
+ * ); // Map(2) { "a" => 2, "b" => 2 }
21
+ *
22
+ * Map.map(
23
+ * Map.create([
24
+ * ["a", 1],
25
+ * ["b", 2],
26
+ * ]),
27
+ * "c",
28
+ * (value) => value * 2,
29
+ * ); // Map(2) { "a" => 1, "b" => 2 }
30
+ * ```
31
+ *
32
+ * ```ts
33
+ * pipe(
34
+ * Map.create([
35
+ * ["a", 1],
36
+ * ["b", 2],
37
+ * ]),
38
+ * Map.map("a", (value) => value * 2),
39
+ * ); // Map(2) { "a" => 2, "b" => 2 }
40
+ *
41
+ * pipe(
42
+ * Map.create([
43
+ * ["a", 1],
44
+ * ["b", 2],
45
+ * ]),
46
+ * Map.map("c", (value) => value * 2),
47
+ */
48
+ declare const map: {
49
+ <K, V>(key: NoInfer<K>, transform: MapMap<K, V>): (target: Map<K, V>) => Map<K, V>;
50
+ <K, V>(key: NoInfer<K>, transform: MapMap<K, V>): (target: ReadonlyMap<K, V>) => ReadonlyMap<K, V>;
51
+ <K, V>(target: Map<K, V>, key: NoInfer<K>, transform: MapMap<K, V>): Map<K, V>;
52
+ <K, V>(target: ReadonlyMap<K, V>, key: NoInfer<K>, transform: MapMap<K, V>): ReadonlyMap<K, V>;
53
+ };
54
+ //#endregion
55
+ export { map };
@@ -0,0 +1,59 @@
1
+ import { dfdlT } from "@monstermann/dfdl";
2
+ import { cloneMap } from "@monstermann/remmi";
3
+
4
+ //#region src/Map/map.ts
5
+ /**
6
+ * `Map.map(map, key, transform)`
7
+ *
8
+ * Transforms the value at `key` in `map` using `transform`, returning a new map if the value changes.
9
+ *
10
+ * ## Example
11
+ *
12
+ * ```ts
13
+ * Map.map(
14
+ * Map.create([
15
+ * ["a", 1],
16
+ * ["b", 2],
17
+ * ]),
18
+ * "a",
19
+ * (value) => value * 2,
20
+ * ); // Map(2) { "a" => 2, "b" => 2 }
21
+ *
22
+ * Map.map(
23
+ * Map.create([
24
+ * ["a", 1],
25
+ * ["b", 2],
26
+ * ]),
27
+ * "c",
28
+ * (value) => value * 2,
29
+ * ); // Map(2) { "a" => 1, "b" => 2 }
30
+ * ```
31
+ *
32
+ * ```ts
33
+ * pipe(
34
+ * Map.create([
35
+ * ["a", 1],
36
+ * ["b", 2],
37
+ * ]),
38
+ * Map.map("a", (value) => value * 2),
39
+ * ); // Map(2) { "a" => 2, "b" => 2 }
40
+ *
41
+ * pipe(
42
+ * Map.create([
43
+ * ["a", 1],
44
+ * ["b", 2],
45
+ * ]),
46
+ * Map.map("c", (value) => value * 2),
47
+ */
48
+ const map = dfdlT((target, key, transform) => {
49
+ if (!target.has(key)) return target;
50
+ const prev = target.get(key);
51
+ const next = transform(prev, key, target);
52
+ if (prev === next) return target;
53
+ const result = cloneMap(target);
54
+ result.set(key, next);
55
+ return result;
56
+ }, 3);
57
+
58
+ //#endregion
59
+ export { map };
@@ -0,0 +1,39 @@
1
+ import { MapMap } from "./internals/types.js";
2
+
3
+ //#region src/Map/mapEach.d.ts
4
+
5
+ /**
6
+ * `Map.mapEach(map, fn)`
7
+ *
8
+ * Maps each value in `map` using `fn`, returning a new map with the transformed values.
9
+ *
10
+ * ## Example
11
+ *
12
+ * ```ts
13
+ * Map.mapEach(
14
+ * Map.create([
15
+ * ["a", 1],
16
+ * ["b", 2],
17
+ * ]),
18
+ * (value, key) => value * 2,
19
+ * ); // Map(2) { "a" => 2, "b" => 4 }
20
+ * ```
21
+ *
22
+ * ```ts
23
+ * pipe(
24
+ * Map.create([
25
+ * ["a", 1],
26
+ * ["b", 2],
27
+ * ]),
28
+ * Map.mapEach((value, key) => value * 2),
29
+ * ); // Map(2) { "a" => 2, "b" => 4 }
30
+ * ```
31
+ */
32
+ declare const mapEach: {
33
+ <K, V, U>(fn: MapMap<K, V, U>): (target: Map<K, V>) => Map<K, U>;
34
+ <K, V, U>(fn: MapMap<K, V, U>): (target: ReadonlyMap<K, V>) => ReadonlyMap<K, U>;
35
+ <K, V, U>(target: Map<K, V>, fn: MapMap<K, V, U>): Map<K, U>;
36
+ <K, V, U>(target: ReadonlyMap<K, V>, fn: MapMap<K, V, U>): ReadonlyMap<K, U>;
37
+ };
38
+ //#endregion
39
+ export { mapEach };
@@ -0,0 +1,44 @@
1
+ import { dfdlT } from "@monstermann/dfdl";
2
+ import { cloneMap } from "@monstermann/remmi";
3
+
4
+ //#region src/Map/mapEach.ts
5
+ /**
6
+ * `Map.mapEach(map, fn)`
7
+ *
8
+ * Maps each value in `map` using `fn`, returning a new map with the transformed values.
9
+ *
10
+ * ## Example
11
+ *
12
+ * ```ts
13
+ * Map.mapEach(
14
+ * Map.create([
15
+ * ["a", 1],
16
+ * ["b", 2],
17
+ * ]),
18
+ * (value, key) => value * 2,
19
+ * ); // Map(2) { "a" => 2, "b" => 4 }
20
+ * ```
21
+ *
22
+ * ```ts
23
+ * pipe(
24
+ * Map.create([
25
+ * ["a", 1],
26
+ * ["b", 2],
27
+ * ]),
28
+ * Map.mapEach((value, key) => value * 2),
29
+ * ); // Map(2) { "a" => 2, "b" => 4 }
30
+ * ```
31
+ */
32
+ const mapEach = dfdlT((target, fn) => {
33
+ let result;
34
+ for (const [k, prev] of target) {
35
+ const next = fn(prev, k, target);
36
+ if (prev === next) continue;
37
+ result ??= cloneMap(target);
38
+ result.set(k, next);
39
+ }
40
+ return result ?? target;
41
+ }, 2);
42
+
43
+ //#endregion
44
+ export { mapEach };
@@ -0,0 +1,59 @@
1
+ import { MapMap } from "./internals/types.js";
2
+
3
+ //#region src/Map/mapOr.d.ts
4
+
5
+ /**
6
+ * `Map.mapOr(map, key, transform, or)`
7
+ *
8
+ * Transforms the value at `key` in `map` using `transform`, returning `or` if the key doesn't exist.
9
+ *
10
+ * ## Example
11
+ *
12
+ * ```ts
13
+ * Map.mapOr(
14
+ * Map.create([
15
+ * ["a", 1],
16
+ * ["b", 2],
17
+ * ]),
18
+ * "a",
19
+ * (value) => value * 2,
20
+ * null,
21
+ * ); // Map(2) { "a" => 2, "b" => 2 }
22
+ *
23
+ * Map.mapOr(
24
+ * Map.create([
25
+ * ["a", 1],
26
+ * ["b", 2],
27
+ * ]),
28
+ * "c",
29
+ * (value) => value * 2,
30
+ * null,
31
+ * ); // null
32
+ * ```
33
+ *
34
+ * ```ts
35
+ * pipe(
36
+ * Map.create([
37
+ * ["a", 1],
38
+ * ["b", 2],
39
+ * ]),
40
+ * Map.mapOr("a", (value) => value * 2, null),
41
+ * ); // Map(2) { "a" => 2, "b" => 2 }
42
+ *
43
+ * pipe(
44
+ * Map.create([
45
+ * ["a", 1],
46
+ * ["b", 2],
47
+ * ]),
48
+ * Map.mapOr("c", (value) => value * 2, null),
49
+ * ); // null
50
+ * ```
51
+ */
52
+ declare const mapOr: {
53
+ <K, V, U>(key: NoInfer<K>, transform: MapMap<K, V>, or: U): (target: Map<K, V>) => Map<K, V> | U;
54
+ <K, V, U>(key: NoInfer<K>, transform: MapMap<K, V>, or: U): (target: ReadonlyMap<K, V>) => ReadonlyMap<K, V> | U;
55
+ <K, V, U>(target: Map<K, V>, key: NoInfer<K>, transform: MapMap<K, V>, or: U): Map<K, V> | U;
56
+ <K, V, U>(target: ReadonlyMap<K, V>, key: NoInfer<K>, transform: MapMap<K, V>, or: U): ReadonlyMap<K, V> | U;
57
+ };
58
+ //#endregion
59
+ export { mapOr };
@@ -0,0 +1,63 @@
1
+ import { dfdlT } from "@monstermann/dfdl";
2
+ import { cloneMap } from "@monstermann/remmi";
3
+
4
+ //#region src/Map/mapOr.ts
5
+ /**
6
+ * `Map.mapOr(map, key, transform, or)`
7
+ *
8
+ * Transforms the value at `key` in `map` using `transform`, returning `or` if the key doesn't exist.
9
+ *
10
+ * ## Example
11
+ *
12
+ * ```ts
13
+ * Map.mapOr(
14
+ * Map.create([
15
+ * ["a", 1],
16
+ * ["b", 2],
17
+ * ]),
18
+ * "a",
19
+ * (value) => value * 2,
20
+ * null,
21
+ * ); // Map(2) { "a" => 2, "b" => 2 }
22
+ *
23
+ * Map.mapOr(
24
+ * Map.create([
25
+ * ["a", 1],
26
+ * ["b", 2],
27
+ * ]),
28
+ * "c",
29
+ * (value) => value * 2,
30
+ * null,
31
+ * ); // null
32
+ * ```
33
+ *
34
+ * ```ts
35
+ * pipe(
36
+ * Map.create([
37
+ * ["a", 1],
38
+ * ["b", 2],
39
+ * ]),
40
+ * Map.mapOr("a", (value) => value * 2, null),
41
+ * ); // Map(2) { "a" => 2, "b" => 2 }
42
+ *
43
+ * pipe(
44
+ * Map.create([
45
+ * ["a", 1],
46
+ * ["b", 2],
47
+ * ]),
48
+ * Map.mapOr("c", (value) => value * 2, null),
49
+ * ); // null
50
+ * ```
51
+ */
52
+ const mapOr = dfdlT((target, key, transform, or) => {
53
+ if (!target.has(key)) return or;
54
+ const prev = target.get(key);
55
+ const next = transform(prev, key, target);
56
+ if (prev === next) return target;
57
+ const result = cloneMap(target);
58
+ result.set(key, next);
59
+ return result;
60
+ }, 4);
61
+
62
+ //#endregion
63
+ export { mapOr };