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,12 @@
1
+ /**
2
+ * C#'s `IComparable<T>.CompareTo` / `IComparable.CompareTo` called through the interface
3
+ * or a type parameter (`((IComparable<string>)s).CompareTo(t)`, `a.CompareTo(b)` with
4
+ * `T : IComparable<T>`): the receiver may be a string or a number, which have no
5
+ * `compareTo` method in JavaScript (#769). A string compares culture-sensitively, as
6
+ * `string.CompareTo` does; any other value goes through `Comparer<T>.Default`'s order
7
+ * (`defaultCompare`), which calls the receiver's own `compareTo` for an object. A `null`
8
+ * `other` answers `1` without calling it, as the `IComparable` contract requires: a user
9
+ * `CompareTo(null)` that returns anything else is not reproduced.
10
+ */
11
+ /** @throws NullReferenceException when `value` is `null`. */
12
+ export declare function comparableCompareTo(value: unknown, other: unknown): number;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * C#'s `IComparable<T>.CompareTo` / `IComparable.CompareTo` called through the interface
3
+ * or a type parameter (`((IComparable<string>)s).CompareTo(t)`, `a.CompareTo(b)` with
4
+ * `T : IComparable<T>`): the receiver may be a string or a number, which have no
5
+ * `compareTo` method in JavaScript (#769). A string compares culture-sensitively, as
6
+ * `string.CompareTo` does; any other value goes through `Comparer<T>.Default`'s order
7
+ * (`defaultCompare`), which calls the receiver's own `compareTo` for an object. A `null`
8
+ * `other` answers `1` without calling it, as the `IComparable` contract requires: a user
9
+ * `CompareTo(null)` that returns anything else is not reproduced.
10
+ */
11
+ import { defaultCompare } from "./collections/default-comparer.js";
12
+ import { NullReferenceException } from "./exceptions/null-reference-exception.js";
13
+ import { stringCompareTo } from "./string-comparison.js";
14
+ /** @throws NullReferenceException when `value` is `null`. */
15
+ export function comparableCompareTo(value, other) {
16
+ if (value === null || value === undefined) {
17
+ throw new NullReferenceException();
18
+ }
19
+ return typeof value === "string" ? stringCompareTo(value, other) : defaultCompare(value, other);
20
+ }
@@ -0,0 +1,15 @@
1
+ import type { InterfaceType } from "./reflection/runtime-type.ts";
2
+ /**
3
+ * C# `System.IDisposable` (spec FR-111): `Dispose()` lowers to `dispose()`, and a C# `using`
4
+ * lowers to `try`/`finally` around the resource. There is no `Symbol.dispose` alias: stable
5
+ * Safari has none, and `using` disposal wraps errors in `SuppressedError`, unlike .NET.
6
+ */
7
+ export interface IDisposable {
8
+ dispose(): void;
9
+ }
10
+ /**
11
+ * The descriptor of {@link IDisposable}. The test is nominal, as in .NET: a value is
12
+ * `IDisposable` only when its class descriptor lists the interface, never because it has a
13
+ * `dispose` method.
14
+ */
15
+ export declare const IDisposable$runtimeType: InterfaceType<IDisposable>;
@@ -0,0 +1,15 @@
1
+ import { implementsInterface } from "./reflection/implements-interface.js";
2
+ /**
3
+ * The descriptor of {@link IDisposable}. The test is nominal, as in .NET: a value is
4
+ * `IDisposable` only when its class descriptor lists the interface, never because it has a
5
+ * `dispose` method.
6
+ */
7
+ export const IDisposable$runtimeType = {
8
+ kind: "interface",
9
+ name: "IDisposable",
10
+ fullName: "System.IDisposable",
11
+ namespace: "System",
12
+ baseInterfaces: [],
13
+ plainImplementers: [],
14
+ test: (value) => implementsInterface(value, IDisposable$runtimeType),
15
+ };
@@ -21,7 +21,8 @@ export declare class HashCode {
21
21
  constructor();
22
22
  add(value: unknown): void;
23
23
  toHashCode(): number;
24
- static combine(v1: unknown): number;
24
+ /** `HashCode.Combine(v1 … v8)` (#749): the same accumulation as {@link add} per value. */
25
+ static combine(...values: unknown[]): number;
25
26
  static combine2(v1: unknown, v2: unknown): number;
26
27
  static combine3(v1: unknown, v2: unknown, v3: unknown): number;
27
28
  static combine4(v1: unknown, v2: unknown, v3: unknown, v4: unknown): number;
@@ -150,9 +150,12 @@ export class HashCode {
150
150
  return hash;
151
151
  }
152
152
  // ─── Static combine helpers ───────────────────────────────
153
- static combine(v1) {
153
+ /** `HashCode.Combine(v1 … v8)` (#749): the same accumulation as {@link add} per value. */
154
+ static combine(...values) {
154
155
  const hc = new HashCode();
155
- hc.add(v1);
156
+ for (const value of values) {
157
+ hc.add(value);
158
+ }
156
159
  return hc.toHashCode();
157
160
  }
158
161
  static combine2(v1, v2) {
@@ -28,6 +28,9 @@ export * from "./math.ts";
28
28
  export * from "./number-parsing.ts";
29
29
  export * from "./strings.ts";
30
30
  export * from "./string-members.ts";
31
+ export * from "./string-comparison.ts";
32
+ export * from "./string-hash-code.ts";
33
+ export * from "./comparable-compare-to.ts";
31
34
  export * from "./decimal-parsing.ts";
32
35
  export * from "./array-length.ts";
33
36
  export * from "./argument-guards.ts";
@@ -38,3 +41,4 @@ export * from "./char-members.ts";
38
41
  export * from "./string-builder.ts";
39
42
  export * from "./lazy.ts";
40
43
  export * from "./task-completion-source.ts";
44
+ export * from "./disposable.ts";
@@ -28,6 +28,9 @@ export * from "./math.js";
28
28
  export * from "./number-parsing.js";
29
29
  export * from "./strings.js";
30
30
  export * from "./string-members.js";
31
+ export * from "./string-comparison.js";
32
+ export * from "./string-hash-code.js";
33
+ export * from "./comparable-compare-to.js";
31
34
  export * from "./decimal-parsing.js";
32
35
  export * from "./array-length.js";
33
36
  export * from "./argument-guards.js";
@@ -38,3 +41,4 @@ export * from "./char-members.js";
38
41
  export * from "./string-builder.js";
39
42
  export * from "./lazy.js";
40
43
  export * from "./task-completion-source.js";
44
+ export * from "./disposable.js";
@@ -6,6 +6,7 @@
6
6
  * every 64-bit overload here.
7
7
  */
8
8
  import { ArgumentException, OverflowException } from "./exceptions/index.js";
9
+ import { throwMinGreaterThanMax } from "./min-max.js";
9
10
  import { divInt64, divUInt64, INT64_MIN, remInt64, remUInt64 } from "./int64.js";
10
11
  /** .NET's message for `Math.Abs(long.MinValue)`. */
11
12
  const NEGATE_MIN_VALUE_MESSAGE = "Negating the minimum value of a twos complement number is invalid.";
@@ -103,7 +104,7 @@ export function compareInt64(left, right) {
103
104
  }
104
105
  function clamp(value, min, max) {
105
106
  if (min > max) {
106
- throw new ArgumentException(`'${min}' cannot be greater than ${max}.`);
107
+ throwMinGreaterThanMax(min, max);
107
108
  }
108
109
  return maxInt64(min, minInt64(value, max));
109
110
  }
@@ -1,4 +1,4 @@
1
- export type { TypeDescriptor, PrimitiveDescriptor, Int64Descriptor, RefDescriptor, TemporalDescriptor, DecimalDescriptor, BytesDescriptor, ArrayDescriptor, KeyedCollectionDescriptor, SequenceCollectionDescriptor, BrandedDescriptor, EnumDescriptor, NumericEnumDescriptor, NullableDescriptor, PropertySpec, TypeSpec, ValueSpec, JsonTypeInfo, TypeDiscriminator, UnknownDerivedTypeHandling, DerivedTypeSpec, PolymorphismSpec, JsonReadOptions, NumberTypeName, JsonConverter, SerializerContextOptions, } from "./types.ts";
1
+ export type { TypeDescriptor, PrimitiveDescriptor, Int64Descriptor, RefDescriptor, TemporalDescriptor, DecimalDescriptor, BytesDescriptor, ArrayDescriptor, KeyedCollectionDescriptor, SequenceCollectionDescriptor, BrandedDescriptor, EnumDescriptor, NumericEnumDescriptor, NullableDescriptor, PropertySpec, TypeSpec, ValueSpec, JsonTypeInfo, TypeDiscriminator, UnknownDerivedTypeHandling, DerivedTypeSpec, PolymorphismSpec, JsonReadOptions, JsonWriteOptions, NumberTypeName, JsonConverter, SerializerContextOptions, } from "./types.ts";
2
2
  export { SerializerContext } from "./serializer-context.ts";
3
3
  export { JsonSerializer, BoundSerializer } from "./json-serializer.ts";
4
4
  export { PropertyNamingPolicy } from "./property-naming-policy.ts";
@@ -60,11 +60,12 @@ function deserializeTextWithInfo(text, spec, converters) {
60
60
  /**
61
61
  * Writes JSON text as System.Text.Json's `Serialize(value, JsonTypeInfo<T>)`
62
62
  * does (#741): the tree `serialize` builds, written by `writeJsonText`
63
- * (STJ's escaping and number text, not `JSON.stringify`'s).
63
+ * (STJ's escaping and number text, not `JSON.stringify`'s), laid out as the spec's
64
+ * `writeOptions` say (`WriteIndented` and friends, #752).
64
65
  */
65
66
  function serializeTextWithInfo(value, spec, converters) {
66
67
  throwIfNull(spec, "jsonTypeInfo");
67
- return writeJsonText(serializeWithInfo(value, spec, converters));
68
+ return writeJsonText(serializeWithInfo(value, spec, converters), spec.writeOptions);
68
69
  }
69
70
  /**
70
71
  * A bound serializer that uses a SerializerContext to resolve custom converters
@@ -3,7 +3,10 @@
3
3
  * System.Text.Json's `JsonSerializer.Serialize(value, JsonTypeInfo<T>)`
4
4
  * writes with its default options (#741, measured on .NET 10):
5
5
  *
6
- * - no whitespace;
6
+ * - no whitespace, unless the context writes indented text (#752): `writeIndented` puts each
7
+ * array element and each property on its own line, indented by `indentSize` copies of
8
+ * `indentCharacter` per level (a space and 2 by default), with `newLine` (`"\n"` by default)
9
+ * between lines, `": "` after a property name, and `[]` / `{}` for an empty container;
7
10
  * - strings escaped by `JavaScriptEncoder.Default`: `\\`, `\b`, `\f`, `\n`,
8
11
  * `\r` and `\t` keep their short escapes; every other control character,
9
12
  * `"`, `&`, `'`, `+`, `<`, `>`, `` ` `` and every character outside
@@ -13,5 +16,9 @@
13
16
  * a `JSON.rawJSON` leaf (an exact `decimal` / 64-bit integer) as its text;
14
17
  * NaN and the infinities throw `ArgumentException`, as STJ throws for them.
15
18
  */
16
- /** The JSON text of `tree`, as System.Text.Json writes it. */
17
- export declare function writeJsonText(tree: unknown): string;
19
+ import type { JsonWriteOptions } from "./types.ts";
20
+ /**
21
+ * The JSON text of `tree`, as System.Text.Json writes it: compact, or laid out as
22
+ * `options` say (the context's `WriteIndented` family, #752).
23
+ */
24
+ export declare function writeJsonText(tree: unknown, options?: JsonWriteOptions): string;
@@ -3,7 +3,10 @@
3
3
  * System.Text.Json's `JsonSerializer.Serialize(value, JsonTypeInfo<T>)`
4
4
  * writes with its default options (#741, measured on .NET 10):
5
5
  *
6
- * - no whitespace;
6
+ * - no whitespace, unless the context writes indented text (#752): `writeIndented` puts each
7
+ * array element and each property on its own line, indented by `indentSize` copies of
8
+ * `indentCharacter` per level (a space and 2 by default), with `newLine` (`"\n"` by default)
9
+ * between lines, `": "` after a property name, and `[]` / `{}` for an empty container;
7
10
  * - strings escaped by `JavaScriptEncoder.Default`: `\\`, `\b`, `\f`, `\n`,
8
11
  * `\r` and `\t` keep their short escapes; every other control character,
9
12
  * `"`, `&`, `'`, `+`, `<`, `>`, `` ` `` and every character outside
@@ -21,13 +24,31 @@ const NON_FINITE_NUMBER_MESSAGE = ".NET number values such as positive and negat
21
24
  "To make it work when using 'JsonSerializer', consider specifying " +
22
25
  "'JsonNumberHandling.AllowNamedFloatingPointLiterals' " +
23
26
  "(see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).";
24
- /** The JSON text of `tree`, as System.Text.Json writes it. */
25
- export function writeJsonText(tree) {
27
+ const COMPACT = {
28
+ lineStart: () => "",
29
+ lineEnd: () => "",
30
+ afterColon: "",
31
+ };
32
+ function indentedLayout(options) {
33
+ const unit = (options.indentCharacter ?? " ").repeat(options.indentSize ?? 2);
34
+ const newLine = options.newLine ?? "\n";
35
+ return {
36
+ lineStart: (depth) => newLine + unit.repeat(depth),
37
+ lineEnd: (depth) => newLine + unit.repeat(depth - 1),
38
+ afterColon: " ",
39
+ };
40
+ }
41
+ /**
42
+ * The JSON text of `tree`, as System.Text.Json writes it: compact, or laid out as
43
+ * `options` say (the context's `WriteIndented` family, #752).
44
+ */
45
+ export function writeJsonText(tree, options) {
26
46
  const parts = [];
27
- writeValue(tree, parts);
47
+ const layout = options?.writeIndented ? indentedLayout(options) : COMPACT;
48
+ writeValue(tree, parts, layout, 0);
28
49
  return parts.join("");
29
50
  }
30
- function writeValue(value, parts) {
51
+ function writeValue(value, parts, layout, depth) {
31
52
  if (value == null) {
32
53
  parts.push("null");
33
54
  return;
@@ -44,9 +65,9 @@ function writeValue(value, parts) {
44
65
  parts.push(String(value));
45
66
  return;
46
67
  }
47
- writeObject(value, parts);
68
+ writeObject(value, parts, layout, depth);
48
69
  }
49
- function writeObject(value, parts) {
70
+ function writeObject(value, parts, layout, depth) {
50
71
  const rawText = rawJsonTextOf(value);
51
72
  if (rawText !== undefined) {
52
73
  parts.push(rawText);
@@ -54,27 +75,35 @@ function writeObject(value, parts) {
54
75
  }
55
76
  const toJson = value.toJSON;
56
77
  if (typeof toJson === "function") {
57
- writeValue(toJson.call(value), parts);
78
+ writeValue(toJson.call(value), parts, layout, depth);
58
79
  return;
59
80
  }
60
81
  if (Array.isArray(value)) {
61
- writeArray(value, parts);
82
+ writeArray(value, parts, layout, depth + 1);
62
83
  return;
63
84
  }
64
- writeProperties(value, parts);
85
+ writeProperties(value, parts, layout, depth + 1);
65
86
  }
66
- function writeArray(items, parts) {
87
+ /** `depth` is the nesting level of the array's elements. */
88
+ function writeArray(items, parts, layout, depth) {
67
89
  parts.push("[");
68
90
  items.forEach((item, index) => {
69
91
  if (index > 0) {
70
92
  parts.push(",");
71
93
  }
72
- writeValue(item, parts);
94
+ parts.push(layout.lineStart(depth));
95
+ writeValue(item, parts, layout, depth);
73
96
  });
97
+ if (items.length > 0) {
98
+ parts.push(layout.lineEnd(depth));
99
+ }
74
100
  parts.push("]");
75
101
  }
76
- /** An object's own enumerable properties; an `undefined` one is skipped, as `JSON.stringify` skips it. */
77
- function writeProperties(object, parts) {
102
+ /**
103
+ * An object's own enumerable properties; an `undefined` one is skipped, as `JSON.stringify`
104
+ * skips it. `depth` is the nesting level of the properties.
105
+ */
106
+ function writeProperties(object, parts, layout, depth) {
78
107
  let first = true;
79
108
  parts.push("{");
80
109
  for (const key of Object.keys(object)) {
@@ -86,8 +115,11 @@ function writeProperties(object, parts) {
86
115
  parts.push(",");
87
116
  }
88
117
  first = false;
89
- parts.push(quoted(key), ":");
90
- writeValue(member, parts);
118
+ parts.push(layout.lineStart(depth), quoted(key), ":", layout.afterColon);
119
+ writeValue(member, parts, layout, depth);
120
+ }
121
+ if (!first) {
122
+ parts.push(layout.lineEnd(depth));
91
123
  }
92
124
  parts.push("}");
93
125
  }
@@ -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 declares
50
- * itself. A spec stays as given when neither has one, so default contexts
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 withReadOptions;
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
- function hasNoOptions(readOptions) {
4
- return Object.keys(readOptions).length === 0;
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.withReadOptions(spec);
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.withReadOptions(spec);
77
+ return this.withContextOptions(spec);
68
78
  }
69
79
  /**
70
- * `spec` with the context's read options, under any the spec declares
71
- * itself. A spec stays as given when neither has one, so default contexts
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
- withReadOptions(spec) {
84
+ withContextOptions(spec) {
75
85
  const readOptions = { ...this.readOptions, ...spec.readOptions };
76
- return hasNoOptions(readOptions) ? spec : { ...spec, readOptions };
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;