|
| 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 | +} |
0 commit comments