@formatjs/icu-messageformat-parser 2.0.19 → 2.1.3
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/date-time-pattern-generator.d.ts +9 -0
- package/date-time-pattern-generator.d.ts.map +1 -0
- package/date-time-pattern-generator.js +87 -0
- package/lib/date-time-pattern-generator.d.ts +9 -0
- package/lib/date-time-pattern-generator.d.ts.map +1 -0
- package/lib/date-time-pattern-generator.js +83 -0
- package/lib/parser.d.ts +5 -0
- package/lib/parser.d.ts.map +1 -1
- package/lib/parser.js +11 -2
- package/lib/time-data.generated.d.ts +2 -0
- package/lib/time-data.generated.d.ts.map +1 -0
- package/lib/time-data.generated.js +1339 -0
- package/package.json +4 -4
- package/parser.d.ts +5 -0
- package/parser.d.ts.map +1 -1
- package/parser.js +11 -2
- package/time-data.generated.d.ts +2 -0
- package/time-data.generated.d.ts.map +1 -0
- package/time-data.generated.js +1342 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the best matching date time pattern if a date time skeleton
|
|
3
|
+
* pattern is provided with a locale. Follows the Unicode specification:
|
|
4
|
+
* https://www.unicode.org/reports/tr35/tr35-dates.html#table-mapping-requested-time-skeletons-to-patterns
|
|
5
|
+
* @param skeleton date time skeleton pattern that possibly includes j, J or C
|
|
6
|
+
* @param locale
|
|
7
|
+
*/
|
|
8
|
+
export declare function getBestPattern(skeleton: string, locale: Intl.Locale): string;
|
|
9
|
+
//# sourceMappingURL=date-time-pattern-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-time-pattern-generator.d.ts","sourceRoot":"","sources":["../../../../../packages/icu-messageformat-parser/date-time-pattern-generator.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,UAsCnE"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBestPattern = void 0;
|
|
4
|
+
var time_data_generated_1 = require("./time-data.generated");
|
|
5
|
+
/**
|
|
6
|
+
* Returns the best matching date time pattern if a date time skeleton
|
|
7
|
+
* pattern is provided with a locale. Follows the Unicode specification:
|
|
8
|
+
* https://www.unicode.org/reports/tr35/tr35-dates.html#table-mapping-requested-time-skeletons-to-patterns
|
|
9
|
+
* @param skeleton date time skeleton pattern that possibly includes j, J or C
|
|
10
|
+
* @param locale
|
|
11
|
+
*/
|
|
12
|
+
function getBestPattern(skeleton, locale) {
|
|
13
|
+
var skeletonCopy = '';
|
|
14
|
+
for (var patternPos = 0; patternPos < skeleton.length; patternPos++) {
|
|
15
|
+
var patternChar = skeleton.charAt(patternPos);
|
|
16
|
+
if (patternChar === 'j') {
|
|
17
|
+
var extraLength = 0;
|
|
18
|
+
while (patternPos + 1 < skeleton.length &&
|
|
19
|
+
skeleton.charAt(patternPos + 1) === patternChar) {
|
|
20
|
+
extraLength++;
|
|
21
|
+
patternPos++;
|
|
22
|
+
}
|
|
23
|
+
var hourLen = 1 + (extraLength & 1);
|
|
24
|
+
var dayPeriodLen = extraLength < 2 ? 1 : 3 + (extraLength >> 1);
|
|
25
|
+
var dayPeriodChar = 'a';
|
|
26
|
+
var hourChar = getDefaultHourSymbolFromLocale(locale);
|
|
27
|
+
if (hourChar == 'H' || hourChar == 'k') {
|
|
28
|
+
dayPeriodLen = 0;
|
|
29
|
+
}
|
|
30
|
+
while (dayPeriodLen-- > 0) {
|
|
31
|
+
skeletonCopy += dayPeriodChar;
|
|
32
|
+
}
|
|
33
|
+
while (hourLen-- > 0) {
|
|
34
|
+
skeletonCopy = hourChar + skeletonCopy;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else if (patternChar === 'J') {
|
|
38
|
+
skeletonCopy += 'H';
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
skeletonCopy += patternChar;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return skeletonCopy;
|
|
45
|
+
}
|
|
46
|
+
exports.getBestPattern = getBestPattern;
|
|
47
|
+
/**
|
|
48
|
+
* Maps the [hour cycle type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle)
|
|
49
|
+
* of the given `locale` to the corresponding time pattern.
|
|
50
|
+
* @param locale
|
|
51
|
+
*/
|
|
52
|
+
function getDefaultHourSymbolFromLocale(locale) {
|
|
53
|
+
var hourCycle = locale.hourCycle;
|
|
54
|
+
if (hourCycle === undefined &&
|
|
55
|
+
// @ts-ignore hourCycle(s) is not identified yet
|
|
56
|
+
locale.hourCycles &&
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
locale.hourCycles.length) {
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
hourCycle = locale.hourCycles[0];
|
|
61
|
+
}
|
|
62
|
+
if (hourCycle) {
|
|
63
|
+
switch (hourCycle) {
|
|
64
|
+
case 'h24':
|
|
65
|
+
return 'k';
|
|
66
|
+
case 'h23':
|
|
67
|
+
return 'H';
|
|
68
|
+
case 'h12':
|
|
69
|
+
return 'h';
|
|
70
|
+
case 'h11':
|
|
71
|
+
return 'K';
|
|
72
|
+
default:
|
|
73
|
+
throw new Error('Invalid hourCycle');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// TODO: Once hourCycle is fully supported remove the following with data generation
|
|
77
|
+
var languageTag = locale.language;
|
|
78
|
+
var regionTag;
|
|
79
|
+
if (languageTag !== 'root') {
|
|
80
|
+
regionTag = locale.maximize().region;
|
|
81
|
+
}
|
|
82
|
+
var hourCycles = time_data_generated_1.timeData[regionTag || ''] ||
|
|
83
|
+
time_data_generated_1.timeData[languageTag || ''] ||
|
|
84
|
+
time_data_generated_1.timeData["".concat(languageTag, "-001")] ||
|
|
85
|
+
time_data_generated_1.timeData['001'];
|
|
86
|
+
return hourCycles[0];
|
|
87
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the best matching date time pattern if a date time skeleton
|
|
3
|
+
* pattern is provided with a locale. Follows the Unicode specification:
|
|
4
|
+
* https://www.unicode.org/reports/tr35/tr35-dates.html#table-mapping-requested-time-skeletons-to-patterns
|
|
5
|
+
* @param skeleton date time skeleton pattern that possibly includes j, J or C
|
|
6
|
+
* @param locale
|
|
7
|
+
*/
|
|
8
|
+
export declare function getBestPattern(skeleton: string, locale: Intl.Locale): string;
|
|
9
|
+
//# sourceMappingURL=date-time-pattern-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-time-pattern-generator.d.ts","sourceRoot":"","sources":["../../../../../../packages/icu-messageformat-parser/date-time-pattern-generator.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,UAsCnE"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { timeData } from './time-data.generated';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the best matching date time pattern if a date time skeleton
|
|
4
|
+
* pattern is provided with a locale. Follows the Unicode specification:
|
|
5
|
+
* https://www.unicode.org/reports/tr35/tr35-dates.html#table-mapping-requested-time-skeletons-to-patterns
|
|
6
|
+
* @param skeleton date time skeleton pattern that possibly includes j, J or C
|
|
7
|
+
* @param locale
|
|
8
|
+
*/
|
|
9
|
+
export function getBestPattern(skeleton, locale) {
|
|
10
|
+
var skeletonCopy = '';
|
|
11
|
+
for (var patternPos = 0; patternPos < skeleton.length; patternPos++) {
|
|
12
|
+
var patternChar = skeleton.charAt(patternPos);
|
|
13
|
+
if (patternChar === 'j') {
|
|
14
|
+
var extraLength = 0;
|
|
15
|
+
while (patternPos + 1 < skeleton.length &&
|
|
16
|
+
skeleton.charAt(patternPos + 1) === patternChar) {
|
|
17
|
+
extraLength++;
|
|
18
|
+
patternPos++;
|
|
19
|
+
}
|
|
20
|
+
var hourLen = 1 + (extraLength & 1);
|
|
21
|
+
var dayPeriodLen = extraLength < 2 ? 1 : 3 + (extraLength >> 1);
|
|
22
|
+
var dayPeriodChar = 'a';
|
|
23
|
+
var hourChar = getDefaultHourSymbolFromLocale(locale);
|
|
24
|
+
if (hourChar == 'H' || hourChar == 'k') {
|
|
25
|
+
dayPeriodLen = 0;
|
|
26
|
+
}
|
|
27
|
+
while (dayPeriodLen-- > 0) {
|
|
28
|
+
skeletonCopy += dayPeriodChar;
|
|
29
|
+
}
|
|
30
|
+
while (hourLen-- > 0) {
|
|
31
|
+
skeletonCopy = hourChar + skeletonCopy;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else if (patternChar === 'J') {
|
|
35
|
+
skeletonCopy += 'H';
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
skeletonCopy += patternChar;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return skeletonCopy;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Maps the [hour cycle type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle)
|
|
45
|
+
* of the given `locale` to the corresponding time pattern.
|
|
46
|
+
* @param locale
|
|
47
|
+
*/
|
|
48
|
+
function getDefaultHourSymbolFromLocale(locale) {
|
|
49
|
+
var hourCycle = locale.hourCycle;
|
|
50
|
+
if (hourCycle === undefined &&
|
|
51
|
+
// @ts-ignore hourCycle(s) is not identified yet
|
|
52
|
+
locale.hourCycles &&
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
locale.hourCycles.length) {
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
hourCycle = locale.hourCycles[0];
|
|
57
|
+
}
|
|
58
|
+
if (hourCycle) {
|
|
59
|
+
switch (hourCycle) {
|
|
60
|
+
case 'h24':
|
|
61
|
+
return 'k';
|
|
62
|
+
case 'h23':
|
|
63
|
+
return 'H';
|
|
64
|
+
case 'h12':
|
|
65
|
+
return 'h';
|
|
66
|
+
case 'h11':
|
|
67
|
+
return 'K';
|
|
68
|
+
default:
|
|
69
|
+
throw new Error('Invalid hourCycle');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// TODO: Once hourCycle is fully supported remove the following with data generation
|
|
73
|
+
var languageTag = locale.language;
|
|
74
|
+
var regionTag;
|
|
75
|
+
if (languageTag !== 'root') {
|
|
76
|
+
regionTag = locale.maximize().region;
|
|
77
|
+
}
|
|
78
|
+
var hourCycles = timeData[regionTag || ''] ||
|
|
79
|
+
timeData[languageTag || ''] ||
|
|
80
|
+
timeData["".concat(languageTag, "-001")] ||
|
|
81
|
+
timeData['001'];
|
|
82
|
+
return hourCycles[0];
|
|
83
|
+
}
|
package/lib/parser.d.ts
CHANGED
|
@@ -30,6 +30,10 @@ export interface ParserOptions {
|
|
|
30
30
|
* Default is false
|
|
31
31
|
*/
|
|
32
32
|
captureLocation?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Instance of Intl.Locale to resolve locale-dependent skeleton
|
|
35
|
+
*/
|
|
36
|
+
locale?: Intl.Locale;
|
|
33
37
|
}
|
|
34
38
|
export declare type Result<T, E> = {
|
|
35
39
|
val: T;
|
|
@@ -41,6 +45,7 @@ export declare type Result<T, E> = {
|
|
|
41
45
|
export declare class Parser {
|
|
42
46
|
private message;
|
|
43
47
|
private position;
|
|
48
|
+
private locale?;
|
|
44
49
|
private ignoreTag;
|
|
45
50
|
private requiresOtherClause;
|
|
46
51
|
private shouldParseSkeletons?;
|
package/lib/parser.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../../../../../packages/icu-messageformat-parser/parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,WAAW,EAAC,MAAM,SAAS,CAAA;AAC9C,OAAO,EAIL,oBAAoB,EAMrB,MAAM,SAAS,CAAA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OAiBG;IACH,OAAO,CAAC,QAAQ;IAkFhB;;OAEG;IACH,OAAO,CAAC,YAAY;IAUpB,OAAO,CAAC,YAAY;IAuCpB,wBAAwB,IAAI,MAAM,GAAG,IAAI;IAczC;;;;OAIG;IACH,OAAO,CAAC,aAAa;IAsDrB,OAAO,CAAC,gBAAgB;IAuBxB,OAAO,CAAC,aAAa;IAsFrB;;;OAGG;IACH,OAAO,CAAC,yBAAyB;IAejC,OAAO,CAAC,oBAAoB;IAyO5B,OAAO,CAAC,qBAAqB;IAe7B;;OAEG;IACH,OAAO,CAAC,6BAA6B;IAiDrC,OAAO,CAAC,6BAA6B;IAwBrC;;;;;;;;;OASG;IACH,OAAO,CAAC,6BAA6B;IA6GrC,OAAO,CAAC,sBAAsB;IAuC9B,OAAO,CAAC,MAAM;IAId,OAAO,CAAC,KAAK;IAIb,OAAO,CAAC,aAAa;IASrB;;;OAGG;IACH,OAAO,CAAC,IAAI;IAYZ,OAAO,CAAC,KAAK;IAcb,oDAAoD;IACpD,OAAO,CAAC,IAAI;IAgBZ;;;;;OAKG;IACH,OAAO,CAAC,MAAM;IAUd;;;OAGG;IACH,OAAO,CAAC,SAAS;IAYjB;;;OAGG;IACH,OAAO,CAAC,MAAM;IA0Bd,sFAAsF;IACtF,OAAO,CAAC,SAAS;IAMjB;;;OAGG;IACH,OAAO,CAAC,IAAI;CASb"}
|
package/lib/parser.js
CHANGED
|
@@ -4,6 +4,7 @@ import { ErrorKind } from './error';
|
|
|
4
4
|
import { SKELETON_TYPE, TYPE, } from './types';
|
|
5
5
|
import { SPACE_SEPARATOR_REGEX } from './regex.generated';
|
|
6
6
|
import { parseNumberSkeleton, parseNumberSkeletonFromString, parseDateTimeSkeleton, } from '@formatjs/icu-skeleton-parser';
|
|
7
|
+
import { getBestPattern } from './date-time-pattern-generator';
|
|
7
8
|
var SPACE_SEPARATOR_START_REGEX = new RegExp("^".concat(SPACE_SEPARATOR_REGEX.source, "*"));
|
|
8
9
|
var SPACE_SEPARATOR_END_REGEX = new RegExp("".concat(SPACE_SEPARATOR_REGEX.source, "*$"));
|
|
9
10
|
function createLocation(start, end) {
|
|
@@ -162,6 +163,7 @@ var Parser = /** @class */ (function () {
|
|
|
162
163
|
this.message = message;
|
|
163
164
|
this.position = { offset: 0, line: 1, column: 1 };
|
|
164
165
|
this.ignoreTag = !!options.ignoreTag;
|
|
166
|
+
this.locale = options.locale;
|
|
165
167
|
this.requiresOtherClause = !!options.requiresOtherClause;
|
|
166
168
|
this.shouldParseSkeletons = !!options.shouldParseSkeletons;
|
|
167
169
|
}
|
|
@@ -537,12 +539,19 @@ var Parser = /** @class */ (function () {
|
|
|
537
539
|
if (skeleton.length === 0) {
|
|
538
540
|
return this.error(ErrorKind.EXPECT_DATE_TIME_SKELETON, location_1);
|
|
539
541
|
}
|
|
542
|
+
var dateTimePattern = skeleton;
|
|
543
|
+
// Get "best match" pattern only if locale is passed, if not, let it
|
|
544
|
+
// pass as-is where `parseDateTimeSkeleton()` will throw an error
|
|
545
|
+
// for unsupported patterns.
|
|
546
|
+
if (this.locale) {
|
|
547
|
+
dateTimePattern = getBestPattern(skeleton, this.locale);
|
|
548
|
+
}
|
|
540
549
|
var style = {
|
|
541
550
|
type: SKELETON_TYPE.dateTime,
|
|
542
|
-
pattern:
|
|
551
|
+
pattern: dateTimePattern,
|
|
543
552
|
location: styleAndLocation.styleLocation,
|
|
544
553
|
parsedOptions: this.shouldParseSkeletons
|
|
545
|
-
? parseDateTimeSkeleton(
|
|
554
|
+
? parseDateTimeSkeleton(dateTimePattern)
|
|
546
555
|
: {},
|
|
547
556
|
};
|
|
548
557
|
var type = argType === 'date' ? TYPE.date : TYPE.time;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time-data.generated.d.ts","sourceRoot":"","sources":["../../../../../../packages/icu-messageformat-parser/time-data.generated.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAwzC7C,CAAC"}
|