Skip to content
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

toMillis: Allow width formatting without separators #545

Merged
merged 13 commits into from
Nov 2, 2021
16 changes: 14 additions & 2 deletions src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ const dateTime = (function () {
def.integerFormat.mandatoryDigits = def.width.min;
}
}
if (def.component === 'Y') {
if ('YMD'.indexOf(def.component) !== -1) {
// §9.8.4.4
def.n = -1;
if (def.width && def.width.max !== undefined) {
Expand Down Expand Up @@ -983,6 +983,7 @@ const dateTime = (function () {
return offsetHours * 60 + offsetMinutes;
};
} else if (part.integerFormat) {
part.integerFormat.n = part.n;
res = generateRegex(part.integerFormat);
} else {
// must be a month or day name
Expand Down Expand Up @@ -1027,6 +1028,17 @@ const dateTime = (function () {
} else { // type === 'integer'
matcher.type = 'integer';
const isUpper = formatSpec.case === tcase.UPPER;
let occurrences;
if(formatSpec.n && formatSpec.n > 0){
if(formatSpec.optionalDigits === 0){
occurrences = `{${formatSpec.n}}`;
} else {
occurrences = `{${formatSpec.n - formatSpec.optionalDigits},${formatSpec.n}}`;
}
} else {
occurrences = '+';
}

switch (formatSpec.primary) {
case formats.LETTERS:
matcher.regex = isUpper ? '[A-Z]+' : '[a-z]+';
Expand All @@ -1047,7 +1059,7 @@ const dateTime = (function () {
};
break;
case formats.DECIMAL:
matcher.regex = '[0-9]+';
matcher.regex = `[0-9]${occurrences}`;
if (formatSpec.ordinal) {
// ordinals
matcher.regex += '(?:th|st|nd|rd)';
Expand Down
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-tomillis/case010.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$toMillis(\"201802\", \"[Y0001][M01]\")",
"dataset": "dataset5",
"bindings": {},
"result": 1517443200000
}
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-tomillis/case011.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$toMillis(\"201802\", \"[Y,*-4][M01]\")",
"dataset": "dataset5",
"bindings": {},
"result": 1517443200000
}
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-tomillis/case012.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$toMillis(\"20180205\", \"[Y0001][M01][D01]\")",
"dataset": "dataset5",
"bindings": {},
"result": 1517788800000
}