-
-
Notifications
You must be signed in to change notification settings - Fork 96
Add xsd:decimal support to vocab runtime and codegen
#640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce09b55
Add xsd:decimal runtime and codegen support
dahlia 9445aba
Relax generated vocab lint rules
dahlia 207f8aa
Fix parseDecimal doctest example
dahlia 05d793d
Handle xsd:decimal whitespace normalization
dahlia af8de5a
Reject ambiguous decimal/string unions
dahlia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { deepStrictEqual, throws } from "node:assert"; | ||
| import { test } from "node:test"; | ||
| import { canParseDecimal, isDecimal, parseDecimal } from "./decimal.ts"; | ||
| import { | ||
| canParseDecimal as canParseDecimalFromModule, | ||
| isDecimal as isDecimalFromModule, | ||
| parseDecimal as parseDecimalFromModule, | ||
| } from "./mod.ts"; | ||
|
|
||
| test("parseDecimal() accepts valid xsd:decimal lexical forms", () => { | ||
| const values = [ | ||
| "-1.23", | ||
| "12678967.543233", | ||
| "+100000.00", | ||
| "210", | ||
| ".5", | ||
| "5.", | ||
| "0", | ||
| "-0.0", | ||
| ]; | ||
|
|
||
| for (const value of values) { | ||
| deepStrictEqual(parseDecimal(value), value); | ||
| } | ||
| }); | ||
|
|
||
| test("isDecimal() reports valid xsd:decimal lexical forms", () => { | ||
| deepStrictEqual(isDecimal("12.50"), true); | ||
| deepStrictEqual(isDecimal(".5"), true); | ||
| deepStrictEqual(isDecimal("1e3"), false); | ||
| deepStrictEqual(isDecimal(" 12.50 "), false); | ||
| deepStrictEqual(isDecimal("\t12.50\n"), false); | ||
| }); | ||
|
|
||
| test("canParseDecimal() accepts whitespace-normalized xsd:decimal strings", () => { | ||
| deepStrictEqual(canParseDecimal("12.50"), true); | ||
| deepStrictEqual(canParseDecimal(" 12.50 "), true); | ||
| deepStrictEqual(canParseDecimal("\t+100000.00\r\n"), true); | ||
| deepStrictEqual(canParseDecimal(" .5 "), true); | ||
| deepStrictEqual(canParseDecimal("1e3"), false); | ||
| deepStrictEqual(canParseDecimal("1 2.50"), false); | ||
| deepStrictEqual(canParseDecimal("1\t2.50"), false); | ||
| }); | ||
|
|
||
| test("parseDecimal() normalizes XML Schema whitespace", () => { | ||
| deepStrictEqual(parseDecimal("12.50"), "12.50"); | ||
| deepStrictEqual(parseDecimal(" 12.50 "), "12.50"); | ||
| deepStrictEqual(parseDecimal("\t+100000.00\r\n"), "+100000.00"); | ||
| deepStrictEqual(parseDecimal(" .5 "), ".5"); | ||
| }); | ||
|
|
||
| test("parseDecimal() rejects invalid xsd:decimal lexical forms", () => { | ||
| const values = [ | ||
| "", | ||
| ".", | ||
| "+", | ||
| "-", | ||
| "1e3", | ||
| "NaN", | ||
| "INF", | ||
| "1,2", | ||
| "1..2", | ||
| "1 2.3", | ||
| "1\t2.3", | ||
| ]; | ||
|
|
||
| for (const value of values) { | ||
| throws( | ||
| () => parseDecimal(value), | ||
| { | ||
| name: "TypeError", | ||
| message: `${ | ||
| JSON.stringify(value) | ||
| } is not a valid xsd:decimal lexical form.`, | ||
| }, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| test("parseDecimal() is exported from the package root", () => { | ||
| deepStrictEqual(parseDecimalFromModule("12.50"), "12.50"); | ||
| }); | ||
|
|
||
| test("canParseDecimal() is exported from the package root", () => { | ||
| deepStrictEqual(canParseDecimalFromModule(" 12.50 "), true); | ||
| }); | ||
|
|
||
| test("isDecimal() is exported from the package root", () => { | ||
| deepStrictEqual(isDecimalFromModule("12.50"), true); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| const DECIMAL_PATTERN = /^(\+|-)?([0-9]+(\.[0-9]*)?|\.[0-9]+)$/; | ||
| const XML_SCHEMA_WHITESPACE_PATTERN = /[\t\n\r ]+/g; | ||
|
|
||
| function collapseXmlSchemaWhitespace(value: string): string { | ||
| return value.replace(XML_SCHEMA_WHITESPACE_PATTERN, " ").trim(); | ||
| } | ||
|
|
||
| /** | ||
| * A branded string representing an `xsd:decimal` value. | ||
| * | ||
| * Unlike JavaScript's `number`, `xsd:decimal` is intended for exact decimal | ||
| * values such as prices, quantities, and measurements where binary | ||
| * floating-point rounding would be inappropriate. Fedify therefore represents | ||
| * these values as validated strings at runtime while preserving a distinct | ||
| * TypeScript type. | ||
| * | ||
| * Values of this type must be created through {@link parseDecimal}, which | ||
| * validates that the string matches the XML Schema `xsd:decimal` lexical form. | ||
| * | ||
| * The runtime representation is still a plain string. The brand exists only | ||
| * at the type level so APIs can distinguish arbitrary strings from validated | ||
| * decimal literals without introducing a decimal arithmetic dependency. | ||
| * | ||
| * Supported lexical forms include signed and unsigned integers and decimal | ||
| * fractions such as `"-1.23"`, `"+100000.00"`, `"210"`, `".5"`, and `"5."`. | ||
| * Scientific notation such as `"1e3"` and special values like `"NaN"` are | ||
| * rejected. Strings with surrounding XML Schema whitespace can be normalized | ||
| * by {@link parseDecimal}, but values of this type are always stored in their | ||
| * normalized lexical form. | ||
| * | ||
| * This representation is designed to be forward-compatible with a future | ||
| * native decimal type if JavaScript eventually gains one, while keeping the | ||
| * public API semantically precise today. | ||
| * | ||
| * @since 2.1.0 | ||
| */ | ||
| export type Decimal = string & { readonly __brand: "Decimal" }; | ||
|
|
||
| /** | ||
| * Checks whether a string is a valid `xsd:decimal` lexical form. | ||
| * | ||
| * This predicate checks the lexical form strictly, without applying XML Schema | ||
| * whitespace normalization first. It is useful as a type guard for values | ||
| * that are already expected to be normalized decimal strings. | ||
| * | ||
| * @param value A candidate `xsd:decimal` lexical form. | ||
| * @returns `true` if the string matches the XML Schema `xsd:decimal` lexical | ||
| * form, or `false` otherwise. | ||
| * @since 2.1.0 | ||
| */ | ||
| export function isDecimal(value: string): value is Decimal { | ||
| return DECIMAL_PATTERN.test(value); | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a string can be parsed as an `xsd:decimal` lexical form. | ||
| * | ||
| * Unlike {@link isDecimal}, this predicate first applies the XML Schema | ||
| * `whiteSpace="collapse"` normalization step and then validates the | ||
| * normalized string. This means values like `" 12.50 "` are parseable even | ||
| * though they are not already normalized decimal literals. | ||
| * | ||
| * @param value A candidate `xsd:decimal` lexical form. | ||
| * @returns `true` if the normalized string matches the XML Schema | ||
| * `xsd:decimal` lexical form, or `false` otherwise. | ||
| * @since 2.1.0 | ||
| */ | ||
| export function canParseDecimal(value: string): boolean { | ||
| return isDecimal(collapseXmlSchemaWhitespace(value)); | ||
| } | ||
|
|
||
| /** | ||
| * Parses a string as an `xsd:decimal` lexical form and returns it as a | ||
| * branded {@link Decimal}. | ||
| * | ||
| * This function validates the input against the XML Schema `xsd:decimal` | ||
| * lexical space after applying the XML Schema `whiteSpace="collapse"` | ||
| * normalization step. It returns the normalized string without any further | ||
| * canonicalization. | ||
| * | ||
| * @param value A candidate `xsd:decimal` lexical form. | ||
| * @returns The normalized string branded as {@link Decimal}. | ||
| * @throws {TypeError} Thrown when the value is not a valid `xsd:decimal` | ||
| * lexical form. | ||
| * @example | ||
| * ```typescript | ||
| * const price = parseDecimal("12.50"); | ||
| * ``` | ||
| * @example | ||
| * ```typescript | ||
| * const price = parseDecimal(" 12.50 "); | ||
| * console.assert(price === "12.50"); | ||
| * ``` | ||
| * @example | ||
| * ```typescript | ||
| * try { | ||
| * parseDecimal("1e3"); | ||
| * } catch (error) { | ||
| * console.assert(error instanceof TypeError); | ||
| * } | ||
| * ``` | ||
| * @since 2.1.0 | ||
| */ | ||
| export function parseDecimal(value: string): Decimal { | ||
| const normalized = collapseXmlSchemaWhitespace(value); | ||
| if (!isDecimal(normalized)) { | ||
| throw new TypeError( | ||
| `${JSON.stringify(value)} is not a valid xsd:decimal lexical form.`, | ||
| ); | ||
| } | ||
| return normalized as Decimal; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 6 additions & 2 deletions
8
packages/vocab-tools/src/__snapshots__/class.test.ts.deno.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 6 additions & 2 deletions
8
packages/vocab-tools/src/__snapshots__/class.test.ts.node.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.