metano-runtime 2.7.1 → 2.7.2

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.
@@ -11,3 +11,4 @@ export * from "./bind-receiver.ts";
11
11
  export * from "./integer-division.ts";
12
12
  export * from "./math.ts";
13
13
  export * from "./number-parsing.ts";
14
+ export * from "./strings.ts";
@@ -11,3 +11,4 @@ export * from "./bind-receiver.js";
11
11
  export * from "./integer-division.js";
12
12
  export * from "./math.js";
13
13
  export * from "./number-parsing.js";
14
+ export * from "./strings.js";
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Runtime helpers for `System.String` members whose C# semantics differ from the
3
+ * closest JavaScript built-in.
4
+ */
5
+ /**
6
+ * C#'s `string.IsNullOrEmpty(value)`: `true` for `null`, `undefined` and `""`.
7
+ *
8
+ * The type predicate lets a `false` result narrow `value` to `string`, as C#'s
9
+ * `[NotNullWhen(false)]` does.
10
+ */
11
+ export declare function isNullOrEmpty(value: string | null | undefined): value is "" | null | undefined;
12
+ /**
13
+ * What a `true` {@link isNullOrWhiteSpace} result narrows its argument to: a missing
14
+ * value or a string the brand marks as white space only. The brand exists only in the
15
+ * type system; it keeps the narrowed string comparable with any literal (`s === " "`).
16
+ */
17
+ export type NullOrWhiteSpace = (string & {
18
+ readonly __brand: "WhiteSpace";
19
+ }) | null | undefined;
20
+ /**
21
+ * C#'s `string.IsNullOrWhiteSpace(value)`: `true` for `null`, `undefined`, `""` and a
22
+ * string made only of .NET white-space characters.
23
+ *
24
+ * The type predicate lets a `false` result narrow `value` to `string`, as C#'s
25
+ * `[NotNullWhen(false)]` does.
26
+ */
27
+ export declare function isNullOrWhiteSpace(value: string | null | undefined): value is NullOrWhiteSpace;
28
+ /**
29
+ * C#'s `value.Replace(oldValue, newValue)` for the `(char, char)` and
30
+ * `(string, string?)` overloads: replaces every ordinal occurrence of `oldValue`,
31
+ * scanning left to right without overlap. Unlike `String.prototype.replace`, it
32
+ * replaces all occurrences, and unlike `replaceAll`, `$` sequences in `newValue` are
33
+ * inserted literally. A `null` `newValue` removes the occurrences.
34
+ *
35
+ * @throws Error with .NET's `ArgumentNullException` message when `oldValue` is `null`,
36
+ * and with its `ArgumentException` message when `oldValue` is empty.
37
+ */
38
+ export declare function stringReplace(value: string, oldValue: string, newValue: string | null | undefined): string;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Runtime helpers for `System.String` members whose C# semantics differ from the
3
+ * closest JavaScript built-in.
4
+ */
5
+ /**
6
+ * The single characters .NET's `char.IsWhiteSpace` accepts besides the ranges tested in
7
+ * {@link isDotNetWhiteSpace}. JavaScript's `trim()` and `\s` disagree with .NET on two
8
+ * code points: they accept U+FEFF, which .NET does not, and reject U+0085, which .NET
9
+ * accepts.
10
+ */
11
+ const DOTNET_WHITE_SPACE_CODES = new Set([
12
+ 0x0020, 0x0085, 0x00a0, 0x1680, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000,
13
+ ]);
14
+ /** Whether `code` is a .NET white-space character (`char.IsWhiteSpace`). */
15
+ function isDotNetWhiteSpace(code) {
16
+ const isControlWhiteSpace = code >= 0x0009 && code <= 0x000d;
17
+ const isSpaceSeparatorRange = code >= 0x2000 && code <= 0x200a;
18
+ return isControlWhiteSpace || isSpaceSeparatorRange || DOTNET_WHITE_SPACE_CODES.has(code);
19
+ }
20
+ /**
21
+ * C#'s `string.IsNullOrEmpty(value)`: `true` for `null`, `undefined` and `""`.
22
+ *
23
+ * The type predicate lets a `false` result narrow `value` to `string`, as C#'s
24
+ * `[NotNullWhen(false)]` does.
25
+ */
26
+ export function isNullOrEmpty(value) {
27
+ return value == null || value.length === 0;
28
+ }
29
+ /**
30
+ * C#'s `string.IsNullOrWhiteSpace(value)`: `true` for `null`, `undefined`, `""` and a
31
+ * string made only of .NET white-space characters.
32
+ *
33
+ * The type predicate lets a `false` result narrow `value` to `string`, as C#'s
34
+ * `[NotNullWhen(false)]` does.
35
+ */
36
+ export function isNullOrWhiteSpace(value) {
37
+ if (value == null) {
38
+ return true;
39
+ }
40
+ for (let index = 0; index < value.length; index++) {
41
+ if (!isDotNetWhiteSpace(value.charCodeAt(index))) {
42
+ return false;
43
+ }
44
+ }
45
+ return true;
46
+ }
47
+ /**
48
+ * C#'s `value.Replace(oldValue, newValue)` for the `(char, char)` and
49
+ * `(string, string?)` overloads: replaces every ordinal occurrence of `oldValue`,
50
+ * scanning left to right without overlap. Unlike `String.prototype.replace`, it
51
+ * replaces all occurrences, and unlike `replaceAll`, `$` sequences in `newValue` are
52
+ * inserted literally. A `null` `newValue` removes the occurrences.
53
+ *
54
+ * @throws Error with .NET's `ArgumentNullException` message when `oldValue` is `null`,
55
+ * and with its `ArgumentException` message when `oldValue` is empty.
56
+ */
57
+ export function stringReplace(value, oldValue, newValue) {
58
+ // Generated code is not always type-checked against this signature (a C# `null!`
59
+ // argument reaches here), so the C# null check stays.
60
+ if (oldValue == null) {
61
+ throw new Error("Value cannot be null. (Parameter 'oldValue')");
62
+ }
63
+ if (oldValue.length === 0) {
64
+ throw new Error("The value cannot be an empty string. (Parameter 'oldValue')");
65
+ }
66
+ return value.split(oldValue).join(newValue ?? "");
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metano-runtime",
3
- "version": "2.7.1",
3
+ "version": "2.7.2",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",