@formatjs/icu-skeleton-parser
Advanced tools
| {"version":3,"file":"index.js","names":[],"sources":["../date-time.ts","../regex.generated.ts","../number.ts"],"sourcesContent":["/**\n * https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table\n * Credit: https://github.com/caridy/intl-datetimeformat-pattern/blob/master/index.js\n * with some tweaks\n */\nconst DATE_TIME_REGEX =\n /(?:[Eec]{1,6}|G{1,5}|[Qq]{1,5}|(?:[yYur]+|U{1,5})|[ML]{1,5}|d{1,2}|D{1,3}|F{1}|[abB]{1,5}|[hkHK]{1,2}|w{1,2}|W{1}|m{1,2}|s{1,2}|[zZOvVxX]{1,4})(?=([^']*'[^']*')*[^']*$)/g\n\n/**\n * Parse Date time skeleton into Intl.DateTimeFormatOptions\n * Ref: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table\n * @public\n * @param skeleton skeleton string\n */\nexport function parseDateTimeSkeleton(\n skeleton: string\n): Intl.DateTimeFormatOptions {\n const result: Intl.DateTimeFormatOptions = {}\n skeleton.replace(DATE_TIME_REGEX, match => {\n const len = match.length\n switch (match[0]) {\n // Era\n case 'G':\n result.era = len === 4 ? 'long' : len === 5 ? 'narrow' : 'short'\n break\n // Year\n case 'y':\n result.year = len === 2 ? '2-digit' : 'numeric'\n break\n case 'Y':\n case 'u':\n case 'U':\n case 'r':\n throw new RangeError(\n '`Y/u/U/r` (year) patterns are not supported, use `y` instead'\n )\n // Quarter\n case 'q':\n case 'Q':\n throw new RangeError('`q/Q` (quarter) patterns are not supported')\n // Month\n case 'M':\n case 'L':\n result.month = ['numeric', '2-digit', 'short', 'long', 'narrow'][\n len - 1\n ] as 'numeric'\n break\n // Week\n case 'w':\n case 'W':\n throw new RangeError('`w/W` (week) patterns are not supported')\n case 'd':\n result.day = ['numeric', '2-digit'][len - 1] as 'numeric'\n break\n case 'D':\n case 'F':\n case 'g':\n throw new RangeError(\n '`D/F/g` (day) patterns are not supported, use `d` instead'\n )\n // Weekday\n case 'E':\n result.weekday = len === 4 ? 'long' : len === 5 ? 'narrow' : 'short'\n break\n case 'e':\n if (len < 4) {\n throw new RangeError('`e..eee` (weekday) patterns are not supported')\n }\n result.weekday = ['short', 'long', 'narrow', 'short'][\n len - 4\n ] as 'short'\n break\n case 'c':\n if (len < 4) {\n throw new RangeError('`c..ccc` (weekday) patterns are not supported')\n }\n result.weekday = ['short', 'long', 'narrow', 'short'][\n len - 4\n ] as 'short'\n break\n\n // Period\n case 'a': // AM, PM\n result.hour12 = true\n break\n case 'b': // am, pm, noon, midnight\n case 'B': // flexible day periods\n throw new RangeError(\n '`b/B` (period) patterns are not supported, use `a` instead'\n )\n // Hour\n case 'h':\n result.hourCycle = 'h12'\n result.hour = ['numeric', '2-digit'][len - 1] as 'numeric'\n break\n case 'H':\n result.hourCycle = 'h23'\n result.hour = ['numeric', '2-digit'][len - 1] as 'numeric'\n break\n case 'K':\n result.hourCycle = 'h11'\n result.hour = ['numeric', '2-digit'][len - 1] as 'numeric'\n break\n case 'k':\n result.hourCycle = 'h24'\n result.hour = ['numeric', '2-digit'][len - 1] as 'numeric'\n break\n case 'j':\n case 'J':\n case 'C':\n throw new RangeError(\n '`j/J/C` (hour) patterns are not supported, use `h/H/K/k` instead'\n )\n // Minute\n case 'm':\n result.minute = ['numeric', '2-digit'][len - 1] as 'numeric'\n break\n // Second\n case 's':\n result.second = ['numeric', '2-digit'][len - 1] as 'numeric'\n break\n case 'S':\n case 'A':\n throw new RangeError(\n '`S/A` (second) patterns are not supported, use `s` instead'\n )\n // Zone\n case 'z': // 1..3, 4: specific non-location format\n result.timeZoneName = len < 4 ? 'short' : 'long'\n break\n case 'Z': // 1..3, 4, 5: The ISO8601 varios formats\n case 'O': // 1, 4: milliseconds in day short, long\n case 'v': // 1, 4: generic non-location format\n case 'V': // 1, 2, 3, 4: time zone ID or city\n case 'X': // 1, 2, 3, 4: The ISO8601 varios formats\n case 'x': // 1, 2, 3, 4: The ISO8601 varios formats\n throw new RangeError(\n '`Z/O/v/V/X/x` (timeZone) patterns are not supported, use `z` instead'\n )\n }\n return ''\n })\n return result\n}\n","// @generated from regex-gen.ts\nexport const WHITE_SPACE_REGEX: RegExp = /[\\t-\\r \\x85\\u200E\\u200F\\u2028\\u2029]/i\n","import type {NumberFormatOptions} from '#packages/ecma402-abstract/types/number.js'\nimport {WHITE_SPACE_REGEX} from '#packages/icu-skeleton-parser/regex.generated.js'\n\nexport interface ExtendedNumberFormatOptions extends NumberFormatOptions {\n scale?: number\n}\nexport interface NumberSkeletonToken {\n stem: string\n options: string[]\n}\n\nexport function parseNumberSkeletonFromString(\n skeleton: string\n): NumberSkeletonToken[] {\n if (skeleton.length === 0) {\n throw new Error('Number skeleton cannot be empty')\n }\n // Parse the skeleton\n const stringTokens = skeleton\n .split(WHITE_SPACE_REGEX)\n .filter(x => x.length > 0)\n\n const tokens: NumberSkeletonToken[] = []\n for (const stringToken of stringTokens) {\n let stemAndOptions = stringToken.split('/')\n if (stemAndOptions.length === 0) {\n throw new Error('Invalid number skeleton')\n }\n\n const [stem, ...options] = stemAndOptions\n for (const option of options) {\n if (option.length === 0) {\n throw new Error('Invalid number skeleton')\n }\n }\n\n tokens.push({stem, options})\n }\n return tokens\n}\n\nfunction icuUnitToEcma(unit: string): ExtendedNumberFormatOptions['unit'] {\n return unit.replace(/^(.*?)-/, '') as ExtendedNumberFormatOptions['unit']\n}\n\nconst FRACTION_PRECISION_REGEX = /^\\.(?:(0+)(\\*)?|(#+)|(0+)(#+))$/g\nconst SIGNIFICANT_PRECISION_REGEX = /^(@+)?(\\+|#+)?[rs]?$/g\nconst INTEGER_WIDTH_REGEX = /(\\*)(0+)|(#+)(0+)|(0+)/g\nconst CONCISE_INTEGER_WIDTH_REGEX = /^(0+)$/\n\nfunction parseSignificantPrecision(str: string): ExtendedNumberFormatOptions {\n const result: ExtendedNumberFormatOptions = {}\n if (str[str.length - 1] === 'r') {\n result.roundingPriority = 'morePrecision'\n } else if (str[str.length - 1] === 's') {\n result.roundingPriority = 'lessPrecision'\n }\n str.replace(\n SIGNIFICANT_PRECISION_REGEX,\n function (_: string, g1: string, g2: string | number) {\n // @@@ case\n if (typeof g2 !== 'string') {\n result.minimumSignificantDigits = g1.length\n result.maximumSignificantDigits = g1.length\n }\n // @@@+ case\n else if (g2 === '+') {\n result.minimumSignificantDigits = g1.length\n }\n // .### case\n else if (g1[0] === '#') {\n result.maximumSignificantDigits = g1.length\n }\n // .@@## or .@@@ case\n else {\n result.minimumSignificantDigits = g1.length\n result.maximumSignificantDigits =\n g1.length + (typeof g2 === 'string' ? g2.length : 0)\n }\n return ''\n }\n )\n return result\n}\n\nfunction parseSign(str: string): ExtendedNumberFormatOptions | undefined {\n switch (str) {\n case 'sign-auto':\n return {\n signDisplay: 'auto',\n }\n case 'sign-accounting':\n case '()':\n return {\n currencySign: 'accounting',\n }\n case 'sign-always':\n case '+!':\n return {\n signDisplay: 'always',\n }\n case 'sign-accounting-always':\n case '()!':\n return {\n signDisplay: 'always',\n currencySign: 'accounting',\n }\n case 'sign-except-zero':\n case '+?':\n return {\n signDisplay: 'exceptZero',\n }\n case 'sign-accounting-except-zero':\n case '()?':\n return {\n signDisplay: 'exceptZero',\n currencySign: 'accounting',\n }\n case 'sign-never':\n case '+_':\n return {\n signDisplay: 'never',\n }\n }\n}\n\nfunction parseConciseScientificAndEngineeringStem(\n stem: string\n): ExtendedNumberFormatOptions | undefined {\n // Engineering\n let result: ExtendedNumberFormatOptions | undefined\n if (stem[0] === 'E' && stem[1] === 'E') {\n result = {\n notation: 'engineering',\n }\n stem = stem.slice(2)\n } else if (stem[0] === 'E') {\n result = {\n notation: 'scientific',\n }\n stem = stem.slice(1)\n }\n if (result) {\n const signDisplay = stem.slice(0, 2)\n if (signDisplay === '+!') {\n result.signDisplay = 'always'\n stem = stem.slice(2)\n } else if (signDisplay === '+?') {\n result.signDisplay = 'exceptZero'\n stem = stem.slice(2)\n }\n if (!CONCISE_INTEGER_WIDTH_REGEX.test(stem)) {\n throw new Error('Malformed concise eng/scientific notation')\n }\n result.minimumIntegerDigits = stem.length\n }\n return result\n}\n\nfunction parseNotationOptions(opt: string): ExtendedNumberFormatOptions {\n const result: ExtendedNumberFormatOptions = {}\n const signOpts = parseSign(opt)\n if (signOpts) {\n return signOpts\n }\n return result\n}\n\n/**\n * https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options\n */\nexport function parseNumberSkeleton(\n tokens: NumberSkeletonToken[]\n): ExtendedNumberFormatOptions {\n let result: ExtendedNumberFormatOptions = {}\n for (const token of tokens) {\n switch (token.stem) {\n case 'percent':\n case '%':\n result.style = 'percent'\n continue\n case '%x100':\n result.style = 'percent'\n result.scale = 100\n continue\n case 'currency':\n result.style = 'currency'\n result.currency = token.options[0]\n continue\n case 'group-off':\n case ',_':\n result.useGrouping = false\n continue\n case 'precision-integer':\n case '.':\n result.maximumFractionDigits = 0\n continue\n case 'measure-unit':\n case 'unit':\n result.style = 'unit'\n result.unit = icuUnitToEcma(token.options[0])\n continue\n case 'compact-short':\n case 'K':\n result.notation = 'compact'\n result.compactDisplay = 'short'\n continue\n case 'compact-long':\n case 'KK':\n result.notation = 'compact'\n result.compactDisplay = 'long'\n continue\n case 'scientific':\n result = {\n ...result,\n notation: 'scientific',\n ...token.options.reduce(\n (all, opt) => ({...all, ...parseNotationOptions(opt)}),\n {}\n ),\n }\n continue\n case 'engineering':\n result = {\n ...result,\n notation: 'engineering',\n ...token.options.reduce(\n (all, opt) => ({...all, ...parseNotationOptions(opt)}),\n {}\n ),\n }\n continue\n case 'notation-simple':\n result.notation = 'standard'\n continue\n // https://github.com/unicode-org/icu/blob/master/icu4c/source/i18n/unicode/unumberformatter.h\n case 'unit-width-narrow':\n result.currencyDisplay = 'narrowSymbol'\n result.unitDisplay = 'narrow'\n continue\n case 'unit-width-short':\n result.currencyDisplay = 'code'\n result.unitDisplay = 'short'\n continue\n case 'unit-width-full-name':\n result.currencyDisplay = 'name'\n result.unitDisplay = 'long'\n continue\n case 'unit-width-iso-code':\n result.currencyDisplay = 'symbol'\n continue\n case 'scale':\n result.scale = parseFloat(token.options[0])\n continue\n case 'rounding-mode-floor':\n result.roundingMode = 'floor'\n continue\n case 'rounding-mode-ceiling':\n result.roundingMode = 'ceil'\n continue\n case 'rounding-mode-down':\n result.roundingMode = 'trunc'\n continue\n case 'rounding-mode-up':\n result.roundingMode = 'expand'\n continue\n case 'rounding-mode-half-even':\n result.roundingMode = 'halfEven'\n continue\n case 'rounding-mode-half-down':\n result.roundingMode = 'halfTrunc'\n continue\n case 'rounding-mode-half-up':\n result.roundingMode = 'halfExpand'\n continue\n // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#integer-width\n case 'integer-width':\n if (token.options.length > 1) {\n throw new RangeError(\n 'integer-width stems only accept a single optional option'\n )\n }\n token.options[0].replace(\n INTEGER_WIDTH_REGEX,\n function (\n _: string,\n g1: string,\n g2: string,\n g3: string,\n g4: string,\n g5: string\n ) {\n if (g1) {\n result.minimumIntegerDigits = g2.length\n } else if (g3 && g4) {\n throw new Error(\n 'We currently do not support maximum integer digits'\n )\n } else if (g5) {\n throw new Error(\n 'We currently do not support exact integer digits'\n )\n }\n return ''\n }\n )\n continue\n }\n // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#integer-width\n if (CONCISE_INTEGER_WIDTH_REGEX.test(token.stem)) {\n result.minimumIntegerDigits = token.stem.length\n continue\n }\n if (FRACTION_PRECISION_REGEX.test(token.stem)) {\n // Precision\n // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#fraction-precision\n // precision-integer case\n if (token.options.length > 1) {\n throw new RangeError(\n 'Fraction-precision stems only accept a single optional option'\n )\n }\n token.stem.replace(\n FRACTION_PRECISION_REGEX,\n function (\n _: string,\n g1: string,\n g2: string | number,\n g3: string,\n g4: string,\n g5: string\n ) {\n // .000* case (before ICU67 it was .000+)\n if (g2 === '*') {\n result.minimumFractionDigits = g1.length\n }\n // .### case\n else if (g3 && g3[0] === '#') {\n result.maximumFractionDigits = g3.length\n }\n // .00## case\n else if (g4 && g5) {\n result.minimumFractionDigits = g4.length\n result.maximumFractionDigits = g4.length + g5.length\n } else {\n result.minimumFractionDigits = g1.length\n result.maximumFractionDigits = g1.length\n }\n return ''\n }\n )\n\n const opt = token.options[0]\n // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#trailing-zero-display\n if (opt === 'w') {\n result = {...result, trailingZeroDisplay: 'stripIfInteger'}\n } else if (opt) {\n result = {...result, ...parseSignificantPrecision(opt)}\n }\n continue\n }\n // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#significant-digits-precision\n if (SIGNIFICANT_PRECISION_REGEX.test(token.stem)) {\n result = {...result, ...parseSignificantPrecision(token.stem)}\n continue\n }\n const signOpts = parseSign(token.stem)\n if (signOpts) {\n result = {...result, ...signOpts}\n }\n const conciseScientificAndEngineeringOpts =\n parseConciseScientificAndEngineeringStem(token.stem)\n if (conciseScientificAndEngineeringOpts) {\n result = {...result, ...conciseScientificAndEngineeringOpts}\n }\n }\n return result\n}\n"],"mappings":";;;;;;AAKA,MAAM,kBACJ;;;;;;;AAQF,SAAgB,sBACd,UAC4B;CAC5B,MAAM,SAAqC,EAAE;AAC7C,UAAS,QAAQ,kBAAiB,UAAS;EACzC,MAAM,MAAM,MAAM;AAClB,UAAQ,MAAM,IAAd;GAEE,KAAK;AACH,WAAO,MAAM,QAAQ,IAAI,SAAS,QAAQ,IAAI,WAAW;AACzD;GAEF,KAAK;AACH,WAAO,OAAO,QAAQ,IAAI,YAAY;AACtC;GACF,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK,IACH,OAAM,IAAI,WACR,+DACD;GAEH,KAAK;GACL,KAAK,IACH,OAAM,IAAI,WAAW,6CAA6C;GAEpE,KAAK;GACL,KAAK;AACH,WAAO,QAAQ;KAAC;KAAW;KAAW;KAAS;KAAQ;KAAS,CAC9D,MAAM;AAER;GAEF,KAAK;GACL,KAAK,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sCACJ,yCAAyC,MAAM,KAAK;AACtD,MAAI,oCACF,UAAS;GAAC,GAAG;GAAQ,GAAG;GAAoC;;AAGhE,QAAO"} |
+68
-2
@@ -1,2 +0,68 @@ | ||
| export * from "./date-time.js"; | ||
| export * from "./number.js"; | ||
| //#region packages/icu-skeleton-parser/date-time.d.ts | ||
| /** | ||
| * Parse Date time skeleton into Intl.DateTimeFormatOptions | ||
| * Ref: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table | ||
| * @public | ||
| * @param skeleton skeleton string | ||
| */ | ||
| declare function parseDateTimeSkeleton(skeleton: string): Intl.DateTimeFormatOptions; | ||
| //#endregion | ||
| //#region packages/ecma402-abstract/types/number.d.ts | ||
| type NumberFormatNotation = "standard" | "scientific" | "engineering" | "compact"; | ||
| type RoundingPriorityType = "auto" | "morePrecision" | "lessPrecision"; | ||
| type RoundingModeType = "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven"; | ||
| type UseGroupingType = "min2" | "auto" | "always" | boolean; | ||
| interface NumberFormatDigitOptions { | ||
| minimumIntegerDigits?: number; | ||
| minimumSignificantDigits?: number; | ||
| maximumSignificantDigits?: number; | ||
| minimumFractionDigits?: number; | ||
| maximumFractionDigits?: number; | ||
| roundingPriority?: RoundingPriorityType; | ||
| roundingIncrement?: number; | ||
| roundingMode?: RoundingModeType; | ||
| trailingZeroDisplay?: TrailingZeroDisplay; | ||
| } | ||
| type NumberFormatOptionsLocaleMatcher = "lookup" | "best fit"; | ||
| type NumberFormatOptionsStyle = "decimal" | "percent" | "currency" | "unit"; | ||
| type NumberFormatOptionsCompactDisplay = "short" | "long"; | ||
| type NumberFormatOptionsCurrencyDisplay = "symbol" | "code" | "name" | "narrowSymbol"; | ||
| type NumberFormatOptionsCurrencySign = "standard" | "accounting"; | ||
| type NumberFormatOptionsNotation = NumberFormatNotation; | ||
| type NumberFormatOptionsSignDisplay = "auto" | "always" | "never" | "exceptZero" | "negative"; | ||
| type NumberFormatOptionsUnitDisplay = "long" | "short" | "narrow"; | ||
| type TrailingZeroDisplay = "auto" | "stripIfInteger"; | ||
| type NumberFormatOptions = Omit<Intl.NumberFormatOptions, "signDisplay" | "useGrouping"> & NumberFormatDigitOptions & { | ||
| localeMatcher?: NumberFormatOptionsLocaleMatcher; | ||
| style?: NumberFormatOptionsStyle; | ||
| compactDisplay?: NumberFormatOptionsCompactDisplay; | ||
| currencyDisplay?: NumberFormatOptionsCurrencyDisplay; | ||
| currencySign?: NumberFormatOptionsCurrencySign; | ||
| notation?: NumberFormatOptionsNotation; | ||
| signDisplay?: NumberFormatOptionsSignDisplay; | ||
| unit?: string; | ||
| unitDisplay?: NumberFormatOptionsUnitDisplay; | ||
| numberingSystem?: string; | ||
| trailingZeroDisplay?: TrailingZeroDisplay; | ||
| roundingPriority?: RoundingPriorityType; | ||
| roundingIncrement?: number; | ||
| roundingMode?: RoundingModeType; | ||
| useGrouping?: UseGroupingType; | ||
| }; | ||
| //#endregion | ||
| //#region packages/icu-skeleton-parser/number.d.ts | ||
| interface ExtendedNumberFormatOptions extends NumberFormatOptions { | ||
| scale?: number; | ||
| } | ||
| interface NumberSkeletonToken { | ||
| stem: string; | ||
| options: string[]; | ||
| } | ||
| declare function parseNumberSkeletonFromString(skeleton: string): NumberSkeletonToken[]; | ||
| /** | ||
| * https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options | ||
| */ | ||
| declare function parseNumberSkeleton(tokens: NumberSkeletonToken[]): ExtendedNumberFormatOptions; | ||
| //#endregion | ||
| export { ExtendedNumberFormatOptions, NumberSkeletonToken, parseDateTimeSkeleton, parseNumberSkeleton, parseNumberSkeletonFromString }; | ||
| //# sourceMappingURL=index.d.ts.map |
+380
-2
@@ -1,2 +0,380 @@ | ||
| export * from "./date-time.js"; | ||
| export * from "./number.js"; | ||
| //#region packages/icu-skeleton-parser/date-time.ts | ||
| /** | ||
| * https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table | ||
| * Credit: https://github.com/caridy/intl-datetimeformat-pattern/blob/master/index.js | ||
| * with some tweaks | ||
| */ | ||
| const DATE_TIME_REGEX = /(?:[Eec]{1,6}|G{1,5}|[Qq]{1,5}|(?:[yYur]+|U{1,5})|[ML]{1,5}|d{1,2}|D{1,3}|F{1}|[abB]{1,5}|[hkHK]{1,2}|w{1,2}|W{1}|m{1,2}|s{1,2}|[zZOvVxX]{1,4})(?=([^']*'[^']*')*[^']*$)/g; | ||
| /** | ||
| * Parse Date time skeleton into Intl.DateTimeFormatOptions | ||
| * Ref: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table | ||
| * @public | ||
| * @param skeleton skeleton string | ||
| */ | ||
| function parseDateTimeSkeleton(skeleton) { | ||
| const result = {}; | ||
| skeleton.replace(DATE_TIME_REGEX, (match) => { | ||
| const len = match.length; | ||
| switch (match[0]) { | ||
| case "G": | ||
| result.era = len === 4 ? "long" : len === 5 ? "narrow" : "short"; | ||
| break; | ||
| case "y": | ||
| result.year = len === 2 ? "2-digit" : "numeric"; | ||
| break; | ||
| case "Y": | ||
| case "u": | ||
| case "U": | ||
| case "r": throw new RangeError("`Y/u/U/r` (year) patterns are not supported, use `y` instead"); | ||
| case "q": | ||
| case "Q": throw new RangeError("`q/Q` (quarter) patterns are not supported"); | ||
| case "M": | ||
| case "L": | ||
| result.month = [ | ||
| "numeric", | ||
| "2-digit", | ||
| "short", | ||
| "long", | ||
| "narrow" | ||
| ][len - 1]; | ||
| break; | ||
| case "w": | ||
| case "W": throw new RangeError("`w/W` (week) patterns are not supported"); | ||
| case "d": | ||
| result.day = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "D": | ||
| case "F": | ||
| case "g": throw new RangeError("`D/F/g` (day) patterns are not supported, use `d` instead"); | ||
| case "E": | ||
| result.weekday = len === 4 ? "long" : len === 5 ? "narrow" : "short"; | ||
| break; | ||
| case "e": | ||
| if (len < 4) throw new RangeError("`e..eee` (weekday) patterns are not supported"); | ||
| result.weekday = [ | ||
| "short", | ||
| "long", | ||
| "narrow", | ||
| "short" | ||
| ][len - 4]; | ||
| break; | ||
| case "c": | ||
| if (len < 4) throw new RangeError("`c..ccc` (weekday) patterns are not supported"); | ||
| result.weekday = [ | ||
| "short", | ||
| "long", | ||
| "narrow", | ||
| "short" | ||
| ][len - 4]; | ||
| break; | ||
| case "a": | ||
| result.hour12 = true; | ||
| break; | ||
| case "b": | ||
| case "B": throw new RangeError("`b/B` (period) patterns are not supported, use `a` instead"); | ||
| case "h": | ||
| result.hourCycle = "h12"; | ||
| result.hour = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "H": | ||
| result.hourCycle = "h23"; | ||
| result.hour = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "K": | ||
| result.hourCycle = "h11"; | ||
| result.hour = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "k": | ||
| result.hourCycle = "h24"; | ||
| result.hour = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "j": | ||
| case "J": | ||
| case "C": throw new RangeError("`j/J/C` (hour) patterns are not supported, use `h/H/K/k` instead"); | ||
| case "m": | ||
| result.minute = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "s": | ||
| result.second = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "S": | ||
| case "A": throw new RangeError("`S/A` (second) patterns are not supported, use `s` instead"); | ||
| case "z": | ||
| result.timeZoneName = len < 4 ? "short" : "long"; | ||
| break; | ||
| case "Z": | ||
| case "O": | ||
| case "v": | ||
| case "V": | ||
| case "X": | ||
| case "x": throw new RangeError("`Z/O/v/V/X/x` (timeZone) patterns are not supported, use `z` instead"); | ||
| } | ||
| return ""; | ||
| }); | ||
| return result; | ||
| } | ||
| //#endregion | ||
| //#region packages/icu-skeleton-parser/regex.generated.ts | ||
| const WHITE_SPACE_REGEX = /[\t-\r \x85\u200E\u200F\u2028\u2029]/i; | ||
| //#endregion | ||
| //#region packages/icu-skeleton-parser/number.ts | ||
| function parseNumberSkeletonFromString(skeleton) { | ||
| if (skeleton.length === 0) throw new Error("Number skeleton cannot be empty"); | ||
| const stringTokens = skeleton.split(WHITE_SPACE_REGEX).filter((x) => x.length > 0); | ||
| const tokens = []; | ||
| for (const stringToken of stringTokens) { | ||
| let stemAndOptions = stringToken.split("/"); | ||
| if (stemAndOptions.length === 0) throw new Error("Invalid number skeleton"); | ||
| const [stem, ...options] = stemAndOptions; | ||
| for (const option of options) if (option.length === 0) throw new Error("Invalid number skeleton"); | ||
| tokens.push({ | ||
| stem, | ||
| options | ||
| }); | ||
| } | ||
| return tokens; | ||
| } | ||
| function icuUnitToEcma(unit) { | ||
| return unit.replace(/^(.*?)-/, ""); | ||
| } | ||
| const FRACTION_PRECISION_REGEX = /^\.(?:(0+)(\*)?|(#+)|(0+)(#+))$/g; | ||
| const SIGNIFICANT_PRECISION_REGEX = /^(@+)?(\+|#+)?[rs]?$/g; | ||
| const INTEGER_WIDTH_REGEX = /(\*)(0+)|(#+)(0+)|(0+)/g; | ||
| const CONCISE_INTEGER_WIDTH_REGEX = /^(0+)$/; | ||
| function parseSignificantPrecision(str) { | ||
| const result = {}; | ||
| if (str[str.length - 1] === "r") result.roundingPriority = "morePrecision"; | ||
| else if (str[str.length - 1] === "s") result.roundingPriority = "lessPrecision"; | ||
| str.replace(SIGNIFICANT_PRECISION_REGEX, function(_, g1, g2) { | ||
| if (typeof g2 !== "string") { | ||
| result.minimumSignificantDigits = g1.length; | ||
| result.maximumSignificantDigits = g1.length; | ||
| } else if (g2 === "+") result.minimumSignificantDigits = g1.length; | ||
| else if (g1[0] === "#") result.maximumSignificantDigits = g1.length; | ||
| else { | ||
| result.minimumSignificantDigits = g1.length; | ||
| result.maximumSignificantDigits = g1.length + (typeof g2 === "string" ? g2.length : 0); | ||
| } | ||
| return ""; | ||
| }); | ||
| return result; | ||
| } | ||
| function parseSign(str) { | ||
| switch (str) { | ||
| case "sign-auto": return { signDisplay: "auto" }; | ||
| case "sign-accounting": | ||
| case "()": return { currencySign: "accounting" }; | ||
| case "sign-always": | ||
| case "+!": return { signDisplay: "always" }; | ||
| case "sign-accounting-always": | ||
| case "()!": return { | ||
| signDisplay: "always", | ||
| currencySign: "accounting" | ||
| }; | ||
| case "sign-except-zero": | ||
| case "+?": return { signDisplay: "exceptZero" }; | ||
| case "sign-accounting-except-zero": | ||
| case "()?": return { | ||
| signDisplay: "exceptZero", | ||
| currencySign: "accounting" | ||
| }; | ||
| case "sign-never": | ||
| case "+_": return { signDisplay: "never" }; | ||
| } | ||
| } | ||
| function parseConciseScientificAndEngineeringStem(stem) { | ||
| let result; | ||
| if (stem[0] === "E" && stem[1] === "E") { | ||
| result = { notation: "engineering" }; | ||
| stem = stem.slice(2); | ||
| } else if (stem[0] === "E") { | ||
| result = { notation: "scientific" }; | ||
| stem = stem.slice(1); | ||
| } | ||
| if (result) { | ||
| const signDisplay = stem.slice(0, 2); | ||
| if (signDisplay === "+!") { | ||
| result.signDisplay = "always"; | ||
| stem = stem.slice(2); | ||
| } else if (signDisplay === "+?") { | ||
| result.signDisplay = "exceptZero"; | ||
| stem = stem.slice(2); | ||
| } | ||
| if (!CONCISE_INTEGER_WIDTH_REGEX.test(stem)) throw new Error("Malformed concise eng/scientific notation"); | ||
| result.minimumIntegerDigits = stem.length; | ||
| } | ||
| return result; | ||
| } | ||
| function parseNotationOptions(opt) { | ||
| const result = {}; | ||
| const signOpts = parseSign(opt); | ||
| if (signOpts) return signOpts; | ||
| return result; | ||
| } | ||
| /** | ||
| * https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options | ||
| */ | ||
| function parseNumberSkeleton(tokens) { | ||
| let result = {}; | ||
| for (const token of tokens) { | ||
| switch (token.stem) { | ||
| case "percent": | ||
| case "%": | ||
| result.style = "percent"; | ||
| continue; | ||
| case "%x100": | ||
| result.style = "percent"; | ||
| result.scale = 100; | ||
| continue; | ||
| case "currency": | ||
| result.style = "currency"; | ||
| result.currency = token.options[0]; | ||
| continue; | ||
| case "group-off": | ||
| case ",_": | ||
| result.useGrouping = false; | ||
| continue; | ||
| case "precision-integer": | ||
| case ".": | ||
| result.maximumFractionDigits = 0; | ||
| continue; | ||
| case "measure-unit": | ||
| case "unit": | ||
| result.style = "unit"; | ||
| result.unit = icuUnitToEcma(token.options[0]); | ||
| continue; | ||
| case "compact-short": | ||
| case "K": | ||
| result.notation = "compact"; | ||
| result.compactDisplay = "short"; | ||
| continue; | ||
| case "compact-long": | ||
| case "KK": | ||
| result.notation = "compact"; | ||
| result.compactDisplay = "long"; | ||
| continue; | ||
| case "scientific": | ||
| result = { | ||
| ...result, | ||
| notation: "scientific", | ||
| ...token.options.reduce((all, opt) => ({ | ||
| ...all, | ||
| ...parseNotationOptions(opt) | ||
| }), {}) | ||
| }; | ||
| continue; | ||
| case "engineering": | ||
| result = { | ||
| ...result, | ||
| notation: "engineering", | ||
| ...token.options.reduce((all, opt) => ({ | ||
| ...all, | ||
| ...parseNotationOptions(opt) | ||
| }), {}) | ||
| }; | ||
| continue; | ||
| case "notation-simple": | ||
| result.notation = "standard"; | ||
| continue; | ||
| case "unit-width-narrow": | ||
| result.currencyDisplay = "narrowSymbol"; | ||
| result.unitDisplay = "narrow"; | ||
| continue; | ||
| case "unit-width-short": | ||
| result.currencyDisplay = "code"; | ||
| result.unitDisplay = "short"; | ||
| continue; | ||
| case "unit-width-full-name": | ||
| result.currencyDisplay = "name"; | ||
| result.unitDisplay = "long"; | ||
| continue; | ||
| case "unit-width-iso-code": | ||
| result.currencyDisplay = "symbol"; | ||
| continue; | ||
| case "scale": | ||
| result.scale = parseFloat(token.options[0]); | ||
| continue; | ||
| case "rounding-mode-floor": | ||
| result.roundingMode = "floor"; | ||
| continue; | ||
| case "rounding-mode-ceiling": | ||
| result.roundingMode = "ceil"; | ||
| continue; | ||
| case "rounding-mode-down": | ||
| result.roundingMode = "trunc"; | ||
| continue; | ||
| case "rounding-mode-up": | ||
| result.roundingMode = "expand"; | ||
| continue; | ||
| case "rounding-mode-half-even": | ||
| result.roundingMode = "halfEven"; | ||
| continue; | ||
| case "rounding-mode-half-down": | ||
| result.roundingMode = "halfTrunc"; | ||
| continue; | ||
| case "rounding-mode-half-up": | ||
| result.roundingMode = "halfExpand"; | ||
| continue; | ||
| case "integer-width": | ||
| if (token.options.length > 1) throw new RangeError("integer-width stems only accept a single optional option"); | ||
| token.options[0].replace(INTEGER_WIDTH_REGEX, function(_, g1, g2, g3, g4, g5) { | ||
| if (g1) result.minimumIntegerDigits = g2.length; | ||
| else if (g3 && g4) throw new Error("We currently do not support maximum integer digits"); | ||
| else if (g5) throw new Error("We currently do not support exact integer digits"); | ||
| return ""; | ||
| }); | ||
| continue; | ||
| } | ||
| if (CONCISE_INTEGER_WIDTH_REGEX.test(token.stem)) { | ||
| result.minimumIntegerDigits = token.stem.length; | ||
| continue; | ||
| } | ||
| if (FRACTION_PRECISION_REGEX.test(token.stem)) { | ||
| if (token.options.length > 1) throw new RangeError("Fraction-precision stems only accept a single optional option"); | ||
| token.stem.replace(FRACTION_PRECISION_REGEX, function(_, g1, g2, g3, g4, g5) { | ||
| if (g2 === "*") result.minimumFractionDigits = g1.length; | ||
| else if (g3 && g3[0] === "#") result.maximumFractionDigits = g3.length; | ||
| else if (g4 && g5) { | ||
| result.minimumFractionDigits = g4.length; | ||
| result.maximumFractionDigits = g4.length + g5.length; | ||
| } else { | ||
| result.minimumFractionDigits = g1.length; | ||
| result.maximumFractionDigits = g1.length; | ||
| } | ||
| return ""; | ||
| }); | ||
| const opt = token.options[0]; | ||
| if (opt === "w") result = { | ||
| ...result, | ||
| trailingZeroDisplay: "stripIfInteger" | ||
| }; | ||
| else if (opt) result = { | ||
| ...result, | ||
| ...parseSignificantPrecision(opt) | ||
| }; | ||
| continue; | ||
| } | ||
| if (SIGNIFICANT_PRECISION_REGEX.test(token.stem)) { | ||
| result = { | ||
| ...result, | ||
| ...parseSignificantPrecision(token.stem) | ||
| }; | ||
| continue; | ||
| } | ||
| const signOpts = parseSign(token.stem); | ||
| if (signOpts) result = { | ||
| ...result, | ||
| ...signOpts | ||
| }; | ||
| const conciseScientificAndEngineeringOpts = parseConciseScientificAndEngineeringStem(token.stem); | ||
| if (conciseScientificAndEngineeringOpts) result = { | ||
| ...result, | ||
| ...conciseScientificAndEngineeringOpts | ||
| }; | ||
| } | ||
| return result; | ||
| } | ||
| //#endregion | ||
| export { parseDateTimeSkeleton, parseNumberSkeleton, parseNumberSkeletonFromString }; | ||
| //# sourceMappingURL=index.js.map |
+2
-4
| { | ||
| "name": "@formatjs/icu-skeleton-parser", | ||
| "version": "2.1.3", | ||
| "version": "2.1.4", | ||
| "license": "MIT", | ||
@@ -10,5 +10,3 @@ "type": "module", | ||
| }, | ||
| "dependencies": { | ||
| "@formatjs/ecma402-abstract": "3.2.0" | ||
| }, | ||
| "dependencies": {}, | ||
| "repository": { | ||
@@ -15,0 +13,0 @@ "type": "git", |
| /** | ||
| * Parse Date time skeleton into Intl.DateTimeFormatOptions | ||
| * Ref: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table | ||
| * @public | ||
| * @param skeleton skeleton string | ||
| */ | ||
| export declare function parseDateTimeSkeleton(skeleton: string): Intl.DateTimeFormatOptions; |
-118
| /** | ||
| * https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table | ||
| * Credit: https://github.com/caridy/intl-datetimeformat-pattern/blob/master/index.js | ||
| * with some tweaks | ||
| */ | ||
| const DATE_TIME_REGEX = /(?:[Eec]{1,6}|G{1,5}|[Qq]{1,5}|(?:[yYur]+|U{1,5})|[ML]{1,5}|d{1,2}|D{1,3}|F{1}|[abB]{1,5}|[hkHK]{1,2}|w{1,2}|W{1}|m{1,2}|s{1,2}|[zZOvVxX]{1,4})(?=([^']*'[^']*')*[^']*$)/g; | ||
| /** | ||
| * Parse Date time skeleton into Intl.DateTimeFormatOptions | ||
| * Ref: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table | ||
| * @public | ||
| * @param skeleton skeleton string | ||
| */ | ||
| export function parseDateTimeSkeleton(skeleton) { | ||
| const result = {}; | ||
| skeleton.replace(DATE_TIME_REGEX, (match) => { | ||
| const len = match.length; | ||
| switch (match[0]) { | ||
| case "G": | ||
| result.era = len === 4 ? "long" : len === 5 ? "narrow" : "short"; | ||
| break; | ||
| case "y": | ||
| result.year = len === 2 ? "2-digit" : "numeric"; | ||
| break; | ||
| case "Y": | ||
| case "u": | ||
| case "U": | ||
| case "r": throw new RangeError("`Y/u/U/r` (year) patterns are not supported, use `y` instead"); | ||
| case "q": | ||
| case "Q": throw new RangeError("`q/Q` (quarter) patterns are not supported"); | ||
| case "M": | ||
| case "L": | ||
| result.month = [ | ||
| "numeric", | ||
| "2-digit", | ||
| "short", | ||
| "long", | ||
| "narrow" | ||
| ][len - 1]; | ||
| break; | ||
| case "w": | ||
| case "W": throw new RangeError("`w/W` (week) patterns are not supported"); | ||
| case "d": | ||
| result.day = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "D": | ||
| case "F": | ||
| case "g": throw new RangeError("`D/F/g` (day) patterns are not supported, use `d` instead"); | ||
| case "E": | ||
| result.weekday = len === 4 ? "long" : len === 5 ? "narrow" : "short"; | ||
| break; | ||
| case "e": | ||
| if (len < 4) { | ||
| throw new RangeError("`e..eee` (weekday) patterns are not supported"); | ||
| } | ||
| result.weekday = [ | ||
| "short", | ||
| "long", | ||
| "narrow", | ||
| "short" | ||
| ][len - 4]; | ||
| break; | ||
| case "c": | ||
| if (len < 4) { | ||
| throw new RangeError("`c..ccc` (weekday) patterns are not supported"); | ||
| } | ||
| result.weekday = [ | ||
| "short", | ||
| "long", | ||
| "narrow", | ||
| "short" | ||
| ][len - 4]; | ||
| break; | ||
| case "a": | ||
| result.hour12 = true; | ||
| break; | ||
| case "b": | ||
| case "B": throw new RangeError("`b/B` (period) patterns are not supported, use `a` instead"); | ||
| case "h": | ||
| result.hourCycle = "h12"; | ||
| result.hour = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "H": | ||
| result.hourCycle = "h23"; | ||
| result.hour = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "K": | ||
| result.hourCycle = "h11"; | ||
| result.hour = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "k": | ||
| result.hourCycle = "h24"; | ||
| result.hour = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "j": | ||
| case "J": | ||
| case "C": throw new RangeError("`j/J/C` (hour) patterns are not supported, use `h/H/K/k` instead"); | ||
| case "m": | ||
| result.minute = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "s": | ||
| result.second = ["numeric", "2-digit"][len - 1]; | ||
| break; | ||
| case "S": | ||
| case "A": throw new RangeError("`S/A` (second) patterns are not supported, use `s` instead"); | ||
| case "z": | ||
| result.timeZoneName = len < 4 ? "short" : "long"; | ||
| break; | ||
| case "Z": | ||
| case "O": | ||
| case "v": | ||
| case "V": | ||
| case "X": | ||
| case "x": throw new RangeError("`Z/O/v/V/X/x` (timeZone) patterns are not supported, use `z` instead"); | ||
| } | ||
| return ""; | ||
| }); | ||
| return result; | ||
| } |
-13
| import type { NumberFormatOptions } from "@formatjs/ecma402-abstract"; | ||
| export interface ExtendedNumberFormatOptions extends NumberFormatOptions { | ||
| scale?: number; | ||
| } | ||
| export interface NumberSkeletonToken { | ||
| stem: string; | ||
| options: string[]; | ||
| } | ||
| export declare function parseNumberSkeletonFromString(skeleton: string): NumberSkeletonToken[]; | ||
| /** | ||
| * https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options | ||
| */ | ||
| export declare function parseNumberSkeleton(tokens: NumberSkeletonToken[]): ExtendedNumberFormatOptions; |
-301
| import { WHITE_SPACE_REGEX } from "./regex.generated.js"; | ||
| export function parseNumberSkeletonFromString(skeleton) { | ||
| if (skeleton.length === 0) { | ||
| throw new Error("Number skeleton cannot be empty"); | ||
| } | ||
| // Parse the skeleton | ||
| const stringTokens = skeleton.split(WHITE_SPACE_REGEX).filter((x) => x.length > 0); | ||
| const tokens = []; | ||
| for (const stringToken of stringTokens) { | ||
| let stemAndOptions = stringToken.split("/"); | ||
| if (stemAndOptions.length === 0) { | ||
| throw new Error("Invalid number skeleton"); | ||
| } | ||
| const [stem, ...options] = stemAndOptions; | ||
| for (const option of options) { | ||
| if (option.length === 0) { | ||
| throw new Error("Invalid number skeleton"); | ||
| } | ||
| } | ||
| tokens.push({ | ||
| stem, | ||
| options | ||
| }); | ||
| } | ||
| return tokens; | ||
| } | ||
| function icuUnitToEcma(unit) { | ||
| return unit.replace(/^(.*?)-/, ""); | ||
| } | ||
| const FRACTION_PRECISION_REGEX = /^\.(?:(0+)(\*)?|(#+)|(0+)(#+))$/g; | ||
| const SIGNIFICANT_PRECISION_REGEX = /^(@+)?(\+|#+)?[rs]?$/g; | ||
| const INTEGER_WIDTH_REGEX = /(\*)(0+)|(#+)(0+)|(0+)/g; | ||
| const CONCISE_INTEGER_WIDTH_REGEX = /^(0+)$/; | ||
| function parseSignificantPrecision(str) { | ||
| const result = {}; | ||
| if (str[str.length - 1] === "r") { | ||
| result.roundingPriority = "morePrecision"; | ||
| } else if (str[str.length - 1] === "s") { | ||
| result.roundingPriority = "lessPrecision"; | ||
| } | ||
| str.replace(SIGNIFICANT_PRECISION_REGEX, function(_, g1, g2) { | ||
| // @@@ case | ||
| if (typeof g2 !== "string") { | ||
| result.minimumSignificantDigits = g1.length; | ||
| result.maximumSignificantDigits = g1.length; | ||
| } else if (g2 === "+") { | ||
| result.minimumSignificantDigits = g1.length; | ||
| } else if (g1[0] === "#") { | ||
| result.maximumSignificantDigits = g1.length; | ||
| } else { | ||
| result.minimumSignificantDigits = g1.length; | ||
| result.maximumSignificantDigits = g1.length + (typeof g2 === "string" ? g2.length : 0); | ||
| } | ||
| return ""; | ||
| }); | ||
| return result; | ||
| } | ||
| function parseSign(str) { | ||
| switch (str) { | ||
| case "sign-auto": return { signDisplay: "auto" }; | ||
| case "sign-accounting": | ||
| case "()": return { currencySign: "accounting" }; | ||
| case "sign-always": | ||
| case "+!": return { signDisplay: "always" }; | ||
| case "sign-accounting-always": | ||
| case "()!": return { | ||
| signDisplay: "always", | ||
| currencySign: "accounting" | ||
| }; | ||
| case "sign-except-zero": | ||
| case "+?": return { signDisplay: "exceptZero" }; | ||
| case "sign-accounting-except-zero": | ||
| case "()?": return { | ||
| signDisplay: "exceptZero", | ||
| currencySign: "accounting" | ||
| }; | ||
| case "sign-never": | ||
| case "+_": return { signDisplay: "never" }; | ||
| } | ||
| } | ||
| function parseConciseScientificAndEngineeringStem(stem) { | ||
| // Engineering | ||
| let result; | ||
| if (stem[0] === "E" && stem[1] === "E") { | ||
| result = { notation: "engineering" }; | ||
| stem = stem.slice(2); | ||
| } else if (stem[0] === "E") { | ||
| result = { notation: "scientific" }; | ||
| stem = stem.slice(1); | ||
| } | ||
| if (result) { | ||
| const signDisplay = stem.slice(0, 2); | ||
| if (signDisplay === "+!") { | ||
| result.signDisplay = "always"; | ||
| stem = stem.slice(2); | ||
| } else if (signDisplay === "+?") { | ||
| result.signDisplay = "exceptZero"; | ||
| stem = stem.slice(2); | ||
| } | ||
| if (!CONCISE_INTEGER_WIDTH_REGEX.test(stem)) { | ||
| throw new Error("Malformed concise eng/scientific notation"); | ||
| } | ||
| result.minimumIntegerDigits = stem.length; | ||
| } | ||
| return result; | ||
| } | ||
| function parseNotationOptions(opt) { | ||
| const result = {}; | ||
| const signOpts = parseSign(opt); | ||
| if (signOpts) { | ||
| return signOpts; | ||
| } | ||
| return result; | ||
| } | ||
| /** | ||
| * https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options | ||
| */ | ||
| export function parseNumberSkeleton(tokens) { | ||
| let result = {}; | ||
| for (const token of tokens) { | ||
| switch (token.stem) { | ||
| case "percent": | ||
| case "%": | ||
| result.style = "percent"; | ||
| continue; | ||
| case "%x100": | ||
| result.style = "percent"; | ||
| result.scale = 100; | ||
| continue; | ||
| case "currency": | ||
| result.style = "currency"; | ||
| result.currency = token.options[0]; | ||
| continue; | ||
| case "group-off": | ||
| case ",_": | ||
| result.useGrouping = false; | ||
| continue; | ||
| case "precision-integer": | ||
| case ".": | ||
| result.maximumFractionDigits = 0; | ||
| continue; | ||
| case "measure-unit": | ||
| case "unit": | ||
| result.style = "unit"; | ||
| result.unit = icuUnitToEcma(token.options[0]); | ||
| continue; | ||
| case "compact-short": | ||
| case "K": | ||
| result.notation = "compact"; | ||
| result.compactDisplay = "short"; | ||
| continue; | ||
| case "compact-long": | ||
| case "KK": | ||
| result.notation = "compact"; | ||
| result.compactDisplay = "long"; | ||
| continue; | ||
| case "scientific": | ||
| result = { | ||
| ...result, | ||
| notation: "scientific", | ||
| ...token.options.reduce((all, opt) => ({ | ||
| ...all, | ||
| ...parseNotationOptions(opt) | ||
| }), {}) | ||
| }; | ||
| continue; | ||
| case "engineering": | ||
| result = { | ||
| ...result, | ||
| notation: "engineering", | ||
| ...token.options.reduce((all, opt) => ({ | ||
| ...all, | ||
| ...parseNotationOptions(opt) | ||
| }), {}) | ||
| }; | ||
| continue; | ||
| case "notation-simple": | ||
| result.notation = "standard"; | ||
| continue; | ||
| case "unit-width-narrow": | ||
| result.currencyDisplay = "narrowSymbol"; | ||
| result.unitDisplay = "narrow"; | ||
| continue; | ||
| case "unit-width-short": | ||
| result.currencyDisplay = "code"; | ||
| result.unitDisplay = "short"; | ||
| continue; | ||
| case "unit-width-full-name": | ||
| result.currencyDisplay = "name"; | ||
| result.unitDisplay = "long"; | ||
| continue; | ||
| case "unit-width-iso-code": | ||
| result.currencyDisplay = "symbol"; | ||
| continue; | ||
| case "scale": | ||
| result.scale = parseFloat(token.options[0]); | ||
| continue; | ||
| case "rounding-mode-floor": | ||
| result.roundingMode = "floor"; | ||
| continue; | ||
| case "rounding-mode-ceiling": | ||
| result.roundingMode = "ceil"; | ||
| continue; | ||
| case "rounding-mode-down": | ||
| result.roundingMode = "trunc"; | ||
| continue; | ||
| case "rounding-mode-up": | ||
| result.roundingMode = "expand"; | ||
| continue; | ||
| case "rounding-mode-half-even": | ||
| result.roundingMode = "halfEven"; | ||
| continue; | ||
| case "rounding-mode-half-down": | ||
| result.roundingMode = "halfTrunc"; | ||
| continue; | ||
| case "rounding-mode-half-up": | ||
| result.roundingMode = "halfExpand"; | ||
| continue; | ||
| case "integer-width": | ||
| if (token.options.length > 1) { | ||
| throw new RangeError("integer-width stems only accept a single optional option"); | ||
| } | ||
| token.options[0].replace(INTEGER_WIDTH_REGEX, function(_, g1, g2, g3, g4, g5) { | ||
| if (g1) { | ||
| result.minimumIntegerDigits = g2.length; | ||
| } else if (g3 && g4) { | ||
| throw new Error("We currently do not support maximum integer digits"); | ||
| } else if (g5) { | ||
| throw new Error("We currently do not support exact integer digits"); | ||
| } | ||
| return ""; | ||
| }); | ||
| continue; | ||
| } | ||
| // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#integer-width | ||
| if (CONCISE_INTEGER_WIDTH_REGEX.test(token.stem)) { | ||
| result.minimumIntegerDigits = token.stem.length; | ||
| continue; | ||
| } | ||
| if (FRACTION_PRECISION_REGEX.test(token.stem)) { | ||
| // Precision | ||
| // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#fraction-precision | ||
| // precision-integer case | ||
| if (token.options.length > 1) { | ||
| throw new RangeError("Fraction-precision stems only accept a single optional option"); | ||
| } | ||
| token.stem.replace(FRACTION_PRECISION_REGEX, function(_, g1, g2, g3, g4, g5) { | ||
| // .000* case (before ICU67 it was .000+) | ||
| if (g2 === "*") { | ||
| result.minimumFractionDigits = g1.length; | ||
| } else if (g3 && g3[0] === "#") { | ||
| result.maximumFractionDigits = g3.length; | ||
| } else if (g4 && g5) { | ||
| result.minimumFractionDigits = g4.length; | ||
| result.maximumFractionDigits = g4.length + g5.length; | ||
| } else { | ||
| result.minimumFractionDigits = g1.length; | ||
| result.maximumFractionDigits = g1.length; | ||
| } | ||
| return ""; | ||
| }); | ||
| const opt = token.options[0]; | ||
| // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#trailing-zero-display | ||
| if (opt === "w") { | ||
| result = { | ||
| ...result, | ||
| trailingZeroDisplay: "stripIfInteger" | ||
| }; | ||
| } else if (opt) { | ||
| result = { | ||
| ...result, | ||
| ...parseSignificantPrecision(opt) | ||
| }; | ||
| } | ||
| continue; | ||
| } | ||
| // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#significant-digits-precision | ||
| if (SIGNIFICANT_PRECISION_REGEX.test(token.stem)) { | ||
| result = { | ||
| ...result, | ||
| ...parseSignificantPrecision(token.stem) | ||
| }; | ||
| continue; | ||
| } | ||
| const signOpts = parseSign(token.stem); | ||
| if (signOpts) { | ||
| result = { | ||
| ...result, | ||
| ...signOpts | ||
| }; | ||
| } | ||
| const conciseScientificAndEngineeringOpts = parseConciseScientificAndEngineeringStem(token.stem); | ||
| if (conciseScientificAndEngineeringOpts) { | ||
| result = { | ||
| ...result, | ||
| ...conciseScientificAndEngineeringOpts | ||
| }; | ||
| } | ||
| } | ||
| return result; | ||
| } |
| export declare const WHITE_SPACE_REGEX: RegExp; |
| // @generated from regex-gen.ts | ||
| export const WHITE_SPACE_REGEX = /[\t-\r \x85\u200E\u200F\u2028\u2029]/i; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
38448
163%0
-100%6
-45.45%445
-0.22%1
Infinity%- Removed
- Removed
- Removed
- Removed
- Removed