metano-runtime 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/system/comparable-compare-to.d.ts +12 -0
- package/dist/system/comparable-compare-to.js +20 -0
- package/dist/system/hash-code.d.ts +2 -1
- package/dist/system/hash-code.js +5 -2
- package/dist/system/index.d.ts +3 -0
- package/dist/system/index.js +3 -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/hash-code.d.ts +2 -1
- package/dist-unchecked/system/hash-code.js +5 -2
- package/dist-unchecked/system/index.d.ts +3 -0
- package/dist-unchecked/system/index.js +3 -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/hash-code.ts +7 -2
- package/src/system/index.ts +3 -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
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
12
|
+
import { defaultCompare } from "./collections/default-comparer.ts";
|
|
13
|
+
import { NullReferenceException } from "./exceptions/null-reference-exception.ts";
|
|
14
|
+
import { stringCompareTo } from "./string-comparison.ts";
|
|
15
|
+
|
|
16
|
+
/** @throws NullReferenceException when `value` is `null`. */
|
|
17
|
+
export function comparableCompareTo(value: unknown, other: unknown): number {
|
|
18
|
+
if (value === null || value === undefined) {
|
|
19
|
+
throw new NullReferenceException();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return typeof value === "string" ? stringCompareTo(value, other) : defaultCompare(value, other);
|
|
23
|
+
}
|
package/src/system/hash-code.ts
CHANGED
|
@@ -170,9 +170,14 @@ export class HashCode {
|
|
|
170
170
|
|
|
171
171
|
// ─── Static combine helpers ───────────────────────────────
|
|
172
172
|
|
|
173
|
-
|
|
173
|
+
/** `HashCode.Combine(v1 … v8)` (#749): the same accumulation as {@link add} per value. */
|
|
174
|
+
static combine(...values: unknown[]): number {
|
|
174
175
|
const hc = new HashCode();
|
|
175
|
-
|
|
176
|
+
|
|
177
|
+
for (const value of values) {
|
|
178
|
+
hc.add(value);
|
|
179
|
+
}
|
|
180
|
+
|
|
176
181
|
return hc.toHashCode();
|
|
177
182
|
}
|
|
178
183
|
|
package/src/system/index.ts
CHANGED
|
@@ -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";
|
package/src/system/int64-math.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* every 64-bit overload here.
|
|
7
7
|
*/
|
|
8
8
|
import { ArgumentException, OverflowException } from "./exceptions/index.ts";
|
|
9
|
+
import { throwMinGreaterThanMax } from "./min-max.ts";
|
|
9
10
|
import { divInt64, divUInt64, INT64_MIN, remInt64, remUInt64 } from "./int64.ts";
|
|
10
11
|
|
|
11
12
|
/** .NET's message for `Math.Abs(long.MinValue)`. */
|
|
@@ -130,7 +131,7 @@ export function compareInt64(left: bigint, right: bigint): number {
|
|
|
130
131
|
|
|
131
132
|
function clamp(value: bigint, min: bigint, max: bigint): bigint {
|
|
132
133
|
if (min > max) {
|
|
133
|
-
|
|
134
|
+
throwMinGreaterThanMax(min, max);
|
|
134
135
|
}
|
|
135
136
|
|
|
136
137
|
return maxInt64(min, minInt64(value, max));
|
package/src/system/json/index.ts
CHANGED
|
@@ -92,7 +92,8 @@ function deserializeTextWithInfo<T>(
|
|
|
92
92
|
/**
|
|
93
93
|
* Writes JSON text as System.Text.Json's `Serialize(value, JsonTypeInfo<T>)`
|
|
94
94
|
* does (#741): the tree `serialize` builds, written by `writeJsonText`
|
|
95
|
-
* (STJ's escaping and number text, not `JSON.stringify`'s)
|
|
95
|
+
* (STJ's escaping and number text, not `JSON.stringify`'s), laid out as the spec's
|
|
96
|
+
* `writeOptions` say (`WriteIndented` and friends, #752).
|
|
96
97
|
*/
|
|
97
98
|
function serializeTextWithInfo<T>(
|
|
98
99
|
value: T | null,
|
|
@@ -101,7 +102,7 @@ function serializeTextWithInfo<T>(
|
|
|
101
102
|
): string {
|
|
102
103
|
throwIfNull(spec, "jsonTypeInfo");
|
|
103
104
|
|
|
104
|
-
return writeJsonText(serializeWithInfo(value, spec, converters));
|
|
105
|
+
return writeJsonText(serializeWithInfo(value, spec, converters), spec.writeOptions);
|
|
105
106
|
}
|
|
106
107
|
|
|
107
108
|
/**
|
|
@@ -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
|
|
@@ -17,6 +20,7 @@
|
|
|
17
20
|
import { formatDouble } from "../double-format.ts";
|
|
18
21
|
import { ArgumentException } from "../exceptions/argument-exception.ts";
|
|
19
22
|
import { rawJsonTextOf } from "./exact-json-number.ts";
|
|
23
|
+
import type { JsonWriteOptions } from "./types.ts";
|
|
20
24
|
|
|
21
25
|
/** The message STJ throws for a NaN or infinite number written as JSON. */
|
|
22
26
|
const NON_FINITE_NUMBER_MESSAGE =
|
|
@@ -25,16 +29,47 @@ const NON_FINITE_NUMBER_MESSAGE =
|
|
|
25
29
|
"'JsonNumberHandling.AllowNamedFloatingPointLiterals' " +
|
|
26
30
|
"(see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).";
|
|
27
31
|
|
|
28
|
-
/**
|
|
29
|
-
|
|
32
|
+
/** How the writer lays out containers: the compact text, or one line per element. */
|
|
33
|
+
interface Layout {
|
|
34
|
+
/** The text before each element of a container at `depth`, or `""` when compact. */
|
|
35
|
+
lineStart(depth: number): string;
|
|
36
|
+
/** The text between the last element of a container and its closing bracket. */
|
|
37
|
+
lineEnd(depth: number): string;
|
|
38
|
+
/** The text after a property name's colon. */
|
|
39
|
+
readonly afterColon: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const COMPACT: Layout = {
|
|
43
|
+
lineStart: () => "",
|
|
44
|
+
lineEnd: () => "",
|
|
45
|
+
afterColon: "",
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function indentedLayout(options: JsonWriteOptions): Layout {
|
|
49
|
+
const unit = (options.indentCharacter ?? " ").repeat(options.indentSize ?? 2);
|
|
50
|
+
const newLine = options.newLine ?? "\n";
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
lineStart: (depth) => newLine + unit.repeat(depth),
|
|
54
|
+
lineEnd: (depth) => newLine + unit.repeat(depth - 1),
|
|
55
|
+
afterColon: " ",
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The JSON text of `tree`, as System.Text.Json writes it: compact, or laid out as
|
|
61
|
+
* `options` say (the context's `WriteIndented` family, #752).
|
|
62
|
+
*/
|
|
63
|
+
export function writeJsonText(tree: unknown, options?: JsonWriteOptions): string {
|
|
30
64
|
const parts: string[] = [];
|
|
65
|
+
const layout = options?.writeIndented ? indentedLayout(options) : COMPACT;
|
|
31
66
|
|
|
32
|
-
writeValue(tree, parts);
|
|
67
|
+
writeValue(tree, parts, layout, 0);
|
|
33
68
|
|
|
34
69
|
return parts.join("");
|
|
35
70
|
}
|
|
36
71
|
|
|
37
|
-
function writeValue(value: unknown, parts: string[]): void {
|
|
72
|
+
function writeValue(value: unknown, parts: string[], layout: Layout, depth: number): void {
|
|
38
73
|
if (value == null) {
|
|
39
74
|
parts.push("null");
|
|
40
75
|
|
|
@@ -57,10 +92,10 @@ function writeValue(value: unknown, parts: string[]): void {
|
|
|
57
92
|
return;
|
|
58
93
|
}
|
|
59
94
|
|
|
60
|
-
writeObject(value as object, parts);
|
|
95
|
+
writeObject(value as object, parts, layout, depth);
|
|
61
96
|
}
|
|
62
97
|
|
|
63
|
-
function writeObject(value: object, parts: string[]): void {
|
|
98
|
+
function writeObject(value: object, parts: string[], layout: Layout, depth: number): void {
|
|
64
99
|
const rawText = rawJsonTextOf(value);
|
|
65
100
|
|
|
66
101
|
if (rawText !== undefined) {
|
|
@@ -72,21 +107,27 @@ function writeObject(value: object, parts: string[]): void {
|
|
|
72
107
|
const toJson = (value as { toJSON?: unknown }).toJSON;
|
|
73
108
|
|
|
74
109
|
if (typeof toJson === "function") {
|
|
75
|
-
writeValue(toJson.call(value), parts);
|
|
110
|
+
writeValue(toJson.call(value), parts, layout, depth);
|
|
76
111
|
|
|
77
112
|
return;
|
|
78
113
|
}
|
|
79
114
|
|
|
80
115
|
if (Array.isArray(value)) {
|
|
81
|
-
writeArray(value, parts);
|
|
116
|
+
writeArray(value, parts, layout, depth + 1);
|
|
82
117
|
|
|
83
118
|
return;
|
|
84
119
|
}
|
|
85
120
|
|
|
86
|
-
writeProperties(value as Record<string, unknown>, parts);
|
|
121
|
+
writeProperties(value as Record<string, unknown>, parts, layout, depth + 1);
|
|
87
122
|
}
|
|
88
123
|
|
|
89
|
-
|
|
124
|
+
/** `depth` is the nesting level of the array's elements. */
|
|
125
|
+
function writeArray(
|
|
126
|
+
items: readonly unknown[],
|
|
127
|
+
parts: string[],
|
|
128
|
+
layout: Layout,
|
|
129
|
+
depth: number,
|
|
130
|
+
): void {
|
|
90
131
|
parts.push("[");
|
|
91
132
|
|
|
92
133
|
items.forEach((item, index) => {
|
|
@@ -94,14 +135,27 @@ function writeArray(items: readonly unknown[], parts: string[]): void {
|
|
|
94
135
|
parts.push(",");
|
|
95
136
|
}
|
|
96
137
|
|
|
97
|
-
|
|
138
|
+
parts.push(layout.lineStart(depth));
|
|
139
|
+
writeValue(item, parts, layout, depth);
|
|
98
140
|
});
|
|
99
141
|
|
|
142
|
+
if (items.length > 0) {
|
|
143
|
+
parts.push(layout.lineEnd(depth));
|
|
144
|
+
}
|
|
145
|
+
|
|
100
146
|
parts.push("]");
|
|
101
147
|
}
|
|
102
148
|
|
|
103
|
-
/**
|
|
104
|
-
|
|
149
|
+
/**
|
|
150
|
+
* An object's own enumerable properties; an `undefined` one is skipped, as `JSON.stringify`
|
|
151
|
+
* skips it. `depth` is the nesting level of the properties.
|
|
152
|
+
*/
|
|
153
|
+
function writeProperties(
|
|
154
|
+
object: Record<string, unknown>,
|
|
155
|
+
parts: string[],
|
|
156
|
+
layout: Layout,
|
|
157
|
+
depth: number,
|
|
158
|
+
): void {
|
|
105
159
|
let first = true;
|
|
106
160
|
|
|
107
161
|
parts.push("{");
|
|
@@ -118,8 +172,12 @@ function writeProperties(object: Record<string, unknown>, parts: string[]): void
|
|
|
118
172
|
}
|
|
119
173
|
|
|
120
174
|
first = false;
|
|
121
|
-
parts.push(quoted(key), ":");
|
|
122
|
-
writeValue(member, parts);
|
|
175
|
+
parts.push(layout.lineStart(depth), quoted(key), ":", layout.afterColon);
|
|
176
|
+
writeValue(member, parts, layout, depth);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (!first) {
|
|
180
|
+
parts.push(layout.lineEnd(depth));
|
|
123
181
|
}
|
|
124
182
|
|
|
125
183
|
parts.push("}");
|
|
@@ -57,7 +57,7 @@ export function ordinalIgnoreCaseKeyOf(name: string): string {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/** The ordinal uppercase of one code point (`character` may be a surrogate pair). */
|
|
60
|
-
function ordinalUppercaseOf(character: string): string {
|
|
60
|
+
export function ordinalUppercaseOf(character: string): string {
|
|
61
61
|
const exception = ORDINAL_UPPERCASE_EXCEPTIONS().get(character);
|
|
62
62
|
|
|
63
63
|
if (exception !== undefined) {
|
|
@@ -4,13 +4,17 @@ import type {
|
|
|
4
4
|
SerializerContextOptions,
|
|
5
5
|
JsonConverter,
|
|
6
6
|
JsonReadOptions,
|
|
7
|
+
JsonWriteOptions,
|
|
7
8
|
} from "./types.ts";
|
|
8
9
|
|
|
9
10
|
/** System.Text.Json's default read behavior: no option set. */
|
|
10
11
|
const DEFAULT_READ_OPTIONS: JsonReadOptions = /* @__PURE__ */ Object.freeze({});
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
/** System.Text.Json's default text formatting: compact text. */
|
|
14
|
+
const DEFAULT_WRITE_OPTIONS: JsonWriteOptions = /* @__PURE__ */ Object.freeze({});
|
|
15
|
+
|
|
16
|
+
function hasNoOptions(options: JsonReadOptions | JsonWriteOptions): boolean {
|
|
17
|
+
return Object.keys(options).length === 0;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
20
|
/**
|
|
@@ -42,9 +46,18 @@ export abstract class SerializerContext {
|
|
|
42
46
|
return DEFAULT_READ_OPTIONS;
|
|
43
47
|
}
|
|
44
48
|
|
|
49
|
+
/**
|
|
50
|
+
* The text formatting of every spec this context creates, mirrored from the
|
|
51
|
+
* C# context's `[JsonSourceGenerationOptions]` (`WriteIndented` and friends,
|
|
52
|
+
* #752). Generated contexts override it only for an indented context.
|
|
53
|
+
*/
|
|
54
|
+
protected get writeOptions(): JsonWriteOptions {
|
|
55
|
+
return DEFAULT_WRITE_OPTIONS;
|
|
56
|
+
}
|
|
57
|
+
|
|
45
58
|
/**
|
|
46
59
|
* Registers a spec in the internal registry and returns it, with the
|
|
47
|
-
* context's read options attached. Intended to be called from lazy getters
|
|
60
|
+
* context's read and write options attached. Intended to be called from lazy getters
|
|
48
61
|
* in generated subclasses:
|
|
49
62
|
*
|
|
50
63
|
* ```ts
|
|
@@ -54,7 +67,7 @@ export abstract class SerializerContext {
|
|
|
54
67
|
* ```
|
|
55
68
|
*/
|
|
56
69
|
protected createSpec<T>(spec: TypeSpec<T>): TypeSpec<T> {
|
|
57
|
-
const attached = this.
|
|
70
|
+
const attached = this.withContextOptions(spec);
|
|
58
71
|
|
|
59
72
|
// Only register class-backed types (those with a constructor reference).
|
|
60
73
|
// [PlainObject] specs have no `type` field — they're accessed via their
|
|
@@ -78,21 +91,25 @@ export abstract class SerializerContext {
|
|
|
78
91
|
* ```
|
|
79
92
|
*
|
|
80
93
|
* Nothing is registered: a value spec has no constructor to resolve. The
|
|
81
|
-
* context's read options are attached, as by `createSpec`.
|
|
94
|
+
* context's read and write options are attached, as by `createSpec`.
|
|
82
95
|
*/
|
|
83
96
|
protected createValueSpec<T>(spec: ValueSpec<T>): ValueSpec<T> {
|
|
84
|
-
return this.
|
|
97
|
+
return this.withContextOptions(spec);
|
|
85
98
|
}
|
|
86
99
|
|
|
87
100
|
/**
|
|
88
|
-
* `spec` with the context's read options, under any the spec
|
|
89
|
-
* itself. A spec stays as given when neither has one, so default
|
|
90
|
-
* keep their specs unchanged.
|
|
101
|
+
* `spec` with the context's read and write options, under any the spec
|
|
102
|
+
* declares itself. A spec stays as given when neither has one, so default
|
|
103
|
+
* contexts keep their specs unchanged.
|
|
91
104
|
*/
|
|
92
|
-
private
|
|
105
|
+
private withContextOptions<
|
|
106
|
+
S extends { readonly readOptions?: JsonReadOptions; readonly writeOptions?: JsonWriteOptions },
|
|
107
|
+
>(spec: S): S {
|
|
93
108
|
const readOptions = { ...this.readOptions, ...spec.readOptions };
|
|
109
|
+
const writeOptions = { ...this.writeOptions, ...spec.writeOptions };
|
|
110
|
+
const withReads = hasNoOptions(readOptions) ? spec : { ...spec, readOptions };
|
|
94
111
|
|
|
95
|
-
return hasNoOptions(
|
|
112
|
+
return hasNoOptions(writeOptions) ? withReads : { ...withReads, writeOptions };
|
|
96
113
|
}
|
|
97
114
|
|
|
98
115
|
/**
|
package/src/system/json/types.ts
CHANGED
|
@@ -311,6 +311,8 @@ export interface TypeSpec<T = unknown> {
|
|
|
311
311
|
readonly typeName?: string;
|
|
312
312
|
/** The context's read behavior; absent when it is System.Text.Json's default. */
|
|
313
313
|
readonly readOptions?: JsonReadOptions;
|
|
314
|
+
/** The context's text formatting; absent when it writes compact text (the default). */
|
|
315
|
+
readonly writeOptions?: JsonWriteOptions;
|
|
314
316
|
/**
|
|
315
317
|
* TS names of this contract's properties (its base chain's included) that
|
|
316
318
|
* a read sets after construction, and only when the payload carries their
|
|
@@ -387,6 +389,23 @@ export interface JsonReadOptions {
|
|
|
387
389
|
readonly allowOutOfOrderMetadataProperties?: boolean;
|
|
388
390
|
}
|
|
389
391
|
|
|
392
|
+
/**
|
|
393
|
+
* Context-level text formatting mirrored from `[JsonSourceGenerationOptions]`
|
|
394
|
+
* (`WriteIndented`, `IndentCharacter`, `IndentSize`, `NewLine`); only
|
|
395
|
+
* `serializeText` reads it. Every member is optional; an absent member is
|
|
396
|
+
* System.Text.Json's default.
|
|
397
|
+
*/
|
|
398
|
+
export interface JsonWriteOptions {
|
|
399
|
+
/** Write each element and property on its own line. */
|
|
400
|
+
readonly writeIndented?: boolean;
|
|
401
|
+
/** The indent character, a space (the default) or a tab. */
|
|
402
|
+
readonly indentCharacter?: " " | "\t";
|
|
403
|
+
/** The indent characters per nesting level, 0 to 127; 2 by default. */
|
|
404
|
+
readonly indentSize?: number;
|
|
405
|
+
/** The line break; `"\n"` by default (System.Text.Json's is the platform's). */
|
|
406
|
+
readonly newLine?: "\n" | "\r\n";
|
|
407
|
+
}
|
|
408
|
+
|
|
390
409
|
// ─── Value Spec ──────────────────────────────────────────────────────────────
|
|
391
410
|
|
|
392
411
|
/**
|
|
@@ -417,6 +436,8 @@ export interface ValueSpec<T = unknown> {
|
|
|
417
436
|
readonly typeName?: string;
|
|
418
437
|
/** The context's read behavior; absent when it is System.Text.Json's default. */
|
|
419
438
|
readonly readOptions?: JsonReadOptions;
|
|
439
|
+
/** The context's text formatting; absent when it writes compact text (the default). */
|
|
440
|
+
readonly writeOptions?: JsonWriteOptions;
|
|
420
441
|
}
|
|
421
442
|
|
|
422
443
|
/** Any spec the generated context exposes: an object contract or a value root. */
|
package/src/system/math.ts
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
import { ArgumentException } from "./exceptions/argument-exception.ts";
|
|
13
13
|
import { ArgumentOutOfRangeException } from "./exceptions/argument-out-of-range-exception.ts";
|
|
14
14
|
import { OverflowException } from "./exceptions/overflow-exception.ts";
|
|
15
|
+
import { throwMinGreaterThanMax } from "./min-max.ts";
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* Mirror of `System.MidpointRounding`. The numeric values match the .NET enum, so a
|
|
@@ -199,3 +200,46 @@ export function absInt32(value: number): number {
|
|
|
199
200
|
|
|
200
201
|
return Math.abs(value);
|
|
201
202
|
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* `Math.Clamp(value, min, max)` for the `int`, `short`, `byte`, `float`, `double` (and the
|
|
206
|
+
* other `number`-backed) overloads (#750); JavaScript has no `Math.clamp`. A `NaN` `value`
|
|
207
|
+
* stays `NaN`, as in .NET.
|
|
208
|
+
*
|
|
209
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
210
|
+
*/
|
|
211
|
+
export function clampNumber(value: number, min: number, max: number): number {
|
|
212
|
+
if (min > max) {
|
|
213
|
+
throwMinGreaterThanMax(min, max);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (value < min) {
|
|
217
|
+
return min;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return value > max ? max : value;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Structural view of a `decimal.js` `Decimal`, the only operations {@link clampDecimal} uses. */
|
|
224
|
+
export interface DecimalClampable<D> {
|
|
225
|
+
lt(other: D): boolean;
|
|
226
|
+
gt(other: D): boolean;
|
|
227
|
+
toString(): string;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* `Math.Clamp(decimal, decimal, decimal)` (#750): the `Decimal` keeps its kind.
|
|
232
|
+
*
|
|
233
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
234
|
+
*/
|
|
235
|
+
export function clampDecimal<D extends DecimalClampable<D>>(value: D, min: D, max: D): D {
|
|
236
|
+
if (min.gt(max)) {
|
|
237
|
+
throwMinGreaterThanMax(min, max);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (value.lt(min)) {
|
|
241
|
+
return min;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return value.gt(max) ? max : value;
|
|
245
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ArgumentException } from "./exceptions/argument-exception.ts";
|
|
2
|
+
import { formatText } from "./default-text.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The `ArgumentException` of `Math.Clamp` and `INumber<T>.Clamp` when `min` is greater than
|
|
6
|
+
* `max`, with .NET's message. The bounds are formatted as .NET formats them (a `decimal`
|
|
7
|
+
* `0.0000001` stays `0.0000001`, a `double` follows `double.ToString()`).
|
|
8
|
+
*/
|
|
9
|
+
export function throwMinGreaterThanMax(min: unknown, max: unknown): never {
|
|
10
|
+
throw new ArgumentException(`'${formatText(min)}' cannot be greater than ${formatText(max)}.`);
|
|
11
|
+
}
|