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.
- package/dist/system/comparable-compare-to.d.ts +12 -0
- package/dist/system/comparable-compare-to.js +20 -0
- package/dist/system/disposable.d.ts +15 -0
- package/dist/system/disposable.js +15 -0
- package/dist/system/hash-code.d.ts +2 -1
- package/dist/system/hash-code.js +5 -2
- package/dist/system/index.d.ts +4 -0
- package/dist/system/index.js +4 -0
- package/dist/system/int64-math.js +2 -1
- package/dist/system/json/index.d.ts +1 -1
- package/dist/system/json/json-serializer.js +3 -2
- package/dist/system/json/json-text-writer.d.ts +10 -3
- package/dist/system/json/json-text-writer.js +48 -16
- package/dist/system/json/ordinal-ignore-case.d.ts +2 -0
- package/dist/system/json/ordinal-ignore-case.js +1 -1
- package/dist/system/json/serializer-context.d.ts +13 -7
- package/dist/system/json/serializer-context.js +23 -11
- package/dist/system/json/types.d.ts +20 -0
- package/dist/system/math.d.ts +20 -0
- package/dist/system/math.js +31 -0
- package/dist/system/min-max.d.ts +6 -0
- package/dist/system/min-max.js +10 -0
- package/dist/system/string-comparison.d.ts +56 -0
- package/dist/system/string-comparison.js +225 -0
- package/dist/system/string-hash-code.d.ts +14 -0
- package/dist/system/string-hash-code.js +37 -0
- package/dist/system/string-members.d.ts +5 -4
- package/dist/system/string-members.js +12 -11
- package/dist-unchecked/system/comparable-compare-to.d.ts +12 -0
- package/dist-unchecked/system/comparable-compare-to.js +20 -0
- package/dist-unchecked/system/disposable.d.ts +15 -0
- package/dist-unchecked/system/disposable.js +15 -0
- package/dist-unchecked/system/hash-code.d.ts +2 -1
- package/dist-unchecked/system/hash-code.js +5 -2
- package/dist-unchecked/system/index.d.ts +4 -0
- package/dist-unchecked/system/index.js +4 -0
- package/dist-unchecked/system/int64-math.js +2 -1
- package/dist-unchecked/system/json/index.d.ts +1 -1
- package/dist-unchecked/system/json/json-serializer.js +3 -2
- package/dist-unchecked/system/json/json-text-writer.d.ts +10 -3
- package/dist-unchecked/system/json/json-text-writer.js +48 -16
- package/dist-unchecked/system/json/ordinal-ignore-case.d.ts +2 -0
- package/dist-unchecked/system/json/ordinal-ignore-case.js +1 -1
- package/dist-unchecked/system/json/serializer-context.d.ts +13 -7
- package/dist-unchecked/system/json/serializer-context.js +23 -11
- package/dist-unchecked/system/json/types.d.ts +20 -0
- package/dist-unchecked/system/math.d.ts +20 -0
- package/dist-unchecked/system/math.js +31 -0
- package/dist-unchecked/system/min-max.d.ts +6 -0
- package/dist-unchecked/system/min-max.js +10 -0
- package/dist-unchecked/system/string-comparison.d.ts +56 -0
- package/dist-unchecked/system/string-comparison.js +225 -0
- package/dist-unchecked/system/string-hash-code.d.ts +14 -0
- package/dist-unchecked/system/string-hash-code.js +37 -0
- package/dist-unchecked/system/string-members.d.ts +5 -4
- package/dist-unchecked/system/string-members.js +12 -11
- package/package.json +1 -1
- package/src/system/comparable-compare-to.ts +23 -0
- package/src/system/disposable.ts +27 -0
- package/src/system/hash-code.ts +7 -2
- package/src/system/index.ts +4 -0
- package/src/system/int64-math.ts +2 -1
- package/src/system/json/index.ts +1 -0
- package/src/system/json/json-serializer.ts +3 -2
- package/src/system/json/json-text-writer.ts +74 -16
- package/src/system/json/ordinal-ignore-case.ts +1 -1
- package/src/system/json/serializer-context.ts +28 -11
- package/src/system/json/types.ts +21 -0
- package/src/system/math.ts +44 -0
- package/src/system/min-max.ts +11 -0
- package/src/system/string-comparison.ts +299 -0
- package/src/system/string-hash-code.ts +44 -0
- package/src/system/string-members.ts +12 -11
|
@@ -12,3 +12,5 @@
|
|
|
12
12
|
*/
|
|
13
13
|
/** The key under which `name` equals every name that matches it ignoring case. */
|
|
14
14
|
export declare function ordinalIgnoreCaseKeyOf(name: string): string;
|
|
15
|
+
/** The ordinal uppercase of one code point (`character` may be a surrogate pair). */
|
|
16
|
+
export declare function ordinalUppercaseOf(character: string): string;
|
|
@@ -47,7 +47,7 @@ export function ordinalIgnoreCaseKeyOf(name) {
|
|
|
47
47
|
return Array.from(name, ordinalUppercaseOf).join("");
|
|
48
48
|
}
|
|
49
49
|
/** The ordinal uppercase of one code point (`character` may be a surrogate pair). */
|
|
50
|
-
function ordinalUppercaseOf(character) {
|
|
50
|
+
export function ordinalUppercaseOf(character) {
|
|
51
51
|
const exception = ORDINAL_UPPERCASE_EXCEPTIONS().get(character);
|
|
52
52
|
if (exception !== undefined) {
|
|
53
53
|
return exception;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { TypeSpec, ValueSpec, SerializerContextOptions, JsonConverter, JsonReadOptions } from "./types.ts";
|
|
1
|
+
import type { TypeSpec, ValueSpec, SerializerContextOptions, JsonConverter, JsonReadOptions, JsonWriteOptions } from "./types.ts";
|
|
2
2
|
/**
|
|
3
3
|
* Base class for generated serializer contexts. Each context holds options
|
|
4
4
|
* (including custom converters) and a registry of type specs for lookup.
|
|
@@ -18,9 +18,15 @@ export declare abstract class SerializerContext {
|
|
|
18
18
|
* default.
|
|
19
19
|
*/
|
|
20
20
|
protected get readOptions(): JsonReadOptions;
|
|
21
|
+
/**
|
|
22
|
+
* The text formatting of every spec this context creates, mirrored from the
|
|
23
|
+
* C# context's `[JsonSourceGenerationOptions]` (`WriteIndented` and friends,
|
|
24
|
+
* #752). Generated contexts override it only for an indented context.
|
|
25
|
+
*/
|
|
26
|
+
protected get writeOptions(): JsonWriteOptions;
|
|
21
27
|
/**
|
|
22
28
|
* Registers a spec in the internal registry and returns it, with the
|
|
23
|
-
* context's read options attached. Intended to be called from lazy getters
|
|
29
|
+
* context's read and write options attached. Intended to be called from lazy getters
|
|
24
30
|
* in generated subclasses:
|
|
25
31
|
*
|
|
26
32
|
* ```ts
|
|
@@ -42,15 +48,15 @@ export declare abstract class SerializerContext {
|
|
|
42
48
|
* ```
|
|
43
49
|
*
|
|
44
50
|
* Nothing is registered: a value spec has no constructor to resolve. The
|
|
45
|
-
* context's read options are attached, as by `createSpec`.
|
|
51
|
+
* context's read and write options are attached, as by `createSpec`.
|
|
46
52
|
*/
|
|
47
53
|
protected createValueSpec<T>(spec: ValueSpec<T>): ValueSpec<T>;
|
|
48
54
|
/**
|
|
49
|
-
* `spec` with the context's read options, under any the spec
|
|
50
|
-
* itself. A spec stays as given when neither has one, so default
|
|
51
|
-
* keep their specs unchanged.
|
|
55
|
+
* `spec` with the context's read and write options, under any the spec
|
|
56
|
+
* declares itself. A spec stays as given when neither has one, so default
|
|
57
|
+
* contexts keep their specs unchanged.
|
|
52
58
|
*/
|
|
53
|
-
private
|
|
59
|
+
private withContextOptions;
|
|
54
60
|
/**
|
|
55
61
|
* Looks up a spec by constructor reference. Returns undefined if the type
|
|
56
62
|
* was not registered via createSpec().
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/** System.Text.Json's default read behavior: no option set. */
|
|
2
2
|
const DEFAULT_READ_OPTIONS = /* @__PURE__ */ Object.freeze({});
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/** System.Text.Json's default text formatting: compact text. */
|
|
4
|
+
const DEFAULT_WRITE_OPTIONS = /* @__PURE__ */ Object.freeze({});
|
|
5
|
+
function hasNoOptions(options) {
|
|
6
|
+
return Object.keys(options).length === 0;
|
|
5
7
|
}
|
|
6
8
|
/**
|
|
7
9
|
* Base class for generated serializer contexts. Each context holds options
|
|
@@ -28,9 +30,17 @@ export class SerializerContext {
|
|
|
28
30
|
get readOptions() {
|
|
29
31
|
return DEFAULT_READ_OPTIONS;
|
|
30
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* The text formatting of every spec this context creates, mirrored from the
|
|
35
|
+
* C# context's `[JsonSourceGenerationOptions]` (`WriteIndented` and friends,
|
|
36
|
+
* #752). Generated contexts override it only for an indented context.
|
|
37
|
+
*/
|
|
38
|
+
get writeOptions() {
|
|
39
|
+
return DEFAULT_WRITE_OPTIONS;
|
|
40
|
+
}
|
|
31
41
|
/**
|
|
32
42
|
* Registers a spec in the internal registry and returns it, with the
|
|
33
|
-
* context's read options attached. Intended to be called from lazy getters
|
|
43
|
+
* context's read and write options attached. Intended to be called from lazy getters
|
|
34
44
|
* in generated subclasses:
|
|
35
45
|
*
|
|
36
46
|
* ```ts
|
|
@@ -40,7 +50,7 @@ export class SerializerContext {
|
|
|
40
50
|
* ```
|
|
41
51
|
*/
|
|
42
52
|
createSpec(spec) {
|
|
43
|
-
const attached = this.
|
|
53
|
+
const attached = this.withContextOptions(spec);
|
|
44
54
|
// Only register class-backed types (those with a constructor reference).
|
|
45
55
|
// [PlainObject] specs have no `type` field — they're accessed via their
|
|
46
56
|
// getter on the context, not via constructor-keyed resolve().
|
|
@@ -61,19 +71,21 @@ export class SerializerContext {
|
|
|
61
71
|
* ```
|
|
62
72
|
*
|
|
63
73
|
* Nothing is registered: a value spec has no constructor to resolve. The
|
|
64
|
-
* context's read options are attached, as by `createSpec`.
|
|
74
|
+
* context's read and write options are attached, as by `createSpec`.
|
|
65
75
|
*/
|
|
66
76
|
createValueSpec(spec) {
|
|
67
|
-
return this.
|
|
77
|
+
return this.withContextOptions(spec);
|
|
68
78
|
}
|
|
69
79
|
/**
|
|
70
|
-
* `spec` with the context's read options, under any the spec
|
|
71
|
-
* itself. A spec stays as given when neither has one, so default
|
|
72
|
-
* keep their specs unchanged.
|
|
80
|
+
* `spec` with the context's read and write options, under any the spec
|
|
81
|
+
* declares itself. A spec stays as given when neither has one, so default
|
|
82
|
+
* contexts keep their specs unchanged.
|
|
73
83
|
*/
|
|
74
|
-
|
|
84
|
+
withContextOptions(spec) {
|
|
75
85
|
const readOptions = { ...this.readOptions, ...spec.readOptions };
|
|
76
|
-
|
|
86
|
+
const writeOptions = { ...this.writeOptions, ...spec.writeOptions };
|
|
87
|
+
const withReads = hasNoOptions(readOptions) ? spec : { ...spec, readOptions };
|
|
88
|
+
return hasNoOptions(writeOptions) ? withReads : { ...withReads, writeOptions };
|
|
77
89
|
}
|
|
78
90
|
/**
|
|
79
91
|
* Looks up a spec by constructor reference. Returns undefined if the type
|
|
@@ -244,6 +244,8 @@ export interface TypeSpec<T = unknown> {
|
|
|
244
244
|
readonly typeName?: string;
|
|
245
245
|
/** The context's read behavior; absent when it is System.Text.Json's default. */
|
|
246
246
|
readonly readOptions?: JsonReadOptions;
|
|
247
|
+
/** The context's text formatting; absent when it writes compact text (the default). */
|
|
248
|
+
readonly writeOptions?: JsonWriteOptions;
|
|
247
249
|
/**
|
|
248
250
|
* TS names of this contract's properties (its base chain's included) that
|
|
249
251
|
* a read sets after construction, and only when the payload carries their
|
|
@@ -309,6 +311,22 @@ export interface JsonReadOptions {
|
|
|
309
311
|
/** The type discriminator may appear after other properties (FR-002). */
|
|
310
312
|
readonly allowOutOfOrderMetadataProperties?: boolean;
|
|
311
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Context-level text formatting mirrored from `[JsonSourceGenerationOptions]`
|
|
316
|
+
* (`WriteIndented`, `IndentCharacter`, `IndentSize`, `NewLine`); only
|
|
317
|
+
* `serializeText` reads it. Every member is optional; an absent member is
|
|
318
|
+
* System.Text.Json's default.
|
|
319
|
+
*/
|
|
320
|
+
export interface JsonWriteOptions {
|
|
321
|
+
/** Write each element and property on its own line. */
|
|
322
|
+
readonly writeIndented?: boolean;
|
|
323
|
+
/** The indent character, a space (the default) or a tab. */
|
|
324
|
+
readonly indentCharacter?: " " | "\t";
|
|
325
|
+
/** The indent characters per nesting level, 0 to 127; 2 by default. */
|
|
326
|
+
readonly indentSize?: number;
|
|
327
|
+
/** The line break; `"\n"` by default (System.Text.Json's is the platform's). */
|
|
328
|
+
readonly newLine?: "\n" | "\r\n";
|
|
329
|
+
}
|
|
312
330
|
/**
|
|
313
331
|
* Spec of a non-object `[JsonSerializable]` root (a collection, dictionary,
|
|
314
332
|
* set, primitive, enum, branded or nullable type): the same descriptor that
|
|
@@ -337,6 +355,8 @@ export interface ValueSpec<T = unknown> {
|
|
|
337
355
|
readonly typeName?: string;
|
|
338
356
|
/** The context's read behavior; absent when it is System.Text.Json's default. */
|
|
339
357
|
readonly readOptions?: JsonReadOptions;
|
|
358
|
+
/** The context's text formatting; absent when it writes compact text (the default). */
|
|
359
|
+
readonly writeOptions?: JsonWriteOptions;
|
|
340
360
|
}
|
|
341
361
|
/** Any spec the generated context exposes: an object contract or a value root. */
|
|
342
362
|
export type JsonTypeInfo<T = unknown> = TypeSpec<T> | ValueSpec<T>;
|
|
@@ -60,3 +60,23 @@ export declare function roundDecimal<D extends DecimalRoundable<D>>(value: D, di
|
|
|
60
60
|
* @throws OverflowException, with .NET's message, for `int.MinValue`.
|
|
61
61
|
*/
|
|
62
62
|
export declare function absInt32(value: number): number;
|
|
63
|
+
/**
|
|
64
|
+
* `Math.Clamp(value, min, max)` for the `int`, `short`, `byte`, `float`, `double` (and the
|
|
65
|
+
* other `number`-backed) overloads (#750); JavaScript has no `Math.clamp`. A `NaN` `value`
|
|
66
|
+
* stays `NaN`, as in .NET.
|
|
67
|
+
*
|
|
68
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
69
|
+
*/
|
|
70
|
+
export declare function clampNumber(value: number, min: number, max: number): number;
|
|
71
|
+
/** Structural view of a `decimal.js` `Decimal`, the only operations {@link clampDecimal} uses. */
|
|
72
|
+
export interface DecimalClampable<D> {
|
|
73
|
+
lt(other: D): boolean;
|
|
74
|
+
gt(other: D): boolean;
|
|
75
|
+
toString(): string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `Math.Clamp(decimal, decimal, decimal)` (#750): the `Decimal` keeps its kind.
|
|
79
|
+
*
|
|
80
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
81
|
+
*/
|
|
82
|
+
export declare function clampDecimal<D extends DecimalClampable<D>>(value: D, min: D, max: D): D;
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import { ArgumentException } from "./exceptions/argument-exception.js";
|
|
12
12
|
import { ArgumentOutOfRangeException } from "./exceptions/argument-out-of-range-exception.js";
|
|
13
13
|
import { OverflowException } from "./exceptions/overflow-exception.js";
|
|
14
|
+
import { throwMinGreaterThanMax } from "./min-max.js";
|
|
14
15
|
/**
|
|
15
16
|
* Mirror of `System.MidpointRounding`. The numeric values match the .NET enum, so a
|
|
16
17
|
* value read from serialized C# data keeps its meaning.
|
|
@@ -153,3 +154,33 @@ export function absInt32(value) {
|
|
|
153
154
|
}
|
|
154
155
|
return Math.abs(value);
|
|
155
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* `Math.Clamp(value, min, max)` for the `int`, `short`, `byte`, `float`, `double` (and the
|
|
159
|
+
* other `number`-backed) overloads (#750); JavaScript has no `Math.clamp`. A `NaN` `value`
|
|
160
|
+
* stays `NaN`, as in .NET.
|
|
161
|
+
*
|
|
162
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
163
|
+
*/
|
|
164
|
+
export function clampNumber(value, min, max) {
|
|
165
|
+
if (min > max) {
|
|
166
|
+
throwMinGreaterThanMax(min, max);
|
|
167
|
+
}
|
|
168
|
+
if (value < min) {
|
|
169
|
+
return min;
|
|
170
|
+
}
|
|
171
|
+
return value > max ? max : value;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* `Math.Clamp(decimal, decimal, decimal)` (#750): the `Decimal` keeps its kind.
|
|
175
|
+
*
|
|
176
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
177
|
+
*/
|
|
178
|
+
export function clampDecimal(value, min, max) {
|
|
179
|
+
if (min.gt(max)) {
|
|
180
|
+
throwMinGreaterThanMax(min, max);
|
|
181
|
+
}
|
|
182
|
+
if (value.lt(min)) {
|
|
183
|
+
return min;
|
|
184
|
+
}
|
|
185
|
+
return value.gt(max) ? max : value;
|
|
186
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `ArgumentException` of `Math.Clamp` and `INumber<T>.Clamp` when `min` is greater than
|
|
3
|
+
* `max`, with .NET's message. The bounds are formatted as .NET formats them (a `decimal`
|
|
4
|
+
* `0.0000001` stays `0.0000001`, a `double` follows `double.ToString()`).
|
|
5
|
+
*/
|
|
6
|
+
export declare function throwMinGreaterThanMax(min: unknown, max: unknown): never;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ArgumentException } from "./exceptions/argument-exception.js";
|
|
2
|
+
import { formatText } from "./default-text.js";
|
|
3
|
+
/**
|
|
4
|
+
* The `ArgumentException` of `Math.Clamp` and `INumber<T>.Clamp` when `min` is greater than
|
|
5
|
+
* `max`, with .NET's message. The bounds are formatted as .NET formats them (a `decimal`
|
|
6
|
+
* `0.0000001` stays `0.0000001`, a `double` follows `double.ToString()`).
|
|
7
|
+
*/
|
|
8
|
+
export function throwMinGreaterThanMax(min, max) {
|
|
9
|
+
throw new ArgumentException(`'${formatText(min)}' cannot be greater than ${formatText(max)}.`);
|
|
10
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
import { StringComparison } from "./string-members.ts";
|
|
29
|
+
/**
|
|
30
|
+
* C#'s `string.Equals(a, b, comparison)` and `a.Equals(b, comparison)`.
|
|
31
|
+
*
|
|
32
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
33
|
+
*/
|
|
34
|
+
export declare function stringEquals(left: string | null, right: string | null, comparison: StringComparison): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* C#'s `string.Compare(a, b, comparison)` and the overloads that imply a comparison:
|
|
37
|
+
* `Compare(a, b)`, `Compare(a, b, ignoreCase)`, `CompareOrdinal(a, b)`, `a.CompareTo(b)`.
|
|
38
|
+
*
|
|
39
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
40
|
+
*/
|
|
41
|
+
export declare function stringCompare(left: string | null, right: string | null, comparison: StringComparison): number;
|
|
42
|
+
/**
|
|
43
|
+
* C#'s `a.Equals(b, comparison)` and `a.Equals(b)` (ordinal).
|
|
44
|
+
*
|
|
45
|
+
* @throws NullReferenceException when `value` is `null`.
|
|
46
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
47
|
+
*/
|
|
48
|
+
export declare function stringInstanceEquals(value: string | null, other: unknown, comparison: StringComparison): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* C#'s `a.CompareTo(b)` for a `string` or `object` argument: a culture-sensitive
|
|
51
|
+
* comparison; `null` sorts first.
|
|
52
|
+
*
|
|
53
|
+
* @throws NullReferenceException when `value` is `null`.
|
|
54
|
+
* @throws ArgumentException when `other` is neither `null` nor a string.
|
|
55
|
+
*/
|
|
56
|
+
export declare function stringCompareTo(value: string | null, other: unknown): number;
|
|
@@ -0,0 +1,225 @@
|
|
|
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
|
+
import { ArgumentException } from "./exceptions/argument-exception.js";
|
|
29
|
+
import { NullReferenceException } from "./exceptions/null-reference-exception.js";
|
|
30
|
+
import { ordinalUppercaseOf } from "./json/ordinal-ignore-case.js";
|
|
31
|
+
import { StringComparison } from "./string-members.js";
|
|
32
|
+
const UNSUPPORTED_COMPARISON_MESSAGE = "The string comparison type passed in is currently not supported.";
|
|
33
|
+
const NOT_A_STRING_MESSAGE = "Object must be of type String.";
|
|
34
|
+
const FIRST_SUPPLEMENTARY_CODE_POINT = 0x10000;
|
|
35
|
+
/* Built on first use, one per culture member, so importing the module runs no code. */
|
|
36
|
+
const cultureCollators = [];
|
|
37
|
+
/** The collator for a culture member (`CurrentCulture` to `InvariantCultureIgnoreCase`). */
|
|
38
|
+
function collatorFor(comparison) {
|
|
39
|
+
const isInvariant = comparison >= StringComparison.InvariantCulture;
|
|
40
|
+
const ignoresCase = comparison % 2 === 1;
|
|
41
|
+
return (cultureCollators[comparison] ??= new Intl.Collator(isInvariant ? "und" : undefined, {
|
|
42
|
+
sensitivity: ignoresCase ? "accent" : "variant",
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
/** @throws ArgumentException when `comparison` is not a `StringComparison` member. */
|
|
46
|
+
function requireComparison(comparison) {
|
|
47
|
+
const isMember = Number.isInteger(comparison) &&
|
|
48
|
+
comparison >= StringComparison.CurrentCulture &&
|
|
49
|
+
comparison <= StringComparison.OrdinalIgnoreCase;
|
|
50
|
+
if (!isMember) {
|
|
51
|
+
throw new ArgumentException(UNSUPPORTED_COMPARISON_MESSAGE, "comparisonType");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The ordinal uppercase code point of the code point at `index` in `value`. */
|
|
55
|
+
function foldedCodePointAt(value, index) {
|
|
56
|
+
const unit = value.charCodeAt(index);
|
|
57
|
+
if (unit < 0x80) {
|
|
58
|
+
return unit >= 0x61 && unit <= 0x7a ? unit - 0x20 : unit;
|
|
59
|
+
}
|
|
60
|
+
const codePoint = value.codePointAt(index);
|
|
61
|
+
return ordinalUppercaseOf(String.fromCodePoint(codePoint)).codePointAt(0);
|
|
62
|
+
}
|
|
63
|
+
function compareOrdinal(left, right) {
|
|
64
|
+
const length = Math.min(left.length, right.length);
|
|
65
|
+
for (let index = 0; index < length; index++) {
|
|
66
|
+
const difference = left.charCodeAt(index) - right.charCodeAt(index);
|
|
67
|
+
if (difference !== 0) {
|
|
68
|
+
return difference;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return left.length - right.length;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Walks both strings code point by code point; a supplementary code point orders by its
|
|
75
|
+
* value (above every BMP one), as .NET's case-insensitive comparison does.
|
|
76
|
+
*/
|
|
77
|
+
function compareOrdinalIgnoreCase(left, right) {
|
|
78
|
+
let leftIndex = 0;
|
|
79
|
+
let rightIndex = 0;
|
|
80
|
+
while (leftIndex < left.length && rightIndex < right.length) {
|
|
81
|
+
const leftFolded = foldedCodePointAt(left, leftIndex);
|
|
82
|
+
const rightFolded = foldedCodePointAt(right, rightIndex);
|
|
83
|
+
if (leftFolded !== rightFolded) {
|
|
84
|
+
const isSupplementary = leftFolded >= FIRST_SUPPLEMENTARY_CODE_POINT ||
|
|
85
|
+
rightFolded >= FIRST_SUPPLEMENTARY_CODE_POINT;
|
|
86
|
+
return isSupplementary ? Math.sign(leftFolded - rightFolded) : leftFolded - rightFolded;
|
|
87
|
+
}
|
|
88
|
+
leftIndex += left.codePointAt(leftIndex) >= FIRST_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
|
|
89
|
+
rightIndex +=
|
|
90
|
+
right.codePointAt(rightIndex) >= FIRST_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
|
|
91
|
+
}
|
|
92
|
+
return left.length - leftIndex - (right.length - rightIndex);
|
|
93
|
+
}
|
|
94
|
+
/** Two non-null strings under `comparison`, already checked. */
|
|
95
|
+
function compareWith(left, right, comparison) {
|
|
96
|
+
switch (comparison) {
|
|
97
|
+
case StringComparison.Ordinal:
|
|
98
|
+
return compareOrdinal(left, right);
|
|
99
|
+
case StringComparison.OrdinalIgnoreCase:
|
|
100
|
+
return compareOrdinalIgnoreCase(left, right);
|
|
101
|
+
case StringComparison.CurrentCultureIgnoreCase:
|
|
102
|
+
case StringComparison.InvariantCultureIgnoreCase:
|
|
103
|
+
return compareCultureIgnoringCase(left, right, comparison);
|
|
104
|
+
default:
|
|
105
|
+
return collatorFor(comparison).compare(left, right);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* .NET's culture `IgnoreCase` on ICU ignores case but not width (`A` / `a`, `カ` / `カ`)
|
|
110
|
+
* or kana type (`あ` / `ア`), which `accent` sensitivity ignores too. Strings that differ
|
|
111
|
+
* at `accent` sensitivity keep that order; strings equal there compare their
|
|
112
|
+
* case-insensitive keys at `variant` sensitivity, where only width and kana remain.
|
|
113
|
+
*/
|
|
114
|
+
function compareCultureIgnoringCase(left, right, comparison) {
|
|
115
|
+
const primary = collatorFor(comparison).compare(left, right);
|
|
116
|
+
if (primary !== 0) {
|
|
117
|
+
return primary;
|
|
118
|
+
}
|
|
119
|
+
const caseSensitive = (comparison - 1);
|
|
120
|
+
return collatorFor(caseSensitive).compare(caseInsensitiveKey(left), caseInsensitiveKey(right));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* `value` with every difference .NET's culture `IgnoreCase` ignores removed: each code
|
|
124
|
+
* point outside the halfwidth and fullwidth forms is replaced by its compatibility
|
|
125
|
+
* decomposition (`ff` → `ff`, `①` → `1`, U+3000 → space) in a single case (simple
|
|
126
|
+
* uppercase, then simple lowercase: `ς` and `Σ` → `σ`, `ẞ` → `ß`). A width form keeps
|
|
127
|
+
* its width and only folds its case (`A` → `a`).
|
|
128
|
+
*/
|
|
129
|
+
function caseInsensitiveKey(value) {
|
|
130
|
+
let key = "";
|
|
131
|
+
for (const character of value) {
|
|
132
|
+
if (isWidthForm(character.codePointAt(0))) {
|
|
133
|
+
key += singleCase(character);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
for (const part of character.normalize("NFKC")) {
|
|
137
|
+
key += singleCase(part);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return key;
|
|
141
|
+
}
|
|
142
|
+
/** The Halfwidth and Fullwidth Forms block. */
|
|
143
|
+
function isWidthForm(codePoint) {
|
|
144
|
+
return codePoint >= 0xff00 && codePoint <= 0xffef;
|
|
145
|
+
}
|
|
146
|
+
function singleCase(character) {
|
|
147
|
+
const upper = simpleMapping(character, character.toUpperCase());
|
|
148
|
+
return simpleMapping(upper, upper.toLowerCase());
|
|
149
|
+
}
|
|
150
|
+
/** `mapped` when it is one code point (the simple mapping), else `character`. */
|
|
151
|
+
function simpleMapping(character, mapped) {
|
|
152
|
+
return mapped.length === character.length || [...mapped].length === 1 ? mapped : character;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* C#'s `string.Equals(a, b, comparison)` and `a.Equals(b, comparison)`.
|
|
156
|
+
*
|
|
157
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
158
|
+
*/
|
|
159
|
+
export function stringEquals(left, right, comparison) {
|
|
160
|
+
requireComparison(comparison);
|
|
161
|
+
if (left === right) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
if (left === null || right === null || comparison === StringComparison.Ordinal) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
if (comparison === StringComparison.OrdinalIgnoreCase && left.length !== right.length) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
return compareWith(left, right, comparison) === 0;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* C#'s `string.Compare(a, b, comparison)` and the overloads that imply a comparison:
|
|
174
|
+
* `Compare(a, b)`, `Compare(a, b, ignoreCase)`, `CompareOrdinal(a, b)`, `a.CompareTo(b)`.
|
|
175
|
+
*
|
|
176
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
177
|
+
*/
|
|
178
|
+
export function stringCompare(left, right, comparison) {
|
|
179
|
+
requireComparison(comparison);
|
|
180
|
+
if (left === right) {
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
if (left === null) {
|
|
184
|
+
return -1;
|
|
185
|
+
}
|
|
186
|
+
if (right === null) {
|
|
187
|
+
return 1;
|
|
188
|
+
}
|
|
189
|
+
return compareWith(left, right, comparison);
|
|
190
|
+
}
|
|
191
|
+
/** @throws NullReferenceException when the receiver of an instance call is `null`. */
|
|
192
|
+
function requireReceiver(value) {
|
|
193
|
+
if (value == null) {
|
|
194
|
+
throw new NullReferenceException();
|
|
195
|
+
}
|
|
196
|
+
return value;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* C#'s `a.Equals(b, comparison)` and `a.Equals(b)` (ordinal).
|
|
200
|
+
*
|
|
201
|
+
* @throws NullReferenceException when `value` is `null`.
|
|
202
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
203
|
+
*/
|
|
204
|
+
export function stringInstanceEquals(value, other, comparison) {
|
|
205
|
+
const receiver = requireReceiver(value);
|
|
206
|
+
if (other === null || other === undefined || typeof other === "string") {
|
|
207
|
+
return stringEquals(receiver, other ?? null, comparison);
|
|
208
|
+
}
|
|
209
|
+
requireComparison(comparison);
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* C#'s `a.CompareTo(b)` for a `string` or `object` argument: a culture-sensitive
|
|
214
|
+
* comparison; `null` sorts first.
|
|
215
|
+
*
|
|
216
|
+
* @throws NullReferenceException when `value` is `null`.
|
|
217
|
+
* @throws ArgumentException when `other` is neither `null` nor a string.
|
|
218
|
+
*/
|
|
219
|
+
export function stringCompareTo(value, other) {
|
|
220
|
+
const receiver = requireReceiver(value);
|
|
221
|
+
if (other !== null && other !== undefined && typeof other !== "string") {
|
|
222
|
+
throw new ArgumentException(NOT_A_STRING_MESSAGE);
|
|
223
|
+
}
|
|
224
|
+
return stringCompare(receiver, other ?? null, StringComparison.CurrentCulture);
|
|
225
|
+
}
|
|
@@ -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
|
-
*
|
|
12
|
-
* their ordinal counterparts (JavaScript has no culture-aware
|
|
13
|
-
* case-insensitive
|
|
14
|
-
*
|
|
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
|
-
*
|
|
12
|
-
* their ordinal counterparts (JavaScript has no culture-aware
|
|
13
|
-
* case-insensitive
|
|
14
|
-
*
|
|
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
|
|
53
|
-
*
|
|
54
|
-
*
|
|
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 (
|
|
59
|
-
const
|
|
60
|
-
|
|
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
|
}
|