Skip to content

Commit

Permalink
make dots in month/weekday names optional
Browse files Browse the repository at this point in the history
  • Loading branch information
icambron committed Oct 26, 2017
1 parent 0f1f2ce commit d1c67b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/impl/tokenParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ function intUnit(regex, post = i => i) {
return { regex, deser: ([s]) => post(parseInt(s, 10)) };
}

function fixListRegex(s) {
// make dots optional and also make them literal
return s.replace(/\./, '\\.?');
}

function stripInsensitivities(s) {
return s.replace(/\./, '').toLowerCase();
}

function oneOf(strings, startIndex) {
if (strings === null) {
return null;
} else {
return {
regex: RegExp(strings.join('|')),
deser: ([s]) => strings.findIndex(i => s.toLowerCase() === i.toLowerCase()) + startIndex
regex: RegExp(strings.map(fixListRegex).join('|')),
deser: ([s]) =>
strings.findIndex(i => stripInsensitivities(s) === stripInsensitivities(i)) + startIndex
};
}
}
Expand Down Expand Up @@ -170,9 +180,9 @@ function match(input, regex, handlers) {
matchIndex += groups;
}
}
return all;
return [matches, all];
} else {
return {};
return [matches, {}];
}
}

Expand Down Expand Up @@ -256,10 +266,10 @@ export class TokenParser {
return { input, tokens, invalidReason: disqualifyingUnit.invalidReason };
} else {
const [regex, handlers] = buildRegex(units),
matches = match(input, RegExp(regex, 'i'), handlers),
[rawMatches, matches] = match(input, RegExp(regex, 'i'), handlers),
[result, zone] = matches ? dateTimeFromMatches(matches) : [null, null];

return { input, tokens, regex, matches, result, zone };
return { input, tokens, regex, rawMatches, matches, result, zone };
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/datetime/tokenParse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ test('DateTime.fromString() parses month names', () => {
expect(i.day).toBe(25);
});

test('DateTime.fromString() makes trailing periods in month names optional', () => {
const i = DateTime.fromString('janv 25 1982', 'LLL dd yyyy', {
locale: 'fr'
});
expect(i.year).toBe(1982);
expect(i.month).toBe(1);
expect(i.day).toBe(25);
});

test('DateTime.fromString() does not match arbitrary stuff with those periods', () => {
const i = DateTime.fromString('janvQ 25 1982', 'LLL dd yyyy', {
locale: 'fr'
});
expect(i.isValid).toBe(false);
});

test('DateTime.fromString() uses case-insensitive matching', () => {
const i = DateTime.fromString('Janv. 25 1982', 'LLL dd yyyy', {
locale: 'fr'
Expand Down

0 comments on commit d1c67b1

Please sign in to comment.