metano-runtime 3.0.2 → 3.2.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 (185) hide show
  1. package/dist/system/char-members.d.ts +23 -0
  2. package/dist/system/char-members.js +46 -0
  3. package/dist/system/collections/hash-core.d.ts +5 -0
  4. package/dist/system/collections/hash-core.js +24 -0
  5. package/dist/system/collections/helpers.d.ts +16 -3
  6. package/dist/system/collections/helpers.js +28 -2
  7. package/dist/system/collections/immutable-list-builder-core.d.ts +7 -2
  8. package/dist/system/collections/immutable-list-builder-core.js +11 -0
  9. package/dist/system/collections/immutable-list-core.d.ts +8 -0
  10. package/dist/system/collections/immutable-list-core.js +19 -1
  11. package/dist/system/collections/immutable-storage.d.ts +2 -0
  12. package/dist/system/collections/immutable-storage.js +6 -1
  13. package/dist/system/collections/interfaces.d.ts +13 -0
  14. package/dist/system/collections/list.d.ts +3 -0
  15. package/dist/system/collections/list.js +7 -0
  16. package/dist/system/collections/read-only-list.d.ts +11 -0
  17. package/dist/system/collections/read-only-list.js +26 -1
  18. package/dist/system/collections/sorted-list.d.ts +5 -0
  19. package/dist/system/collections/sorted-list.js +12 -1
  20. package/dist/system/comparable-compare-to.d.ts +12 -0
  21. package/dist/system/comparable-compare-to.js +20 -0
  22. package/dist/system/decimal-parsing.d.ts +12 -0
  23. package/dist/system/decimal-parsing.js +86 -0
  24. package/dist/system/exceptions/index.d.ts +1 -0
  25. package/dist/system/exceptions/index.js +1 -0
  26. package/dist/system/exceptions/throw-error.d.ts +7 -0
  27. package/dist/system/exceptions/throw-error.js +9 -0
  28. package/dist/system/hash-code.d.ts +2 -1
  29. package/dist/system/hash-code.js +5 -2
  30. package/dist/system/index.d.ts +3 -0
  31. package/dist/system/index.js +3 -0
  32. package/dist/system/int64-math.js +2 -1
  33. package/dist/system/int64.d.ts +6 -0
  34. package/dist/system/int64.js +18 -9
  35. package/dist/system/integer-division.d.ts +12 -4
  36. package/dist/system/integer-division.js +19 -2
  37. package/dist/system/json/index.d.ts +1 -1
  38. package/dist/system/json/json-serializer.d.ts +12 -1
  39. package/dist/system/json/json-serializer.js +29 -0
  40. package/dist/system/json/json-text-writer.d.ts +24 -0
  41. package/dist/system/json/json-text-writer.js +207 -0
  42. package/dist/system/json/number-from-string.js +20 -6
  43. package/dist/system/json/numeric-converters.js +41 -0
  44. package/dist/system/json/ordinal-ignore-case.d.ts +2 -0
  45. package/dist/system/json/ordinal-ignore-case.js +1 -1
  46. package/dist/system/json/serializer-context.d.ts +13 -7
  47. package/dist/system/json/serializer-context.js +23 -11
  48. package/dist/system/json/types.d.ts +20 -0
  49. package/dist/system/linq/grouping.d.ts +12 -0
  50. package/dist/system/linq/grouping.js +42 -0
  51. package/dist/system/linq/index.d.ts +4 -3
  52. package/dist/system/linq/index.js +3 -2
  53. package/dist/system/linq/linq.js +20 -1
  54. package/dist/system/linq/lookup.d.ts +17 -0
  55. package/dist/system/linq/lookup.js +20 -0
  56. package/dist/system/linq/operators.d.ts +12 -4
  57. package/dist/system/linq/operators.js +61 -15
  58. package/dist/system/linq/terminals.d.ts +29 -2
  59. package/dist/system/linq/terminals.js +71 -3
  60. package/dist/system/linq/types.d.ts +27 -4
  61. package/dist/system/math.d.ts +27 -0
  62. package/dist/system/math.js +45 -0
  63. package/dist/system/min-max.d.ts +6 -0
  64. package/dist/system/min-max.js +10 -0
  65. package/dist/system/number-parsing.d.ts +16 -4
  66. package/dist/system/number-parsing.js +21 -4
  67. package/dist/system/string-comparison.d.ts +56 -0
  68. package/dist/system/string-comparison.js +225 -0
  69. package/dist/system/string-hash-code.d.ts +14 -0
  70. package/dist/system/string-hash-code.js +37 -0
  71. package/dist/system/string-members.d.ts +5 -4
  72. package/dist/system/string-members.js +12 -11
  73. package/dist-unchecked/system/char-members.d.ts +23 -0
  74. package/dist-unchecked/system/char-members.js +46 -0
  75. package/dist-unchecked/system/collections/hash-core.d.ts +5 -0
  76. package/dist-unchecked/system/collections/hash-core.js +24 -0
  77. package/dist-unchecked/system/collections/helpers.d.ts +16 -3
  78. package/dist-unchecked/system/collections/helpers.js +28 -2
  79. package/dist-unchecked/system/collections/immutable-list-builder-core.d.ts +7 -2
  80. package/dist-unchecked/system/collections/immutable-list-builder-core.js +11 -0
  81. package/dist-unchecked/system/collections/immutable-list-core.d.ts +8 -0
  82. package/dist-unchecked/system/collections/immutable-list-core.js +19 -1
  83. package/dist-unchecked/system/collections/immutable-storage.d.ts +2 -0
  84. package/dist-unchecked/system/collections/immutable-storage.js +6 -1
  85. package/dist-unchecked/system/collections/interfaces.d.ts +13 -0
  86. package/dist-unchecked/system/collections/list.d.ts +3 -0
  87. package/dist-unchecked/system/collections/list.js +7 -0
  88. package/dist-unchecked/system/collections/read-only-list.d.ts +11 -0
  89. package/dist-unchecked/system/collections/read-only-list.js +26 -1
  90. package/dist-unchecked/system/collections/sorted-list.d.ts +5 -0
  91. package/dist-unchecked/system/collections/sorted-list.js +12 -1
  92. package/dist-unchecked/system/comparable-compare-to.d.ts +12 -0
  93. package/dist-unchecked/system/comparable-compare-to.js +20 -0
  94. package/dist-unchecked/system/decimal-parsing.d.ts +12 -0
  95. package/dist-unchecked/system/decimal-parsing.js +86 -0
  96. package/dist-unchecked/system/exceptions/index.d.ts +1 -0
  97. package/dist-unchecked/system/exceptions/index.js +1 -0
  98. package/dist-unchecked/system/exceptions/throw-error.d.ts +7 -0
  99. package/dist-unchecked/system/exceptions/throw-error.js +9 -0
  100. package/dist-unchecked/system/hash-code.d.ts +2 -1
  101. package/dist-unchecked/system/hash-code.js +5 -2
  102. package/dist-unchecked/system/index.d.ts +3 -0
  103. package/dist-unchecked/system/index.js +3 -0
  104. package/dist-unchecked/system/int64-math.js +2 -1
  105. package/dist-unchecked/system/int64.d.ts +6 -0
  106. package/dist-unchecked/system/int64.js +18 -9
  107. package/dist-unchecked/system/integer-division.d.ts +12 -4
  108. package/dist-unchecked/system/integer-division.js +19 -2
  109. package/dist-unchecked/system/json/index.d.ts +1 -1
  110. package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
  111. package/dist-unchecked/system/json/json-serializer.js +29 -0
  112. package/dist-unchecked/system/json/json-text-writer.d.ts +24 -0
  113. package/dist-unchecked/system/json/json-text-writer.js +207 -0
  114. package/dist-unchecked/system/json/number-from-string.js +20 -6
  115. package/dist-unchecked/system/json/numeric-converters.js +41 -0
  116. package/dist-unchecked/system/json/ordinal-ignore-case.d.ts +2 -0
  117. package/dist-unchecked/system/json/ordinal-ignore-case.js +1 -1
  118. package/dist-unchecked/system/json/serializer-context.d.ts +13 -7
  119. package/dist-unchecked/system/json/serializer-context.js +23 -11
  120. package/dist-unchecked/system/json/types.d.ts +20 -0
  121. package/dist-unchecked/system/linq/grouping.d.ts +12 -0
  122. package/dist-unchecked/system/linq/grouping.js +42 -0
  123. package/dist-unchecked/system/linq/index.d.ts +4 -3
  124. package/dist-unchecked/system/linq/index.js +3 -2
  125. package/dist-unchecked/system/linq/linq.js +20 -1
  126. package/dist-unchecked/system/linq/lookup.d.ts +17 -0
  127. package/dist-unchecked/system/linq/lookup.js +20 -0
  128. package/dist-unchecked/system/linq/operators.d.ts +12 -4
  129. package/dist-unchecked/system/linq/operators.js +61 -15
  130. package/dist-unchecked/system/linq/terminals.d.ts +29 -2
  131. package/dist-unchecked/system/linq/terminals.js +71 -3
  132. package/dist-unchecked/system/linq/types.d.ts +27 -4
  133. package/dist-unchecked/system/math.d.ts +27 -0
  134. package/dist-unchecked/system/math.js +45 -0
  135. package/dist-unchecked/system/min-max.d.ts +6 -0
  136. package/dist-unchecked/system/min-max.js +10 -0
  137. package/dist-unchecked/system/number-parsing.d.ts +16 -4
  138. package/dist-unchecked/system/number-parsing.js +21 -4
  139. package/dist-unchecked/system/string-comparison.d.ts +56 -0
  140. package/dist-unchecked/system/string-comparison.js +225 -0
  141. package/dist-unchecked/system/string-hash-code.d.ts +14 -0
  142. package/dist-unchecked/system/string-hash-code.js +37 -0
  143. package/dist-unchecked/system/string-members.d.ts +5 -4
  144. package/dist-unchecked/system/string-members.js +12 -11
  145. package/package.json +1 -1
  146. package/src/system/char-members.ts +57 -0
  147. package/src/system/collections/hash-core.ts +31 -0
  148. package/src/system/collections/helpers.ts +39 -4
  149. package/src/system/collections/immutable-list-builder-core.ts +16 -2
  150. package/src/system/collections/immutable-list-core.ts +23 -1
  151. package/src/system/collections/immutable-storage.ts +8 -1
  152. package/src/system/collections/interfaces.ts +16 -0
  153. package/src/system/collections/list.ts +9 -0
  154. package/src/system/collections/read-only-list.ts +35 -1
  155. package/src/system/collections/sorted-list.ts +15 -1
  156. package/src/system/comparable-compare-to.ts +23 -0
  157. package/src/system/decimal-parsing.ts +109 -0
  158. package/src/system/exceptions/index.ts +1 -0
  159. package/src/system/exceptions/throw-error.ts +9 -0
  160. package/src/system/hash-code.ts +7 -2
  161. package/src/system/index.ts +3 -0
  162. package/src/system/int64-math.ts +2 -1
  163. package/src/system/int64.ts +27 -10
  164. package/src/system/integer-division.ts +43 -6
  165. package/src/system/json/index.ts +1 -0
  166. package/src/system/json/json-serializer.ts +42 -0
  167. package/src/system/json/json-text-writer.ts +282 -0
  168. package/src/system/json/number-from-string.ts +26 -6
  169. package/src/system/json/numeric-converters.ts +55 -0
  170. package/src/system/json/ordinal-ignore-case.ts +1 -1
  171. package/src/system/json/serializer-context.ts +28 -11
  172. package/src/system/json/types.ts +21 -0
  173. package/src/system/linq/grouping.ts +77 -0
  174. package/src/system/linq/index.ts +8 -0
  175. package/src/system/linq/linq.ts +23 -1
  176. package/src/system/linq/lookup.ts +34 -0
  177. package/src/system/linq/operators.ts +78 -15
  178. package/src/system/linq/terminals.ts +85 -3
  179. package/src/system/linq/types.ts +32 -0
  180. package/src/system/math.ts +63 -0
  181. package/src/system/min-max.ts +11 -0
  182. package/src/system/number-parsing.ts +24 -4
  183. package/src/system/string-comparison.ts +299 -0
  184. package/src/system/string-hash-code.ts +44 -0
  185. package/src/system/string-members.ts +12 -11
@@ -0,0 +1,14 @@
1
+ /**
2
+ * C#'s `value.GetHashCode(comparison)` (#769): the hash `StringComparer.Ordinal` or
3
+ * `StringComparer.OrdinalIgnoreCase` gives, so strings equal under the comparison hash
4
+ * alike. JavaScript has no culture sort key, so a culture-sensitive comparison throws
5
+ * `NotSupportedException` instead of returning a hash that disagrees with
6
+ * `string.Equals(a, b, comparison)`.
7
+ */
8
+ import { StringComparison } from "./string-members.ts";
9
+ /**
10
+ * @throws NullReferenceException when `value` is `null`.
11
+ * @throws ArgumentException when `comparison` is not a `StringComparison` member.
12
+ * @throws NotSupportedException for a culture-sensitive comparison.
13
+ */
14
+ export declare function stringHashCode(value: string | null, comparison: StringComparison): number;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * C#'s `value.GetHashCode(comparison)` (#769): the hash `StringComparer.Ordinal` or
3
+ * `StringComparer.OrdinalIgnoreCase` gives, so strings equal under the comparison hash
4
+ * alike. JavaScript has no culture sort key, so a culture-sensitive comparison throws
5
+ * `NotSupportedException` instead of returning a hash that disagrees with
6
+ * `string.Equals(a, b, comparison)`.
7
+ */
8
+ import { StringComparer } from "./collections/string-comparer.js";
9
+ import { ArgumentException } from "./exceptions/argument-exception.js";
10
+ import { NotSupportedException } from "./exceptions/not-supported-exception.js";
11
+ import { NullReferenceException } from "./exceptions/null-reference-exception.js";
12
+ import { StringComparison } from "./string-members.js";
13
+ const UNSUPPORTED_COMPARISON_MESSAGE = "The string comparison type passed in is currently not supported.";
14
+ const CULTURE_HASH_MESSAGE = "GetHashCode with a culture-sensitive StringComparison is not supported in JavaScript.";
15
+ /**
16
+ * @throws NullReferenceException when `value` is `null`.
17
+ * @throws ArgumentException when `comparison` is not a `StringComparison` member.
18
+ * @throws NotSupportedException for a culture-sensitive comparison.
19
+ */
20
+ export function stringHashCode(value, comparison) {
21
+ if (value == null) {
22
+ throw new NullReferenceException();
23
+ }
24
+ switch (comparison) {
25
+ case StringComparison.Ordinal:
26
+ return StringComparer.ordinal.getHashCode(value);
27
+ case StringComparison.OrdinalIgnoreCase:
28
+ return StringComparer.ordinalIgnoreCase.getHashCode(value);
29
+ case StringComparison.CurrentCulture:
30
+ case StringComparison.CurrentCultureIgnoreCase:
31
+ case StringComparison.InvariantCulture:
32
+ case StringComparison.InvariantCultureIgnoreCase:
33
+ throw new NotSupportedException(CULTURE_HASH_MESSAGE);
34
+ default:
35
+ throw new ArgumentException(UNSUPPORTED_COMPARISON_MESSAGE, "comparisonType");
36
+ }
37
+ }
@@ -8,10 +8,11 @@
8
8
  * JavaScript clamps or swaps. The compiler maps each member here declaratively
9
9
  * (`Metano/Runtime/Strings.cs`); every helper reads its arguments once.
10
10
  *
11
- * Comparisons are ordinal. The culture-sensitive `StringComparison` members behave as
12
- * their ordinal counterparts (JavaScript has no culture-aware comparison), and a
13
- * case-insensitive comparison folds each UTF-16 code unit by its invariant upper-case
14
- * form, as `OrdinalIgnoreCase` does.
11
+ * Searches are ordinal. The culture-sensitive `StringComparison` members behave as
12
+ * their ordinal counterparts (JavaScript has no culture-aware substring search), and a
13
+ * case-insensitive search folds each code point by its ordinal uppercase, as
14
+ * `OrdinalIgnoreCase` does (`ordinalUppercaseOf`, shared with `StringComparer`).
15
+ * `Equals` and `Compare` with a comparison live in `string-comparison.ts`.
15
16
  */
16
17
  /**
17
18
  * Mirror of `System.StringComparison`. The numeric values match the .NET enum, so a
@@ -8,14 +8,16 @@
8
8
  * JavaScript clamps or swaps. The compiler maps each member here declaratively
9
9
  * (`Metano/Runtime/Strings.cs`); every helper reads its arguments once.
10
10
  *
11
- * Comparisons are ordinal. The culture-sensitive `StringComparison` members behave as
12
- * their ordinal counterparts (JavaScript has no culture-aware comparison), and a
13
- * case-insensitive comparison folds each UTF-16 code unit by its invariant upper-case
14
- * form, as `OrdinalIgnoreCase` does.
11
+ * Searches are ordinal. The culture-sensitive `StringComparison` members behave as
12
+ * their ordinal counterparts (JavaScript has no culture-aware substring search), and a
13
+ * case-insensitive search folds each code point by its ordinal uppercase, as
14
+ * `OrdinalIgnoreCase` does (`ordinalUppercaseOf`, shared with `StringComparer`).
15
+ * `Equals` and `Compare` with a comparison live in `string-comparison.ts`.
15
16
  */
16
17
  import { ArgumentException } from "./exceptions/argument-exception.js";
17
18
  import { ArgumentNullException } from "./exceptions/argument-null-exception.js";
18
19
  import { ArgumentOutOfRangeException } from "./exceptions/argument-out-of-range-exception.js";
20
+ import { ordinalUppercaseOf } from "./json/ordinal-ignore-case.js";
19
21
  import { isDotNetWhiteSpace } from "./strings.js";
20
22
  /**
21
23
  * Mirror of `System.StringComparison`. The numeric values match the .NET enum, so a
@@ -49,16 +51,15 @@ function ignoresCase(comparison) {
49
51
  comparison === StringComparison.OrdinalIgnoreCase);
50
52
  }
51
53
  /**
52
- * `value` with each UTF-16 code unit replaced by its invariant upper-case form when that
53
- * form is a single unit, so the result has the length of `value` and an index into one
54
- * is an index into the other.
54
+ * `value` with each code point replaced by its ordinal uppercase when that has the same
55
+ * UTF-16 length, so the result has the length of `value` and an index into one is an
56
+ * index into the other.
55
57
  */
56
58
  function foldCase(value) {
57
59
  let folded = "";
58
- for (let index = 0; index < value.length; index++) {
59
- const unit = value[index];
60
- const upper = unit.toUpperCase();
61
- folded += upper.length === 1 ? upper : unit;
60
+ for (const character of value) {
61
+ const upper = ordinalUppercaseOf(character);
62
+ folded += upper.length === character.length ? upper : character;
62
63
  }
63
64
  return folded;
64
65
  }
@@ -26,3 +26,26 @@ export declare function charIsSeparator(c: string): boolean;
26
26
  export declare function charIsControl(c: string): boolean;
27
27
  /** `char.IsWhiteSpace(c)`. */
28
28
  export declare function charIsWhiteSpace(c: string): boolean;
29
+ /** `char.IsAscii(c)`: a code unit below U+0080. */
30
+ export declare function charIsAscii(c: string): boolean;
31
+ /** `char.IsAsciiDigit(c)`: `0`-`9`. */
32
+ export declare function charIsAsciiDigit(c: string): boolean;
33
+ /** `char.IsAsciiLetterLower(c)`: `a`-`z`. */
34
+ export declare function charIsAsciiLetterLower(c: string): boolean;
35
+ /** `char.IsAsciiLetterUpper(c)`: `A`-`Z`. */
36
+ export declare function charIsAsciiLetterUpper(c: string): boolean;
37
+ /** `char.IsAsciiLetter(c)`: `a`-`z` or `A`-`Z`. */
38
+ export declare function charIsAsciiLetter(c: string): boolean;
39
+ /** `char.IsAsciiLetterOrDigit(c)`. */
40
+ export declare function charIsAsciiLetterOrDigit(c: string): boolean;
41
+ /** `char.IsAsciiHexDigitLower(c)`: `0`-`9` or `a`-`f`. */
42
+ export declare function charIsAsciiHexDigitLower(c: string): boolean;
43
+ /** `char.IsAsciiHexDigitUpper(c)`: `0`-`9` or `A`-`F`. */
44
+ export declare function charIsAsciiHexDigitUpper(c: string): boolean;
45
+ /** `char.IsAsciiHexDigit(c)`: `0`-`9`, `a`-`f` or `A`-`F`. */
46
+ export declare function charIsAsciiHexDigit(c: string): boolean;
47
+ /**
48
+ * `char.IsBetween(c, min, max)`: .NET computes `(uint)(c - min) <= (uint)(max - min)`
49
+ * by code unit, so the range wraps around when `min > max`.
50
+ */
51
+ export declare function charIsBetween(c: string, min: string, max: string): boolean;
@@ -60,3 +60,49 @@ export function charIsControl(c) {
60
60
  export function charIsWhiteSpace(c) {
61
61
  return WHITE_SPACE.test(c);
62
62
  }
63
+ /** `char.IsAscii(c)`: a code unit below U+0080. */
64
+ export function charIsAscii(c) {
65
+ return c.charCodeAt(0) < 0x80;
66
+ }
67
+ /** `char.IsAsciiDigit(c)`: `0`-`9`. */
68
+ export function charIsAsciiDigit(c) {
69
+ return c >= "0" && c <= "9";
70
+ }
71
+ /** `char.IsAsciiLetterLower(c)`: `a`-`z`. */
72
+ export function charIsAsciiLetterLower(c) {
73
+ return c >= "a" && c <= "z";
74
+ }
75
+ /** `char.IsAsciiLetterUpper(c)`: `A`-`Z`. */
76
+ export function charIsAsciiLetterUpper(c) {
77
+ return c >= "A" && c <= "Z";
78
+ }
79
+ /** `char.IsAsciiLetter(c)`: `a`-`z` or `A`-`Z`. */
80
+ export function charIsAsciiLetter(c) {
81
+ return charIsAsciiLetterLower(c) || charIsAsciiLetterUpper(c);
82
+ }
83
+ /** `char.IsAsciiLetterOrDigit(c)`. */
84
+ export function charIsAsciiLetterOrDigit(c) {
85
+ return charIsAsciiLetter(c) || charIsAsciiDigit(c);
86
+ }
87
+ /** `char.IsAsciiHexDigitLower(c)`: `0`-`9` or `a`-`f`. */
88
+ export function charIsAsciiHexDigitLower(c) {
89
+ return charIsAsciiDigit(c) || (c >= "a" && c <= "f");
90
+ }
91
+ /** `char.IsAsciiHexDigitUpper(c)`: `0`-`9` or `A`-`F`. */
92
+ export function charIsAsciiHexDigitUpper(c) {
93
+ return charIsAsciiDigit(c) || (c >= "A" && c <= "F");
94
+ }
95
+ /** `char.IsAsciiHexDigit(c)`: `0`-`9`, `a`-`f` or `A`-`F`. */
96
+ export function charIsAsciiHexDigit(c) {
97
+ return charIsAsciiHexDigitLower(c) || charIsAsciiHexDigitUpper(c);
98
+ }
99
+ /**
100
+ * `char.IsBetween(c, min, max)`: .NET computes `(uint)(c - min) <= (uint)(max - min)`
101
+ * by code unit, so the range wraps around when `min > max`.
102
+ */
103
+ export function charIsBetween(c, min, max) {
104
+ const start = min.charCodeAt(0);
105
+ const offset = (c.charCodeAt(0) - start) >>> 0;
106
+ const width = (max.charCodeAt(0) - start) >>> 0;
107
+ return offset <= width;
108
+ }
@@ -30,6 +30,11 @@ export declare class HashCore<K, V> {
30
30
  findKey(key: K): K | undefined;
31
31
  /** Stores `value` under `key`: `true` when inserted; an overwrite keeps the entry's position. */
32
32
  set(key: K, value: V): boolean;
33
+ /**
34
+ * Appends `element` to the array stored under `key`, storing a new array when there is none:
35
+ * a `get` and a `set` in one lookup, for the groupings of `groupBy`, `toLookup` and `join`.
36
+ */
37
+ append<E>(this: HashCore<K, E[]>, key: K, element: E): void;
33
38
  delete(key: K): boolean;
34
39
  clear(): void;
35
40
  /** An O(n) copy: same comparer, same enumeration order. */
@@ -71,6 +71,30 @@ export class HashCore {
71
71
  this._bucketOf(this._hash(key)).push(key);
72
72
  return true;
73
73
  }
74
+ /**
75
+ * Appends `element` to the array stored under `key`, storing a new array when there is none:
76
+ * a `get` and a `set` in one lookup, for the groupings of `groupBy`, `toLookup` and `join`.
77
+ */
78
+ append(key, element) {
79
+ const entries = this._entries;
80
+ if (this._direct(key)) {
81
+ const bucket = entries.get(key);
82
+ if (bucket === undefined) {
83
+ entries.set(key, [element]);
84
+ }
85
+ else {
86
+ bucket.push(element);
87
+ }
88
+ return;
89
+ }
90
+ const stored = this._find(key);
91
+ if (!isMissing(stored)) {
92
+ entries.get(stored).push(element);
93
+ return;
94
+ }
95
+ entries.set(key, [element]);
96
+ this._bucketOf(this._hash(key)).push(key);
97
+ }
74
98
  delete(key) {
75
99
  if (this._direct(key)) {
76
100
  return this._entries.delete(key);
@@ -1,4 +1,4 @@
1
- import type { IList, Span } from "./interfaces.ts";
1
+ import type { IList, IReadOnlyList, Span } from "./interfaces.ts";
2
2
  import { List } from "./list.ts";
3
3
  /** `arr[^n]` on a native array (`T[]`, `JsArray<T>`): always checked. */
4
4
  export declare function fromEnd<T>(array: readonly T[], n: number): T;
@@ -10,8 +10,21 @@ export declare function isIterable(value: unknown): value is Iterable<unknown>;
10
10
  * `null` otherwise.
11
11
  */
12
12
  export declare function asArray(value: unknown): unknown[] | null;
13
- /** `list.AsJsArray()`: the backing array itself; `ArgumentNullException` for `null`. */
13
+ /**
14
+ * `list.AsJsArray()`: the backing array itself; `ArgumentNullException` for `null`.
15
+ * Contract (mirroring `CollectionsMarshal.AsSpan`): do not add or remove elements
16
+ * through the writable view while the `List` is being enumerated (it bypasses `_version`).
17
+ */
14
18
  export declare function asJsArray<T>(list: List<T> | null | undefined): T[];
19
+ /**
20
+ * `list.AsReadOnlyJsArray()`: a read-only view `readonly T[]` over the list's
21
+ * backing array; `ArgumentNullException` for `null`. Zero-copy and live for
22
+ * runtime array-backed list kinds (and native arrays); a shallow snapshot copy
23
+ * (`Array.from(list)`) when `ReadOnlyList` wraps a custom `IList`, when the
24
+ * receiver lacks `asReadOnlyJsArray()` (such as a user `IReadOnlyList` class),
25
+ * and for older immutable versions sharing a grown store.
26
+ */
27
+ export declare function asReadOnlyJsArray<T>(list: IReadOnlyList<T> | readonly T[] | null | undefined): readonly T[];
15
28
  /** `list.AsSpan()` / `CollectionsMarshal.AsSpan(list)`: the backing array; empty for `null`. */
16
29
  export declare function asSpan<T>(list: List<T> | null | undefined): Span<T>;
17
30
  /** `list.AsReadOnlySpan()`: the backing array, read-only; empty for `null`. */
@@ -26,4 +39,4 @@ export declare function asReadOnlySpan<T>(list: List<T> | null | undefined): Rea
26
39
  * collection descriptor), so an object that is not a list fails at its first use instead
27
40
  * of at the cast.
28
41
  */
29
- export declare function asList<T>(value: unknown, targetType: string): IList<T> | null;
42
+ export declare function asList<T>(value: unknown, targetType: string): (IList<T> & IReadOnlyList<T>) | null;
@@ -33,13 +33,37 @@ export function asArray(value) {
33
33
  }
34
34
  return value instanceof List && value.isFixedSize ? value._items : null;
35
35
  }
36
- /** `list.AsJsArray()`: the backing array itself; `ArgumentNullException` for `null`. */
36
+ /**
37
+ * `list.AsJsArray()`: the backing array itself; `ArgumentNullException` for `null`.
38
+ * Contract (mirroring `CollectionsMarshal.AsSpan`): do not add or remove elements
39
+ * through the writable view while the `List` is being enumerated (it bypasses `_version`).
40
+ */
37
41
  export function asJsArray(list) {
38
42
  if (list === null || list === undefined) {
39
43
  throwArgumentNull("list");
40
44
  }
41
45
  return list._items;
42
46
  }
47
+ /**
48
+ * `list.AsReadOnlyJsArray()`: a read-only view `readonly T[]` over the list's
49
+ * backing array; `ArgumentNullException` for `null`. Zero-copy and live for
50
+ * runtime array-backed list kinds (and native arrays); a shallow snapshot copy
51
+ * (`Array.from(list)`) when `ReadOnlyList` wraps a custom `IList`, when the
52
+ * receiver lacks `asReadOnlyJsArray()` (such as a user `IReadOnlyList` class),
53
+ * and for older immutable versions sharing a grown store.
54
+ */
55
+ export function asReadOnlyJsArray(list) {
56
+ if (list === null || list === undefined) {
57
+ throwArgumentNull("list");
58
+ }
59
+ if (Array.isArray(list)) {
60
+ return list;
61
+ }
62
+ if (typeof list.asReadOnlyJsArray === "function") {
63
+ return list.asReadOnlyJsArray();
64
+ }
65
+ return Array.from(list);
66
+ }
43
67
  /** `list.AsSpan()` / `CollectionsMarshal.AsSpan(list)`: the backing array; empty for `null`. */
44
68
  export function asSpan(list) {
45
69
  return list === null || list === undefined ? [] : list._items;
@@ -65,7 +89,9 @@ export function asList(value, targetType) {
65
89
  if (isPrimitive(value)) {
66
90
  throw invalidCast(value, targetType);
67
91
  }
68
- return Array.isArray(value) ? List.adopt(value, { fixedSize: true }) : value;
92
+ return Array.isArray(value)
93
+ ? List.adopt(value, { fixedSize: true })
94
+ : value;
69
95
  }
70
96
  function isPrimitive(value) {
71
97
  const type = typeof value;
@@ -1,5 +1,5 @@
1
1
  import { type ComparisonArgument } from "./default-comparer.ts";
2
- import type { IList } from "./interfaces.ts";
2
+ import type { IList, IReadOnlyList } from "./interfaces.ts";
3
3
  import { List } from "./list.ts";
4
4
  /**
5
5
  * The one implementation behind `ImmutableList<T>.Builder` and
@@ -12,7 +12,7 @@ import { List } from "./list.ts";
12
12
  * check with .NET's default `ArgumentOutOfRangeException` text, not `List`'s.
13
13
  * Internal.
14
14
  */
15
- export declare abstract class ImmutableListBuilderCore<T> implements IList<T> {
15
+ export declare abstract class ImmutableListBuilderCore<T> implements IList<T>, IReadOnlyList<T> {
16
16
  readonly _list: List<T>;
17
17
  /** Defines `_list`, a copy of `source`; each subclass then defines its own fields. */
18
18
  constructor(source: Iterable<T> | null | undefined);
@@ -30,6 +30,7 @@ export declare abstract class ImmutableListBuilderCore<T> implements IList<T> {
30
30
  /** The `[start, length]` a backward search covers, or `null` when .NET returns `-1` without checking. */
31
31
  protected abstract backwardRange(startIndex: number | undefined, count: number | undefined): readonly [start: number, length: number] | null;
32
32
  get count(): number;
33
+ get length(): number;
33
34
  get isReadOnly(): boolean;
34
35
  get(index: number): T;
35
36
  set(index: number, value: T): void;
@@ -53,6 +54,10 @@ export declare abstract class ImmutableListBuilderCore<T> implements IList<T> {
53
54
  sort(comparison?: ComparisonArgument<T>): void;
54
55
  reverse(): void;
55
56
  copyTo(array: T[], arrayIndex?: number): void;
57
+ /** Detached shallow copy (FR-055). */
58
+ toArray(): T[];
59
+ /** Zero-copy live read-only view over the builder's backing array (FR-056). */
60
+ asReadOnlyJsArray(): readonly T[];
56
61
  toJSON(): T[];
57
62
  private checkInsertIndex;
58
63
  }
@@ -20,6 +20,9 @@ export class ImmutableListBuilderCore {
20
20
  get count() {
21
21
  return this._list.count;
22
22
  }
23
+ get length() {
24
+ return this._list.count;
25
+ }
23
26
  get isReadOnly() {
24
27
  return false;
25
28
  }
@@ -125,6 +128,14 @@ export class ImmutableListBuilderCore {
125
128
  copyTo(array, arrayIndex = 0) {
126
129
  this._list.copyTo(array, arrayIndex);
127
130
  }
131
+ /** Detached shallow copy (FR-055). */
132
+ toArray() {
133
+ return this._list.toArray();
134
+ }
135
+ /** Zero-copy live read-only view over the builder's backing array (FR-056). */
136
+ asReadOnlyJsArray() {
137
+ return this._list.asReadOnlyJsArray();
138
+ }
128
139
  toJSON() {
129
140
  return this._list.toArray();
130
141
  }
@@ -18,6 +18,7 @@ export declare abstract class ImmutableListCore<T, Self extends ImmutableListCor
18
18
  /** Defines `_items` and `_count`; each subclass then defines its own `$typeArguments$<Class>` slot. */
19
19
  constructor(items: T[], count: number);
20
20
  get count(): number;
21
+ get length(): number;
21
22
  get isEmpty(): boolean;
22
23
  /** The indexer, with the subclass's .NET exception. */
23
24
  abstract get(index: number): T;
@@ -64,6 +65,13 @@ export declare abstract class ImmutableListCore<T, Self extends ImmutableListCor
64
65
  reverse(): Self;
65
66
  /** A detached copy. */
66
67
  toArray(): T[];
68
+ /**
69
+ * Read-only view over `_items`: zero-copy when this version owns the full array
70
+ * (detaching tip-append ownership so later `add` calls copy rather than mutating
71
+ * the view); falls back to a shallow copy when an older version shares a grown
72
+ * store (values are immutable, though array identity is not stable across calls).
73
+ */
74
+ asReadOnlyJsArray(): readonly T[];
67
75
  toJSON(): T[];
68
76
  [Symbol.iterator](): Iterator<T>;
69
77
  /** The first `count` elements from `index` that equal `item`, `index` checked under `indexName`. */
@@ -1,5 +1,5 @@
1
1
  import { comparisonOf, elementEquals, } from "./default-comparer.js";
2
- import { ownsTip } from "./immutable-storage.js";
2
+ import { detachTip, ownsTip } from "./immutable-storage.js";
3
3
  import { binarySearch } from "./sorted-array.js";
4
4
  import { throwArgumentNull, throwArgumentOutOfRange, throwCompareFailed, throwOldValueNotFound, } from "./throw-helpers.js";
5
5
  /**
@@ -22,6 +22,9 @@ export class ImmutableListCore {
22
22
  get count() {
23
23
  return this._count;
24
24
  }
25
+ get length() {
26
+ return this._count;
27
+ }
25
28
  get isEmpty() {
26
29
  return this._count === 0;
27
30
  }
@@ -138,6 +141,21 @@ export class ImmutableListCore {
138
141
  toArray() {
139
142
  return this.visible();
140
143
  }
144
+ /**
145
+ * Read-only view over `_items`: zero-copy when this version owns the full array
146
+ * (detaching tip-append ownership so later `add` calls copy rather than mutating
147
+ * the view); falls back to a shallow copy when an older version shares a grown
148
+ * store (values are immutable, though array identity is not stable across calls).
149
+ */
150
+ asReadOnlyJsArray() {
151
+ if (this._items.length !== this._count) {
152
+ return this.visible();
153
+ }
154
+ if (this._count > 0) {
155
+ detachTip(this._items);
156
+ }
157
+ return this._items;
158
+ }
141
159
  toJSON() {
142
160
  return this.visible();
143
161
  }
@@ -14,6 +14,8 @@
14
14
  */
15
15
  import { type IEqualityComparer } from "./default-comparer.ts";
16
16
  import { HashCore } from "./hash-core.ts";
17
+ /** Marks `store` as exposed to a read-only view so later appends copy instead of mutating it. */
18
+ export declare function detachTip(store: object): void;
17
19
  /** W4-11's ownership rule: a version of `count` entries may append to `store` in place. */
18
20
  export declare function ownsTip(store: {
19
21
  readonly length: number;
@@ -14,10 +14,15 @@
14
14
  */
15
15
  import { defaultEqualityComparer } from "./default-comparer.js";
16
16
  import { HashCore } from "./hash-core.js";
17
+ let detachedTips;
18
+ /** Marks `store` as exposed to a read-only view so later appends copy instead of mutating it. */
19
+ export function detachTip(store) {
20
+ (detachedTips ??= new WeakSet()).add(store);
21
+ }
17
22
  /** W4-11's ownership rule: a version of `count` entries may append to `store` in place. */
18
23
  export function ownsTip(store, count) {
19
24
  // Empty versions can be canonical singletons: never mutate their backing store.
20
- return count > 0 && store.length === count;
25
+ return count > 0 && store.length === count && !detachedTips?.has(store);
21
26
  }
22
27
  /** `null` for the default equality comparer, so comparing comparers sees one default. */
23
28
  export function normalizedComparer(comparer) {
@@ -7,7 +7,20 @@ export interface IReadOnlyCollection<T> extends Iterable<T> {
7
7
  readonly count: number;
8
8
  }
9
9
  export interface IReadOnlyList<T> extends IReadOnlyCollection<T> {
10
+ /** Mirrors `count` for JavaScript/TypeScript array ergonomics. */
11
+ readonly length: number;
10
12
  get(index: number): T;
13
+ /** Detached shallow copy of the list's elements (spec 011 FR-055). */
14
+ toArray(): T[];
15
+ /**
16
+ * Read-only view over the backing JS array (spec 011 FR-056): zero-copy and
17
+ * live for runtime array-backed list kinds (reflects later mutations of a
18
+ * mutable source; array identity is not stable for older immutable versions
19
+ * sharing a grown store), and a shallow snapshot copy when `ReadOnlyList`
20
+ * wraps a custom `IList` without `asReadOnlyJsArray()`. Read-only for the
21
+ * consumer.
22
+ */
23
+ asReadOnlyJsArray(): readonly T[];
11
24
  }
12
25
  export interface ICollection<T> extends Iterable<T> {
13
26
  readonly count: number;
@@ -34,6 +34,7 @@ export declare class List<T> implements IList<T>, IReadOnlyList<T> {
34
34
  /** The runtime type descriptor of the open `List<T>`: a data literal, built on first read. */
35
35
  static get $runtimeType(): RuntimeType<List<unknown>>;
36
36
  get count(): number;
37
+ get length(): number;
37
38
  get isReadOnly(): boolean;
38
39
  get isFixedSize(): boolean;
39
40
  get(index: number): T;
@@ -82,6 +83,8 @@ export declare class List<T> implements IList<T>, IReadOnlyList<T> {
82
83
  toArray(): T[];
83
84
  /** The backing array itself (FR-056). */
84
85
  asJsArray(): T[];
86
+ /** The backing array itself, typed read-only (FR-056). */
87
+ asReadOnlyJsArray(): readonly T[];
85
88
  /** The backing array itself, typed as a length-keeping view (FR-056). */
86
89
  asSpan(): Span<T>;
87
90
  /** The backing array itself, typed read-only (FR-056). */
@@ -56,6 +56,9 @@ class List {
56
56
  get count() {
57
57
  return this._items.length;
58
58
  }
59
+ get length() {
60
+ return this._items.length;
61
+ }
59
62
  get isReadOnly() {
60
63
  return !1;
61
64
  }
@@ -220,6 +223,10 @@ class List {
220
223
  asJsArray() {
221
224
  return this._items;
222
225
  }
226
+ /** The backing array itself, typed read-only (FR-056). */
227
+ asReadOnlyJsArray() {
228
+ return this._items;
229
+ }
223
230
  /** The backing array itself, typed as a length-keeping view (FR-056). */
224
231
  asSpan() {
225
232
  return this._items;
@@ -20,6 +20,7 @@ export declare class ReadOnlyList<T> implements IList<T>, IReadOnlyList<T> {
20
20
  static empty<T>(typeArguments?: readonly RuntimeType[] | null): ReadOnlyList<T>;
21
21
  static get $runtimeType(): CollectionType<ReadOnlyList<unknown>>;
22
22
  get count(): number;
23
+ get length(): number;
23
24
  get isReadOnly(): boolean;
24
25
  /** The source's element; an array source is bounds-checked as .NET's array `IList<T>` indexer. */
25
26
  get(index: number): T;
@@ -36,6 +37,16 @@ export declare class ReadOnlyList<T> implements IList<T>, IReadOnlyList<T> {
36
37
  remove(_item: T): never;
37
38
  removeAt(_index: number): never;
38
39
  clear(): never;
40
+ /** Detached shallow copy of the source's elements (FR-055). */
41
+ toArray(): T[];
42
+ /**
43
+ * Read-only view over the source's backing array (spec 011 FR-056):
44
+ * zero-copy and live ONLY for runtime array-backed sources (a native array,
45
+ * a `List`, or a source exposing `asReadOnlyJsArray()`). For a custom `IList`
46
+ * source without `asReadOnlyJsArray()`, returns a **shallow snapshot copy**
47
+ * (not live; allocates a new array on each call).
48
+ */
49
+ asReadOnlyJsArray(): readonly T[];
39
50
  toJSON(): T[];
40
51
  /** .NET's ``System.Collections.ObjectModel.ReadOnlyCollection`1[T]`` text. */
41
52
  toString(): string;
@@ -66,6 +66,9 @@ export class ReadOnlyList {
66
66
  const source = this._source;
67
67
  return isArraySource(source) ? source.length : source.count;
68
68
  }
69
+ get length() {
70
+ return this.count;
71
+ }
69
72
  get isReadOnly() {
70
73
  return true;
71
74
  }
@@ -129,8 +132,27 @@ export class ReadOnlyList {
129
132
  clear() {
130
133
  return throwReadOnly();
131
134
  }
135
+ /** Detached shallow copy of the source's elements (FR-055). */
136
+ toArray() {
137
+ const source = this._source;
138
+ return isArraySource(source) ? source.slice() : Array.from(source);
139
+ }
140
+ /**
141
+ * Read-only view over the source's backing array (spec 011 FR-056):
142
+ * zero-copy and live ONLY for runtime array-backed sources (a native array,
143
+ * a `List`, or a source exposing `asReadOnlyJsArray()`). For a custom `IList`
144
+ * source without `asReadOnlyJsArray()`, returns a **shallow snapshot copy**
145
+ * (not live; allocates a new array on each call).
146
+ */
147
+ asReadOnlyJsArray() {
148
+ const source = this._source;
149
+ if (isArraySource(source)) {
150
+ return source;
151
+ }
152
+ return hasAsReadOnlyJsArray(source) ? source.asReadOnlyJsArray() : Array.from(source);
153
+ }
132
154
  toJSON() {
133
- return [...this];
155
+ return this.toArray();
134
156
  }
135
157
  /** .NET's ``System.Collections.ObjectModel.ReadOnlyCollection`1[T]`` text. */
136
158
  toString() {
@@ -150,3 +172,6 @@ export function asReadOnly(list, typeArguments) {
150
172
  function isArraySource(source) {
151
173
  return Array.isArray(source);
152
174
  }
175
+ function hasAsReadOnlyJsArray(source) {
176
+ return typeof source.asReadOnlyJsArray === "function";
177
+ }
@@ -109,6 +109,7 @@ export declare class SortedListView<T> implements IList<T>, IReadOnlyList<T> {
109
109
  /** .NET's `KeyList` / `ValueList`: `IList<T>` and `ICollection<T>`, no read-only interface. */
110
110
  static get $runtimeType(): CollectionType<SortedListView<unknown>>;
111
111
  get count(): number;
112
+ get length(): number;
112
113
  get isReadOnly(): true;
113
114
  get(index: number): T;
114
115
  /** `view[^n]`. */
@@ -124,6 +125,10 @@ export declare class SortedListView<T> implements IList<T>, IReadOnlyList<T> {
124
125
  remove(_item: T): never;
125
126
  removeAt(_index: number): never;
126
127
  clear(): never;
128
+ /** Detached shallow copy (FR-055). */
129
+ toArray(): T[];
130
+ /** Zero-copy live read-only view over the list's backing key/value array (FR-056). */
131
+ asReadOnlyJsArray(): readonly T[];
127
132
  toJSON(): T[];
128
133
  [Symbol.iterator](): Iterator<T>;
129
134
  private items;
@@ -230,6 +230,9 @@ class SortedListView {
230
230
  get count() {
231
231
  return this._list.count;
232
232
  }
233
+ get length() {
234
+ return this._list.count;
235
+ }
233
236
  get isReadOnly() {
234
237
  return !0;
235
238
  }
@@ -269,8 +272,16 @@ class SortedListView {
269
272
  clear() {
270
273
  return throwSortedListNested();
271
274
  }
275
+ /** Detached shallow copy (FR-055). */
276
+ toArray() {
277
+ return this.items().slice();
278
+ }
279
+ /** Zero-copy live read-only view over the list's backing key/value array (FR-056). */
280
+ asReadOnlyJsArray() {
281
+ return this.items();
282
+ }
272
283
  toJSON() {
273
- return [...this.items()];
284
+ return this.toArray();
274
285
  }
275
286
  [Symbol.iterator]() {
276
287
  return new SortedListIterator(this._list, (index) => at(this.items(), index));