@fedify/vocab-runtime 2.1.0-dev.592 → 2.1.0-dev.599
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/deno.json +1 -1
- package/dist/mod.cjs +4428 -4289
- package/dist/mod.d.cts +95 -4
- package/dist/mod.d.ts +95 -4
- package/dist/mod.js +4426 -4290
- package/dist/tests/decimal.test.cjs +154 -0
- package/dist/tests/decimal.test.d.cts +1 -0
- package/dist/tests/decimal.test.d.ts +1 -0
- package/dist/tests/decimal.test.js +153 -0
- package/dist/tests/docloader-B7jXFZpf.cjs +4562 -0
- package/dist/tests/docloader-DMCOWvVB.js +4550 -0
- package/dist/tests/docloader.test.cjs +7 -4491
- package/dist/tests/docloader.test.js +4 -4488
- package/dist/tests/internal/multicodec.test.cjs +1 -1
- package/dist/tests/internal/multicodec.test.js +1 -1
- package/dist/tests/key-ByCmSI2y.js +183 -0
- package/dist/tests/key-CCPn6TEY.cjs +231 -0
- package/dist/tests/key.test.cjs +33 -211
- package/dist/tests/key.test.js +3 -181
- package/dist/tests/langstr-BsVE3s9u.js +30 -0
- package/dist/tests/langstr-EPh86hXK.cjs +36 -0
- package/dist/tests/langstr.test.cjs +5 -33
- package/dist/tests/langstr.test.js +1 -29
- package/dist/tests/link.test.cjs +1 -1
- package/dist/tests/link.test.js +1 -1
- package/dist/tests/multibase/multibase.test.cjs +1 -1
- package/dist/tests/multibase/multibase.test.js +1 -1
- package/dist/tests/{request-DKDE-Rcx.js → request-BQtyeAfw.js} +1 -1
- package/dist/tests/{request-CK7hgRUX.cjs → request-CW9KOmQu.cjs} +1 -1
- package/dist/tests/request.test.cjs +1 -1
- package/dist/tests/request.test.js +1 -1
- package/dist/tests/url.test.cjs +1 -1
- package/dist/tests/url.test.js +1 -1
- package/package.json +1 -1
- package/src/contexts/activitystreams.json +379 -0
- package/src/contexts/did-v1.json +57 -0
- package/src/contexts/fep-5711.json +36 -0
- package/src/contexts/gotosocial.json +86 -0
- package/src/contexts/identity-v1.json +152 -0
- package/src/contexts/joinmastodon.json +28 -0
- package/src/contexts/schemaorg.json +8845 -0
- package/src/contexts/security-data-integrity-v1.json +78 -0
- package/src/contexts/security-data-integrity-v2.json +81 -0
- package/src/contexts/security-multikey-v1.json +35 -0
- package/src/contexts/security-v1.json +74 -0
- package/src/contexts/webfinger.json +10 -0
- package/src/contexts.ts +33 -4392
- package/src/decimal.test.ts +90 -0
- package/src/decimal.ts +112 -0
- package/src/mod.ts +6 -0
- /package/dist/tests/{link-Ck2yj4dH.js → link-C3q2TC2G.js} +0 -0
- /package/dist/tests/{link-CdFPEo9O.cjs → link-DYNFAdNu.cjs} +0 -0
- /package/dist/tests/{multibase-BdHCGO4H.js → multibase-B4g8pz6F.js} +0 -0
- /package/dist/tests/{multibase-B2D6B0V4.cjs → multibase-o_ovPHYJ.cjs} +0 -0
- /package/dist/tests/{multicodec-mHcRzSGY.cjs → multicodec--6hQ74zI.cjs} +0 -0
- /package/dist/tests/{multicodec-DvC5xnX2.js → multicodec-Dq3IiOV4.js} +0 -0
- /package/dist/tests/{url-fW_DHbih.js → url-CWEP9Zs9.js} +0 -0
- /package/dist/tests/{url-C5Vs9nYh.cjs → url-DIjOdK8Q.cjs} +0 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { deepStrictEqual, throws } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { canParseDecimal, isDecimal, parseDecimal } from "./decimal.ts";
|
|
4
|
+
import {
|
|
5
|
+
canParseDecimal as canParseDecimalFromModule,
|
|
6
|
+
isDecimal as isDecimalFromModule,
|
|
7
|
+
parseDecimal as parseDecimalFromModule,
|
|
8
|
+
} from "./mod.ts";
|
|
9
|
+
|
|
10
|
+
test("parseDecimal() accepts valid xsd:decimal lexical forms", () => {
|
|
11
|
+
const values = [
|
|
12
|
+
"-1.23",
|
|
13
|
+
"12678967.543233",
|
|
14
|
+
"+100000.00",
|
|
15
|
+
"210",
|
|
16
|
+
".5",
|
|
17
|
+
"5.",
|
|
18
|
+
"0",
|
|
19
|
+
"-0.0",
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
for (const value of values) {
|
|
23
|
+
deepStrictEqual(parseDecimal(value), value);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("isDecimal() reports valid xsd:decimal lexical forms", () => {
|
|
28
|
+
deepStrictEqual(isDecimal("12.50"), true);
|
|
29
|
+
deepStrictEqual(isDecimal(".5"), true);
|
|
30
|
+
deepStrictEqual(isDecimal("1e3"), false);
|
|
31
|
+
deepStrictEqual(isDecimal(" 12.50 "), false);
|
|
32
|
+
deepStrictEqual(isDecimal("\t12.50\n"), false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("canParseDecimal() accepts whitespace-normalized xsd:decimal strings", () => {
|
|
36
|
+
deepStrictEqual(canParseDecimal("12.50"), true);
|
|
37
|
+
deepStrictEqual(canParseDecimal(" 12.50 "), true);
|
|
38
|
+
deepStrictEqual(canParseDecimal("\t+100000.00\r\n"), true);
|
|
39
|
+
deepStrictEqual(canParseDecimal(" .5 "), true);
|
|
40
|
+
deepStrictEqual(canParseDecimal("1e3"), false);
|
|
41
|
+
deepStrictEqual(canParseDecimal("1 2.50"), false);
|
|
42
|
+
deepStrictEqual(canParseDecimal("1\t2.50"), false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("parseDecimal() normalizes XML Schema whitespace", () => {
|
|
46
|
+
deepStrictEqual(parseDecimal("12.50"), "12.50");
|
|
47
|
+
deepStrictEqual(parseDecimal(" 12.50 "), "12.50");
|
|
48
|
+
deepStrictEqual(parseDecimal("\t+100000.00\r\n"), "+100000.00");
|
|
49
|
+
deepStrictEqual(parseDecimal(" .5 "), ".5");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("parseDecimal() rejects invalid xsd:decimal lexical forms", () => {
|
|
53
|
+
const values = [
|
|
54
|
+
"",
|
|
55
|
+
".",
|
|
56
|
+
"+",
|
|
57
|
+
"-",
|
|
58
|
+
"1e3",
|
|
59
|
+
"NaN",
|
|
60
|
+
"INF",
|
|
61
|
+
"1,2",
|
|
62
|
+
"1..2",
|
|
63
|
+
"1 2.3",
|
|
64
|
+
"1\t2.3",
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
for (const value of values) {
|
|
68
|
+
throws(
|
|
69
|
+
() => parseDecimal(value),
|
|
70
|
+
{
|
|
71
|
+
name: "TypeError",
|
|
72
|
+
message: `${
|
|
73
|
+
JSON.stringify(value)
|
|
74
|
+
} is not a valid xsd:decimal lexical form.`,
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("parseDecimal() is exported from the package root", () => {
|
|
81
|
+
deepStrictEqual(parseDecimalFromModule("12.50"), "12.50");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("canParseDecimal() is exported from the package root", () => {
|
|
85
|
+
deepStrictEqual(canParseDecimalFromModule(" 12.50 "), true);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("isDecimal() is exported from the package root", () => {
|
|
89
|
+
deepStrictEqual(isDecimalFromModule("12.50"), true);
|
|
90
|
+
});
|
package/src/decimal.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
const DECIMAL_PATTERN = /^(\+|-)?([0-9]+(\.[0-9]*)?|\.[0-9]+)$/;
|
|
2
|
+
const XML_SCHEMA_WHITESPACE_PATTERN = /[\t\n\r ]+/g;
|
|
3
|
+
|
|
4
|
+
function collapseXmlSchemaWhitespace(value: string): string {
|
|
5
|
+
return value.replace(XML_SCHEMA_WHITESPACE_PATTERN, " ").trim();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A branded string representing an `xsd:decimal` value.
|
|
10
|
+
*
|
|
11
|
+
* Unlike JavaScript's `number`, `xsd:decimal` is intended for exact decimal
|
|
12
|
+
* values such as prices, quantities, and measurements where binary
|
|
13
|
+
* floating-point rounding would be inappropriate. Fedify therefore represents
|
|
14
|
+
* these values as validated strings at runtime while preserving a distinct
|
|
15
|
+
* TypeScript type.
|
|
16
|
+
*
|
|
17
|
+
* Values of this type must be created through {@link parseDecimal}, which
|
|
18
|
+
* validates that the string matches the XML Schema `xsd:decimal` lexical form.
|
|
19
|
+
*
|
|
20
|
+
* The runtime representation is still a plain string. The brand exists only
|
|
21
|
+
* at the type level so APIs can distinguish arbitrary strings from validated
|
|
22
|
+
* decimal literals without introducing a decimal arithmetic dependency.
|
|
23
|
+
*
|
|
24
|
+
* Supported lexical forms include signed and unsigned integers and decimal
|
|
25
|
+
* fractions such as `"-1.23"`, `"+100000.00"`, `"210"`, `".5"`, and `"5."`.
|
|
26
|
+
* Scientific notation such as `"1e3"` and special values like `"NaN"` are
|
|
27
|
+
* rejected. Strings with surrounding XML Schema whitespace can be normalized
|
|
28
|
+
* by {@link parseDecimal}, but values of this type are always stored in their
|
|
29
|
+
* normalized lexical form.
|
|
30
|
+
*
|
|
31
|
+
* This representation is designed to be forward-compatible with a future
|
|
32
|
+
* native decimal type if JavaScript eventually gains one, while keeping the
|
|
33
|
+
* public API semantically precise today.
|
|
34
|
+
*
|
|
35
|
+
* @since 2.1.0
|
|
36
|
+
*/
|
|
37
|
+
export type Decimal = string & { readonly __brand: "Decimal" };
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Checks whether a string is a valid `xsd:decimal` lexical form.
|
|
41
|
+
*
|
|
42
|
+
* This predicate checks the lexical form strictly, without applying XML Schema
|
|
43
|
+
* whitespace normalization first. It is useful as a type guard for values
|
|
44
|
+
* that are already expected to be normalized decimal strings.
|
|
45
|
+
*
|
|
46
|
+
* @param value A candidate `xsd:decimal` lexical form.
|
|
47
|
+
* @returns `true` if the string matches the XML Schema `xsd:decimal` lexical
|
|
48
|
+
* form, or `false` otherwise.
|
|
49
|
+
* @since 2.1.0
|
|
50
|
+
*/
|
|
51
|
+
export function isDecimal(value: string): value is Decimal {
|
|
52
|
+
return DECIMAL_PATTERN.test(value);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Checks whether a string can be parsed as an `xsd:decimal` lexical form.
|
|
57
|
+
*
|
|
58
|
+
* Unlike {@link isDecimal}, this predicate first applies the XML Schema
|
|
59
|
+
* `whiteSpace="collapse"` normalization step and then validates the
|
|
60
|
+
* normalized string. This means values like `" 12.50 "` are parseable even
|
|
61
|
+
* though they are not already normalized decimal literals.
|
|
62
|
+
*
|
|
63
|
+
* @param value A candidate `xsd:decimal` lexical form.
|
|
64
|
+
* @returns `true` if the normalized string matches the XML Schema
|
|
65
|
+
* `xsd:decimal` lexical form, or `false` otherwise.
|
|
66
|
+
* @since 2.1.0
|
|
67
|
+
*/
|
|
68
|
+
export function canParseDecimal(value: string): boolean {
|
|
69
|
+
return isDecimal(collapseXmlSchemaWhitespace(value));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Parses a string as an `xsd:decimal` lexical form and returns it as a
|
|
74
|
+
* branded {@link Decimal}.
|
|
75
|
+
*
|
|
76
|
+
* This function validates the input against the XML Schema `xsd:decimal`
|
|
77
|
+
* lexical space after applying the XML Schema `whiteSpace="collapse"`
|
|
78
|
+
* normalization step. It returns the normalized string without any further
|
|
79
|
+
* canonicalization.
|
|
80
|
+
*
|
|
81
|
+
* @param value A candidate `xsd:decimal` lexical form.
|
|
82
|
+
* @returns The normalized string branded as {@link Decimal}.
|
|
83
|
+
* @throws {TypeError} Thrown when the value is not a valid `xsd:decimal`
|
|
84
|
+
* lexical form.
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const price = parseDecimal("12.50");
|
|
88
|
+
* ```
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const price = parseDecimal(" 12.50 ");
|
|
92
|
+
* console.assert(price === "12.50");
|
|
93
|
+
* ```
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* try {
|
|
97
|
+
* parseDecimal("1e3");
|
|
98
|
+
* } catch (error) {
|
|
99
|
+
* console.assert(error instanceof TypeError);
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
* @since 2.1.0
|
|
103
|
+
*/
|
|
104
|
+
export function parseDecimal(value: string): Decimal {
|
|
105
|
+
const normalized = collapseXmlSchemaWhitespace(value);
|
|
106
|
+
if (!isDecimal(normalized)) {
|
|
107
|
+
throw new TypeError(
|
|
108
|
+
`${JSON.stringify(value)} is not a valid xsd:decimal lexical form.`,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
return normalized as Decimal;
|
|
112
|
+
}
|
package/src/mod.ts
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|