metano-runtime 3.1.0 → 3.3.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 (73) hide show
  1. package/dist/system/comparable-compare-to.d.ts +12 -0
  2. package/dist/system/comparable-compare-to.js +20 -0
  3. package/dist/system/disposable.d.ts +15 -0
  4. package/dist/system/disposable.js +15 -0
  5. package/dist/system/hash-code.d.ts +2 -1
  6. package/dist/system/hash-code.js +5 -2
  7. package/dist/system/index.d.ts +4 -0
  8. package/dist/system/index.js +4 -0
  9. package/dist/system/int64-math.js +2 -1
  10. package/dist/system/json/index.d.ts +1 -1
  11. package/dist/system/json/json-serializer.js +3 -2
  12. package/dist/system/json/json-text-writer.d.ts +10 -3
  13. package/dist/system/json/json-text-writer.js +48 -16
  14. package/dist/system/json/ordinal-ignore-case.d.ts +2 -0
  15. package/dist/system/json/ordinal-ignore-case.js +1 -1
  16. package/dist/system/json/serializer-context.d.ts +13 -7
  17. package/dist/system/json/serializer-context.js +23 -11
  18. package/dist/system/json/types.d.ts +20 -0
  19. package/dist/system/math.d.ts +20 -0
  20. package/dist/system/math.js +31 -0
  21. package/dist/system/min-max.d.ts +6 -0
  22. package/dist/system/min-max.js +10 -0
  23. package/dist/system/string-comparison.d.ts +56 -0
  24. package/dist/system/string-comparison.js +225 -0
  25. package/dist/system/string-hash-code.d.ts +14 -0
  26. package/dist/system/string-hash-code.js +37 -0
  27. package/dist/system/string-members.d.ts +5 -4
  28. package/dist/system/string-members.js +12 -11
  29. package/dist-unchecked/system/comparable-compare-to.d.ts +12 -0
  30. package/dist-unchecked/system/comparable-compare-to.js +20 -0
  31. package/dist-unchecked/system/disposable.d.ts +15 -0
  32. package/dist-unchecked/system/disposable.js +15 -0
  33. package/dist-unchecked/system/hash-code.d.ts +2 -1
  34. package/dist-unchecked/system/hash-code.js +5 -2
  35. package/dist-unchecked/system/index.d.ts +4 -0
  36. package/dist-unchecked/system/index.js +4 -0
  37. package/dist-unchecked/system/int64-math.js +2 -1
  38. package/dist-unchecked/system/json/index.d.ts +1 -1
  39. package/dist-unchecked/system/json/json-serializer.js +3 -2
  40. package/dist-unchecked/system/json/json-text-writer.d.ts +10 -3
  41. package/dist-unchecked/system/json/json-text-writer.js +48 -16
  42. package/dist-unchecked/system/json/ordinal-ignore-case.d.ts +2 -0
  43. package/dist-unchecked/system/json/ordinal-ignore-case.js +1 -1
  44. package/dist-unchecked/system/json/serializer-context.d.ts +13 -7
  45. package/dist-unchecked/system/json/serializer-context.js +23 -11
  46. package/dist-unchecked/system/json/types.d.ts +20 -0
  47. package/dist-unchecked/system/math.d.ts +20 -0
  48. package/dist-unchecked/system/math.js +31 -0
  49. package/dist-unchecked/system/min-max.d.ts +6 -0
  50. package/dist-unchecked/system/min-max.js +10 -0
  51. package/dist-unchecked/system/string-comparison.d.ts +56 -0
  52. package/dist-unchecked/system/string-comparison.js +225 -0
  53. package/dist-unchecked/system/string-hash-code.d.ts +14 -0
  54. package/dist-unchecked/system/string-hash-code.js +37 -0
  55. package/dist-unchecked/system/string-members.d.ts +5 -4
  56. package/dist-unchecked/system/string-members.js +12 -11
  57. package/package.json +1 -1
  58. package/src/system/comparable-compare-to.ts +23 -0
  59. package/src/system/disposable.ts +27 -0
  60. package/src/system/hash-code.ts +7 -2
  61. package/src/system/index.ts +4 -0
  62. package/src/system/int64-math.ts +2 -1
  63. package/src/system/json/index.ts +1 -0
  64. package/src/system/json/json-serializer.ts +3 -2
  65. package/src/system/json/json-text-writer.ts +74 -16
  66. package/src/system/json/ordinal-ignore-case.ts +1 -1
  67. package/src/system/json/serializer-context.ts +28 -11
  68. package/src/system/json/types.ts +21 -0
  69. package/src/system/math.ts +44 -0
  70. package/src/system/min-max.ts +11 -0
  71. package/src/system/string-comparison.ts +299 -0
  72. package/src/system/string-hash-code.ts +44 -0
  73. package/src/system/string-members.ts +12 -11
@@ -0,0 +1,299 @@
1
+ /**
2
+ * C#'s `string.Equals` and `string.Compare` with a `StringComparison` (#769), and the
3
+ * overloads that imply one (`Compare(a, b)` and `CompareTo` are culture-sensitive,
4
+ * `CompareOrdinal` is ordinal).
5
+ *
6
+ * - `Ordinal` compares UTF-16 code units.
7
+ * - `OrdinalIgnoreCase` compares the ordinal uppercase of each code point
8
+ * (`ordinalUppercaseOf`: .NET's invariant simple case mapping, so `ß` never equals
9
+ * `SS` and `ı` never equals `I`), not a whole-string `toLowerCase()`.
10
+ * - The culture members compare with `Intl.Collator`: `CurrentCulture` with the
11
+ * engine's default locale, `InvariantCulture` with the root locale, at `variant`
12
+ * sensitivity. The ignore-case members ignore case only, as .NET does on ICU: the
13
+ * strings first compare at `accent` sensitivity (which also ignores width and kana),
14
+ * and strings equal there are told apart by width and kana alone
15
+ * (`caseInsensitiveKey`), so `"A"` never equals `"a"` while `"A"` equals `"a"`,
16
+ * `ß` equals `ẞ` and `ff` equals `FF`. Measured against .NET 10 on ICU, five exotic
17
+ * pairs still differ (known gaps): `İ` / `i̇` and `ガ` / `ガ` and U+FFE8 / `│` (.NET
18
+ * equal), and small against full-size kana (`ア` / `ァ`, `ア` / `ァ`: the sign).
19
+ *
20
+ * The instance forms (`a.Equals(b, comparison)`, `a.Equals(b)`, `a.CompareTo(b)`) throw
21
+ * `NullReferenceException` for a `null` receiver, as .NET does; `CompareTo(object)`
22
+ * throws `ArgumentException` for an argument that is not a string.
23
+ *
24
+ * `null` equals only `null` and sorts before every string, as in .NET. A `Compare`
25
+ * result has .NET's sign; .NET leaves the magnitude unspecified, so the ordinal forms
26
+ * return the difference of the first code units that differ, or of the lengths.
27
+ */
28
+
29
+ import { ArgumentException } from "./exceptions/argument-exception.ts";
30
+ import { NullReferenceException } from "./exceptions/null-reference-exception.ts";
31
+ import { ordinalUppercaseOf } from "./json/ordinal-ignore-case.ts";
32
+ import { StringComparison } from "./string-members.ts";
33
+
34
+ const UNSUPPORTED_COMPARISON_MESSAGE =
35
+ "The string comparison type passed in is currently not supported.";
36
+
37
+ const NOT_A_STRING_MESSAGE = "Object must be of type String.";
38
+
39
+ const FIRST_SUPPLEMENTARY_CODE_POINT = 0x10000;
40
+
41
+ /* Built on first use, one per culture member, so importing the module runs no code. */
42
+ const cultureCollators: Intl.Collator[] = [];
43
+
44
+ /** The collator for a culture member (`CurrentCulture` to `InvariantCultureIgnoreCase`). */
45
+ function collatorFor(comparison: StringComparison): Intl.Collator {
46
+ const isInvariant = comparison >= StringComparison.InvariantCulture;
47
+ const ignoresCase = comparison % 2 === 1;
48
+
49
+ return (cultureCollators[comparison] ??= new Intl.Collator(isInvariant ? "und" : undefined, {
50
+ sensitivity: ignoresCase ? "accent" : "variant",
51
+ }));
52
+ }
53
+
54
+ /** @throws ArgumentException when `comparison` is not a `StringComparison` member. */
55
+ function requireComparison(comparison: StringComparison): void {
56
+ const isMember =
57
+ Number.isInteger(comparison) &&
58
+ comparison >= StringComparison.CurrentCulture &&
59
+ comparison <= StringComparison.OrdinalIgnoreCase;
60
+
61
+ if (!isMember) {
62
+ throw new ArgumentException(UNSUPPORTED_COMPARISON_MESSAGE, "comparisonType");
63
+ }
64
+ }
65
+
66
+ /** The ordinal uppercase code point of the code point at `index` in `value`. */
67
+ function foldedCodePointAt(value: string, index: number): number {
68
+ const unit = value.charCodeAt(index);
69
+
70
+ if (unit < 0x80) {
71
+ return unit >= 0x61 && unit <= 0x7a ? unit - 0x20 : unit;
72
+ }
73
+
74
+ const codePoint = value.codePointAt(index) as number;
75
+
76
+ return ordinalUppercaseOf(String.fromCodePoint(codePoint)).codePointAt(0) as number;
77
+ }
78
+
79
+ function compareOrdinal(left: string, right: string): number {
80
+ const length = Math.min(left.length, right.length);
81
+
82
+ for (let index = 0; index < length; index++) {
83
+ const difference = left.charCodeAt(index) - right.charCodeAt(index);
84
+
85
+ if (difference !== 0) {
86
+ return difference;
87
+ }
88
+ }
89
+
90
+ return left.length - right.length;
91
+ }
92
+
93
+ /**
94
+ * Walks both strings code point by code point; a supplementary code point orders by its
95
+ * value (above every BMP one), as .NET's case-insensitive comparison does.
96
+ */
97
+ function compareOrdinalIgnoreCase(left: string, right: string): number {
98
+ let leftIndex = 0;
99
+ let rightIndex = 0;
100
+
101
+ while (leftIndex < left.length && rightIndex < right.length) {
102
+ const leftFolded = foldedCodePointAt(left, leftIndex);
103
+ const rightFolded = foldedCodePointAt(right, rightIndex);
104
+
105
+ if (leftFolded !== rightFolded) {
106
+ const isSupplementary =
107
+ leftFolded >= FIRST_SUPPLEMENTARY_CODE_POINT ||
108
+ rightFolded >= FIRST_SUPPLEMENTARY_CODE_POINT;
109
+
110
+ return isSupplementary ? Math.sign(leftFolded - rightFolded) : leftFolded - rightFolded;
111
+ }
112
+
113
+ leftIndex += (left.codePointAt(leftIndex) as number) >= FIRST_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
114
+ rightIndex +=
115
+ (right.codePointAt(rightIndex) as number) >= FIRST_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
116
+ }
117
+
118
+ return left.length - leftIndex - (right.length - rightIndex);
119
+ }
120
+
121
+ /** Two non-null strings under `comparison`, already checked. */
122
+ function compareWith(left: string, right: string, comparison: StringComparison): number {
123
+ switch (comparison) {
124
+ case StringComparison.Ordinal:
125
+ return compareOrdinal(left, right);
126
+ case StringComparison.OrdinalIgnoreCase:
127
+ return compareOrdinalIgnoreCase(left, right);
128
+ case StringComparison.CurrentCultureIgnoreCase:
129
+ case StringComparison.InvariantCultureIgnoreCase:
130
+ return compareCultureIgnoringCase(left, right, comparison);
131
+ default:
132
+ return collatorFor(comparison).compare(left, right);
133
+ }
134
+ }
135
+
136
+ /**
137
+ * .NET's culture `IgnoreCase` on ICU ignores case but not width (`A` / `a`, `カ` / `カ`)
138
+ * or kana type (`あ` / `ア`), which `accent` sensitivity ignores too. Strings that differ
139
+ * at `accent` sensitivity keep that order; strings equal there compare their
140
+ * case-insensitive keys at `variant` sensitivity, where only width and kana remain.
141
+ */
142
+ function compareCultureIgnoringCase(
143
+ left: string,
144
+ right: string,
145
+ comparison: StringComparison,
146
+ ): number {
147
+ const primary = collatorFor(comparison).compare(left, right);
148
+
149
+ if (primary !== 0) {
150
+ return primary;
151
+ }
152
+
153
+ const caseSensitive = (comparison - 1) as StringComparison;
154
+
155
+ return collatorFor(caseSensitive).compare(caseInsensitiveKey(left), caseInsensitiveKey(right));
156
+ }
157
+
158
+ /**
159
+ * `value` with every difference .NET's culture `IgnoreCase` ignores removed: each code
160
+ * point outside the halfwidth and fullwidth forms is replaced by its compatibility
161
+ * decomposition (`ff` → `ff`, `①` → `1`, U+3000 → space) in a single case (simple
162
+ * uppercase, then simple lowercase: `ς` and `Σ` → `σ`, `ẞ` → `ß`). A width form keeps
163
+ * its width and only folds its case (`A` → `a`).
164
+ */
165
+ function caseInsensitiveKey(value: string): string {
166
+ let key = "";
167
+
168
+ for (const character of value) {
169
+ if (isWidthForm(character.codePointAt(0) as number)) {
170
+ key += singleCase(character);
171
+ continue;
172
+ }
173
+
174
+ for (const part of character.normalize("NFKC")) {
175
+ key += singleCase(part);
176
+ }
177
+ }
178
+
179
+ return key;
180
+ }
181
+
182
+ /** The Halfwidth and Fullwidth Forms block. */
183
+ function isWidthForm(codePoint: number): boolean {
184
+ return codePoint >= 0xff00 && codePoint <= 0xffef;
185
+ }
186
+
187
+ function singleCase(character: string): string {
188
+ const upper = simpleMapping(character, character.toUpperCase());
189
+
190
+ return simpleMapping(upper, upper.toLowerCase());
191
+ }
192
+
193
+ /** `mapped` when it is one code point (the simple mapping), else `character`. */
194
+ function simpleMapping(character: string, mapped: string): string {
195
+ return mapped.length === character.length || [...mapped].length === 1 ? mapped : character;
196
+ }
197
+
198
+ /**
199
+ * C#'s `string.Equals(a, b, comparison)` and `a.Equals(b, comparison)`.
200
+ *
201
+ * @throws ArgumentException when `comparison` is not a `StringComparison` member.
202
+ */
203
+ export function stringEquals(
204
+ left: string | null,
205
+ right: string | null,
206
+ comparison: StringComparison,
207
+ ): boolean {
208
+ requireComparison(comparison);
209
+
210
+ if (left === right) {
211
+ return true;
212
+ }
213
+
214
+ if (left === null || right === null || comparison === StringComparison.Ordinal) {
215
+ return false;
216
+ }
217
+
218
+ if (comparison === StringComparison.OrdinalIgnoreCase && left.length !== right.length) {
219
+ return false;
220
+ }
221
+
222
+ return compareWith(left, right, comparison) === 0;
223
+ }
224
+
225
+ /**
226
+ * C#'s `string.Compare(a, b, comparison)` and the overloads that imply a comparison:
227
+ * `Compare(a, b)`, `Compare(a, b, ignoreCase)`, `CompareOrdinal(a, b)`, `a.CompareTo(b)`.
228
+ *
229
+ * @throws ArgumentException when `comparison` is not a `StringComparison` member.
230
+ */
231
+ export function stringCompare(
232
+ left: string | null,
233
+ right: string | null,
234
+ comparison: StringComparison,
235
+ ): number {
236
+ requireComparison(comparison);
237
+
238
+ if (left === right) {
239
+ return 0;
240
+ }
241
+
242
+ if (left === null) {
243
+ return -1;
244
+ }
245
+
246
+ if (right === null) {
247
+ return 1;
248
+ }
249
+
250
+ return compareWith(left, right, comparison);
251
+ }
252
+
253
+ /** @throws NullReferenceException when the receiver of an instance call is `null`. */
254
+ function requireReceiver(value: string | null | undefined): string {
255
+ if (value == null) {
256
+ throw new NullReferenceException();
257
+ }
258
+
259
+ return value;
260
+ }
261
+
262
+ /**
263
+ * C#'s `a.Equals(b, comparison)` and `a.Equals(b)` (ordinal).
264
+ *
265
+ * @throws NullReferenceException when `value` is `null`.
266
+ * @throws ArgumentException when `comparison` is not a `StringComparison` member.
267
+ */
268
+ export function stringInstanceEquals(
269
+ value: string | null,
270
+ other: unknown,
271
+ comparison: StringComparison,
272
+ ): boolean {
273
+ const receiver = requireReceiver(value);
274
+
275
+ if (other === null || other === undefined || typeof other === "string") {
276
+ return stringEquals(receiver, other ?? null, comparison);
277
+ }
278
+
279
+ requireComparison(comparison);
280
+
281
+ return false;
282
+ }
283
+
284
+ /**
285
+ * C#'s `a.CompareTo(b)` for a `string` or `object` argument: a culture-sensitive
286
+ * comparison; `null` sorts first.
287
+ *
288
+ * @throws NullReferenceException when `value` is `null`.
289
+ * @throws ArgumentException when `other` is neither `null` nor a string.
290
+ */
291
+ export function stringCompareTo(value: string | null, other: unknown): number {
292
+ const receiver = requireReceiver(value);
293
+
294
+ if (other !== null && other !== undefined && typeof other !== "string") {
295
+ throw new ArgumentException(NOT_A_STRING_MESSAGE);
296
+ }
297
+
298
+ return stringCompare(receiver, other ?? null, StringComparison.CurrentCulture);
299
+ }
@@ -0,0 +1,44 @@
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
+
9
+ import { StringComparer } from "./collections/string-comparer.ts";
10
+ import { ArgumentException } from "./exceptions/argument-exception.ts";
11
+ import { NotSupportedException } from "./exceptions/not-supported-exception.ts";
12
+ import { NullReferenceException } from "./exceptions/null-reference-exception.ts";
13
+ import { StringComparison } from "./string-members.ts";
14
+
15
+ const UNSUPPORTED_COMPARISON_MESSAGE =
16
+ "The string comparison type passed in is currently not supported.";
17
+
18
+ const CULTURE_HASH_MESSAGE =
19
+ "GetHashCode with a culture-sensitive StringComparison is not supported in JavaScript.";
20
+
21
+ /**
22
+ * @throws NullReferenceException when `value` is `null`.
23
+ * @throws ArgumentException when `comparison` is not a `StringComparison` member.
24
+ * @throws NotSupportedException for a culture-sensitive comparison.
25
+ */
26
+ export function stringHashCode(value: string | null, comparison: StringComparison): number {
27
+ if (value == null) {
28
+ throw new NullReferenceException();
29
+ }
30
+
31
+ switch (comparison) {
32
+ case StringComparison.Ordinal:
33
+ return StringComparer.ordinal.getHashCode(value);
34
+ case StringComparison.OrdinalIgnoreCase:
35
+ return StringComparer.ordinalIgnoreCase.getHashCode(value);
36
+ case StringComparison.CurrentCulture:
37
+ case StringComparison.CurrentCultureIgnoreCase:
38
+ case StringComparison.InvariantCulture:
39
+ case StringComparison.InvariantCultureIgnoreCase:
40
+ throw new NotSupportedException(CULTURE_HASH_MESSAGE);
41
+ default:
42
+ throw new ArgumentException(UNSUPPORTED_COMPARISON_MESSAGE, "comparisonType");
43
+ }
44
+ }
@@ -8,15 +8,17 @@
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
  import { ArgumentException } from "./exceptions/argument-exception.ts";
18
19
  import { ArgumentNullException } from "./exceptions/argument-null-exception.ts";
19
20
  import { ArgumentOutOfRangeException } from "./exceptions/argument-out-of-range-exception.ts";
21
+ import { ordinalUppercaseOf } from "./json/ordinal-ignore-case.ts";
20
22
  import { isDotNetWhiteSpace } from "./strings.ts";
21
23
 
22
24
  /**
@@ -70,18 +72,17 @@ function ignoresCase(comparison: StringComparison | undefined): boolean {
70
72
  }
71
73
 
72
74
  /**
73
- * `value` with each UTF-16 code unit replaced by its invariant upper-case form when that
74
- * form is a single unit, so the result has the length of `value` and an index into one
75
- * is an index into the other.
75
+ * `value` with each code point replaced by its ordinal uppercase when that has the same
76
+ * UTF-16 length, so the result has the length of `value` and an index into one is an
77
+ * index into the other.
76
78
  */
77
79
  function foldCase(value: string): string {
78
80
  let folded = "";
79
81
 
80
- for (let index = 0; index < value.length; index++) {
81
- const unit = value[index] as string;
82
- const upper = unit.toUpperCase();
82
+ for (const character of value) {
83
+ const upper = ordinalUppercaseOf(character);
83
84
 
84
- folded += upper.length === 1 ? upper : unit;
85
+ folded += upper.length === character.length ? upper : character;
85
86
  }
86
87
 
87
88
  return folded;