get-date-format
Advanced tools
Comparing version 0.0.4 to 0.1.0
@@ -17,2 +17,4 @@ "use strict"; | ||
var allFormatsPrefetched = (0, _getAllFormats2.default)({}); | ||
/** | ||
@@ -24,6 +26,5 @@ * Extract date format from provided sample of dates | ||
*/ | ||
exports.default = function (dates) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var allFormats = (0, _getAllFormats2.default)(options); | ||
exports.default = function (dates, options) { | ||
var allFormats = options ? (0, _getAllFormats2.default)(options) : allFormatsPrefetched; | ||
var extractedDateFormat = allFormats.find(function (format) { | ||
@@ -30,0 +31,0 @@ return dates.reduce(function (acc, date) { |
@@ -29,9 +29,9 @@ "use strict"; | ||
var _ref$dayFormats = _ref.dayFormats, | ||
dayFormats = _ref$dayFormats === undefined ? ["DD", "D"] : _ref$dayFormats, | ||
dayFormats = _ref$dayFormats === undefined ? ["DD", "D", "ddd", "dddd"] : _ref$dayFormats, | ||
_ref$monthFormats = _ref.monthFormats, | ||
monthFormats = _ref$monthFormats === undefined ? ["MMM", "MM", "M"] : _ref$monthFormats, | ||
monthFormats = _ref$monthFormats === undefined ? ["MMM", "MMMM", "MM", "M"] : _ref$monthFormats, | ||
_ref$yearFormats = _ref.yearFormats, | ||
yearFormats = _ref$yearFormats === undefined ? ["YYYY", "YY"] : _ref$yearFormats, | ||
_ref$delimiters = _ref.delimiters, | ||
delimiters = _ref$delimiters === undefined ? ["-", "/"] : _ref$delimiters; | ||
delimiters = _ref$delimiters === undefined ? ["-", "/", ".", " "] : _ref$delimiters; | ||
@@ -38,0 +38,0 @@ // const dayFormats = [ 'DD', 'D'] |
@@ -25,3 +25,3 @@ "use strict"; | ||
var formats = (0, _getAllFormats2.default)({}); | ||
(0, _chai.expect)(formats.length).to.be.equal(144); | ||
(0, _chai.expect)(formats.length).to.be.equal(768); | ||
}); |
{ | ||
"name": "get-date-format", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Module that tries to get the date format out of sample of dates", | ||
@@ -72,2 +72,2 @@ "main": "./dist/lib.js", | ||
} | ||
} | ||
} |
# get-date-format | ||
Work In Progress | ||
Module that extracts date format out of sample of dates | ||
## Intro | ||
If you have sources of the data with inconsistent date formats, usually it is required to figure out date format of each data set before doing any analysis with the data. This module is trying to solve that issue by extracting date format from the array of the dates. The more elements are in the array, the more accurate is the prediction. 13 consecutive days are enough for accurate prediction of the date format. | ||
This module can detect all official date formats of all the countries represented here https://en.wikipedia.org/wiki/Date_format_by_country | ||
## Examples | ||
Example usage: | ||
```javascript | ||
const getDateFormat = require('get-date-format') | ||
// or | ||
// import getDateFormat from 'get-date-format' | ||
const sampleOfDates = ['2017-01-01', '2017-03-11', '2017-03-13', '2017-12-12'] | ||
const dateFormat = getDateFormat(sampleOfDates) | ||
console.log(dateFormat) | ||
// YYYY-MM-DD | ||
``` | ||
If date format can't be extracted, function is returning `null` | ||
```javascript | ||
const getDateFormat = require('get-date-format') | ||
// or | ||
// import getDateFormat from 'get-date-format' | ||
const sampleOfDates = ['some text', 'more text'] | ||
const dateFormat = getDateFormat(sampleOfDates) | ||
console.log(dateFormat) | ||
// null | ||
``` | ||
## Optimizing for speed | ||
*TODO* |
12763
42
14
175