Skip to content

Commit 4ef4ade

Browse files
committed
Fixed bug in parsing
1 parent 50fdd35 commit 4ef4ade

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

dist/fecha.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/fecha.umd.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,14 @@
250250
// Make literals inactive by replacing them with ??
251251
mask = mask.replace(literal, function($0, $1) {
252252
literals.push($1);
253-
return '??';
253+
return '@@@';
254254
});
255255
// Apply formatting rules
256256
mask = mask.replace(token, function ($0) {
257257
return $0 in formatFlags ? formatFlags[$0](dateObj, i18n) : $0.slice(1, $0.length - 1);
258258
});
259259
// Inline literal values back into the formatted value
260-
return mask.replace(/\?\?/g, function() {
260+
return mask.replace(/@@@/g, function() {
261261
return literals.shift();
262262
});
263263
};
@@ -286,6 +286,11 @@
286286

287287
var dateInfo = {};
288288
var parseInfo = [];
289+
var literals = [];
290+
format = format.replace(literal, function($0, $1) {
291+
literals.push($1);
292+
return '@@@';
293+
});
289294
var newFormat = regexEscape(format).replace(token, function ($0) {
290295
if (parseFlags[$0]) {
291296
var info = parseFlags[$0];
@@ -295,6 +300,9 @@
295300

296301
return $0;
297302
});
303+
newFormat = newFormat.replace(/@@@/g, function() {
304+
return literals.shift();
305+
});
298306
var matches = dateStr.match(new RegExp(newFormat, 'i'));
299307
if (!matches) {
300308
return null;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fecha",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
44
"description": "Date formatting and parsing",
55
"main": "lib/fecha.umd.js",
66
"module": "src/fecha.js",

src/fecha.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@ fecha.format = function (dateObj, mask, i18nSettings) {
244244
// Make literals inactive by replacing them with ??
245245
mask = mask.replace(literal, function($0, $1) {
246246
literals.push($1);
247-
return '??';
247+
return '@@@';
248248
});
249249
// Apply formatting rules
250250
mask = mask.replace(token, function ($0) {
251251
return $0 in formatFlags ? formatFlags[$0](dateObj, i18n) : $0.slice(1, $0.length - 1);
252252
});
253253
// Inline literal values back into the formatted value
254-
return mask.replace(/\?\?/g, function() {
254+
return mask.replace(/@@@/g, function() {
255255
return literals.shift();
256256
});
257257
};
@@ -280,6 +280,11 @@ fecha.parse = function (dateStr, format, i18nSettings) {
280280

281281
var dateInfo = {};
282282
var parseInfo = [];
283+
var literals = [];
284+
format = format.replace(literal, function($0, $1) {
285+
literals.push($1);
286+
return '@@@';
287+
});
283288
var newFormat = regexEscape(format).replace(token, function ($0) {
284289
if (parseFlags[$0]) {
285290
var info = parseFlags[$0];
@@ -289,6 +294,9 @@ fecha.parse = function (dateStr, format, i18nSettings) {
289294

290295
return $0;
291296
});
297+
newFormat = newFormat.replace(/@@@/g, function() {
298+
return literals.shift();
299+
});
292300
var matches = dateStr.match(new RegExp(newFormat, 'i'));
293301
if (!matches) {
294302
return null;

test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ testParse('UTC timezone offset', '09:20:31 GMT-0000 (UTC)', 'HH:mm:ss ZZ', new D
5050
testParse('UTC timezone offset without explicit offset', '09:20:31Z', 'HH:mm:ssZZ', new Date(year, 0, 1, 9, 20, 31));
5151
testParse('UTC timezone offset without GMT', '09:20:31 -0000 (UTC)', 'HH:mm:ss ZZ', new Date(Date.UTC(year, 0, 1, 9, 20, 31)));
5252
testParse('invalid date', 'hello', 'HH:mm:ss ZZ', null);
53+
testParse('formatted date with brackets', '2019-04-12T15:53', 'YYYY-MM-DD[T]HH:mm', new Date(2019, 3, 12, 15, 53))
5354
test('i18n month short parse', function() {
5455
assert.equal(+fecha.parse('def 3rd, 2021', 'MMM Do, YYYY', {
5556
monthNamesShort: ['Adk', 'Def', 'Sdfs', 'Sdf', 'Sdh', 'Tre', 'Iis', 'Swd', 'Ews', 'Sdf', 'Qaas', 'Ier']

0 commit comments

Comments
 (0)