moment-parseformat
Advanced tools
Comparing version 0.0.10 to 0.0.11
{ | ||
"name": "moment-parseformat", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Gregor Martynus <gregor@martynus.net>" |
@@ -31,5 +31,3 @@ 'use strict'; | ||
var regexFirstSecondThirdFourth = /(\d+)(st|nd|rd|th)\b/i; | ||
var regexBigEndian = /(\d{2,4})\-(\d{1,2})\-(\d{1,2})/; | ||
var regexLittleEndian = /(\d{1,2})\.(\d{1,2})\.(\d{2,4})/; | ||
var regexMiddleEndian = /(\d{1,2})\/(\d{1,2})\/(\d{2,4})/; | ||
var regexEndian = /(\d{1,4})([\/\.\-])(\d{1,2})[\/\.\-](\d{1,4})/; | ||
@@ -49,5 +47,16 @@ var amOrPm = '('+[amDesignator,pmDesignator].join('|')+')'; | ||
function parseDateFormat(dateString) { | ||
// option defaults | ||
var defaultOrder = { | ||
'/': 'MDY', | ||
'.': 'DMY', | ||
'-': 'YMD' | ||
}; | ||
function parseDateFormat(dateString, options) { | ||
var format = dateString; | ||
// default options | ||
options = options || {}; | ||
options.preferedOrder = options.preferedOrder || defaultOrder; | ||
// escape filling words | ||
@@ -75,13 +84,5 @@ format = format.replace(regexFillingWords, '[$1]'); | ||
// DAY - MONTH - YEAR ORDER | ||
// replace endians, like 8/20/2010, 20.8.2010 or 2010-8-20 | ||
format = format.replace( regexEndian, replaceEndian.bind(null, options)); | ||
// 03-04-05 ☛ yy- | ||
format = format.replace( regexBigEndian, replaceBigEndian); | ||
// Little-endian (day, month, year), e.g. 05.04.03 | ||
format = format.replace(regexLittleEndian, replaceLittleEndian); | ||
// Middle-endian (month, day, year), e.g. 04/05/03 | ||
format = format.replace( regexMiddleEndian, replaceMiddleEndian); | ||
// TIME | ||
@@ -116,55 +117,56 @@ | ||
// 2014-01-01 → YYYY-MM-DD | ||
// 14-01-01 → YY-MM-DD | ||
// 14-1-1 → YY-M-D | ||
// 2014-1-1 → YYYY-M-D | ||
function replaceBigEndian (_, year, month, day) { | ||
return replaceDayMonthYearNr({ | ||
day: day, | ||
month: month, | ||
year: year | ||
}, ['year', 'month','day'], '-'); | ||
} | ||
// if we can't find an endian based on the separator, but | ||
// there still is a short date with day, month & year, | ||
// we try to make a smart decision to identify the order | ||
function replaceEndian(options, matchedPart, first, separator, second, third) { | ||
var parts; | ||
var hasSingleDigit = Math.min(first.length, second.length, third.length) === 1; | ||
var hasQuadDigit = Math.max(first.length, second.length, third.length) === 4; | ||
var index = -1; | ||
var preferedOrder = typeof options.preferedOrder === 'string' ? options.preferedOrder : options.preferedOrder[separator]; | ||
// 01.01.2014 → DD.MM.YYYY | ||
// 01.01.14 → DD.MM.YY | ||
// 1.1.14 → D.M.YY | ||
// 1.1.2014 → D.M.YYYY | ||
function replaceLittleEndian (_, day, month, year) { | ||
return replaceDayMonthYearNr({ | ||
day: day, | ||
month: month, | ||
year: year | ||
}, ['day','month','year'], '.'); | ||
} | ||
first = parseInt(first, 10); | ||
second = parseInt(second, 10); | ||
third = parseInt(third, 10); | ||
parts = [first, second, third]; | ||
preferedOrder = preferedOrder.toUpperCase(); | ||
// 01/01/2014 → DD/MM/YYYY | ||
// 01/01/14 → DD/MM/YY | ||
// 1/1/14 → D/M/YY | ||
// 1/1/2014 → D/M/YYYY | ||
function replaceMiddleEndian (_, month, day, year) { | ||
return replaceDayMonthYearNr({ | ||
day: day, | ||
month: month, | ||
year: year | ||
}, ['month','day','year'], '/'); | ||
} | ||
// If first is a year, order will always be Year-Month-Day | ||
if (first > 31) { | ||
parts[0] = hasQuadDigit ? 'YYYY' : 'YY'; | ||
parts[1] = hasSingleDigit ? 'M' : 'MM'; | ||
parts[2] = hasSingleDigit ? 'D' : 'DD'; | ||
return parts.join(separator); | ||
} | ||
// | ||
// Replaces year to YYYY or YY, depending on length of match. | ||
// Replacing day/month with one or two format (D or DD, M or MM), | ||
// depending on whether on of the matches has a lenght of 1 | ||
// | ||
function replaceDayMonthYearNr (parts, order, separator) { | ||
parts.year = (parts.year.length === 2) ? 'YY' : 'YYYY'; | ||
if (Math.min(parts.day.length, parts.month.length) === 1) { | ||
parts.day = 'D'; | ||
parts.month = 'M'; | ||
} else { | ||
parts.day = 'DD'; | ||
parts.month = 'MM'; | ||
// Second will never be the year. And if it is a day, | ||
// the order will always be Month-Day-Year | ||
if (second > 12) { | ||
parts[0] = hasSingleDigit ? 'M' : 'MM'; | ||
parts[1] = hasSingleDigit ? 'D' : 'DD'; | ||
parts[2] = hasQuadDigit ? 'YYYY' : 'YY'; | ||
return parts.join(separator); | ||
} | ||
return order.map(function(name) { | ||
return parts[name]; | ||
}).join(separator); | ||
// if third is a year ... | ||
if (third > 31) { | ||
parts[2] = hasQuadDigit ? 'YYYY' : 'YY'; | ||
// ... try to find day in first and second. | ||
// If found, the remaining part is the month. | ||
index = first > 12 ? 0 : second > 12 ? 1 : -1; | ||
if (index !== -1) { | ||
parts[index] = hasSingleDigit ? 'D' : 'DD'; | ||
index = index === 0 ? 1 : 0; | ||
parts[index] = hasSingleDigit ? 'M' : 'MM'; | ||
return parts.join(separator); | ||
} | ||
} | ||
// if we had no luck until here, we use the prefered order | ||
parts[preferedOrder.indexOf('D')] = hasSingleDigit ? 'D' : 'DD'; | ||
parts[preferedOrder.indexOf('M')] = hasSingleDigit ? 'M' : 'MM'; | ||
parts[preferedOrder.indexOf('Y')] = hasQuadDigit ? 'YYYY' : 'YY'; | ||
return parts.join(separator); | ||
} | ||
@@ -171,0 +173,0 @@ |
{ | ||
"name": "moment-parseformat", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "A moment.js plugin to extract the format of a date/time string", | ||
@@ -5,0 +5,0 @@ "main": "moment.parseFormat.js", |
@@ -22,7 +22,7 @@ moment.parseFormat – a moment.js plugin | ||
Usage | ||
Browser Usage | ||
----- | ||
```js | ||
var format = moment.parseFormat('Thursday, February 6th, 2014 9:20pm'); | ||
var format = moment.parseFormat('Thursday, February 6th, 2014 9:20pm'/* , options */); | ||
// dddd, MMMM Do, YYYY h:mma | ||
@@ -32,2 +32,61 @@ moment().format(format); // format | ||
Node / CommonJS Usage | ||
----- | ||
```js | ||
var moment = require 'moment' | ||
var parseFormat = require('moment-parseformat') | ||
var format = parseFormat('Thursday, February 6th, 2014 9:20pm'/* , options */); | ||
// dddd, MMMM Do, YYYY h:mma | ||
moment().format(format); // format | ||
``` | ||
Options | ||
---- | ||
Options can be passed as 2nd parameter | ||
#### preferedOrder | ||
Type: `Object` or `String` | ||
`parseFormat` tries to figure out the the order of day/month/year by itself | ||
if it finds 3 numbers separated by `.`, `-` or `/`. But if it can't, it will fallback | ||
to `preferedOrder`, which can either be set as an object to differentiate by separator, | ||
or as a simple string. | ||
Default value: | ||
```js | ||
preferedOrder: { | ||
'/': 'MDY', | ||
'.': 'DMY', | ||
'-': 'YMD' | ||
} | ||
``` | ||
Usage | ||
```js | ||
parseFormat('10.10.2010', {preferedOrder: 'DMY'}); | ||
// ☛ DD.MM.YYYY | ||
parseFormat('10.10.2010', {preferedOrder: 'MDY'}); | ||
// ☛ MM.DD.YYYY | ||
parseFormat('10.10.2010', {preferedOrder: { | ||
'/': 'MDY', | ||
'.': 'DMY', | ||
'-': 'YMD' | ||
}}); | ||
// ☛ MM.DD.YYYY | ||
parseFormat('10/10/2010', {preferedOrder: { | ||
'/': 'MDY', | ||
'.': 'DMY', | ||
'-': 'YMD' | ||
}}); | ||
// ☛ DD/MM/YYYY | ||
``` | ||
Fine Print | ||
@@ -34,0 +93,0 @@ ---------- |
/* global moment, test, equal */ | ||
test('standard cases', function() { | ||
@@ -16,4 +17,11 @@ 'use strict'; | ||
// https://github.com/gr2m/moment.parseFormat/issues/3 | ||
equal( moment.parseFormat('Thursday, February 6th, 2014 9:20pm'), 'dddd, MMMM Do, YYYY h:mma', '#3 Thursday, February 6th, 2014 9:20pm -> dddd, MMMM Do, YYYY h:mma' ); | ||
equal( moment('Thursday, February 6th, 2014 9:20pm', 'dddd, MMMM Do, YYYY h:mma').date(), 6, '#3 sanity check: "Thursday, February 6th, 2014 9:20pm" parses 6th correctly' ); | ||
// https://github.com/gr2m/moment.parseFormat/issues/4 | ||
equal( moment.parseFormat('1.1.2010'), 'D.M.YYYY', '#4 1.1.2010 → D.M.YYYY' ); | ||
// https://github.com/gr2m/moment.parseFormat/issues/11 | ||
equal( moment.parseFormat('9-17-1980'), 'M-D-YYYY', '#11 9-17-1980 → M-D-YYYY' ); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2972766
44271
97