any-date-parser
Advanced tools
Comparing version 1.3.1 to 1.4.0
## Change Log | ||
### v1.0.0 on 20XX-XX-XX | ||
### v1.4.0 on 2021-05-23 | ||
- Added support for other whitespace characters between date parts | ||
- Improved docs | ||
- Improved test scripts to auto install luxon and full-icu if not present | ||
- Fixed bug in numbering system RegExp | ||
- Support restricting parsers to a list of locales | ||
- Restrict monthDayYear to `en-US` and similar locales | ||
_... incomplete history_ | ||
### v1.0.0 on 2020-02-20 | ||
- Initial release |
ISC License (ISC) | ||
Copyright 2020 Ken Snyder | ||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. | ||
Copyright 2016-2021 Ken Snyder | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
Permission to use, copy, modify, and/or distribute this software for any purpose | ||
with or without fee is hereby granted, provided that the above copyright notice | ||
and this permission notice appear in all copies. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS | ||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | ||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF | ||
THIS SOFTWARE. |
{ | ||
"name": "any-date-parser", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Parse a wide range of date formats including human-input dates", | ||
@@ -40,3 +40,6 @@ "tags": [ | ||
}, | ||
"author": "", | ||
"author": { | ||
"name": "Ken Snyder", | ||
"email": "kendsnyder@gmail.com" | ||
}, | ||
"license": "ISC", | ||
@@ -43,0 +46,0 @@ "bugs": { |
107
README.md
# any-date-parser | ||
[![NPM Link](https://img.shields.io/npm/v/any-date-parser?v=1.3.1)](https://npm.com/package/any-date-parser) | ||
[![Build Status](https://travis-ci.org/kensnyder/any-date-parser.svg?branch=master&v=1.3.1)](https://travis-ci.org/kensnyder/any-date-parser) | ||
[![Code Coverage](https://codecov.io/gh/kensnyder/any-date-parser/branch/master/graph/badge.svg?v=1.3.1)](https://codecov.io/gh/kensnyder/any-date-parser) | ||
[![ISC License](https://img.shields.io/npm/l/any-date-parser.svg?v=1.3.1)](https://opensource.org/licenses/ISC) | ||
[![NPM Link](https://img.shields.io/npm/v/any-date-parser?v=1.4.0)](https://npm.com/package/any-date-parser) | ||
[![Build Status](https://travis-ci.org/kensnyder/any-date-parser.svg?branch=master&v=1.4.0)](https://travis-ci.org/kensnyder/any-date-parser) | ||
[![Code Coverage](https://codecov.io/gh/kensnyder/any-date-parser/branch/master/graph/badge.svg?v=1.4.0)](https://codecov.io/gh/kensnyder/any-date-parser) | ||
[![ISC License](https://img.shields.io/npm/l/any-date-parser.svg?v=1.4.0)](https://opensource.org/licenses/ISC) | ||
@@ -19,3 +19,3 @@ Parse a wide range of date formats including human-input dates. | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/any-date-parser@1.3.1/dist/browser-bundle.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/any-date-parser@1.4.0/dist/browser-bundle.js"></script> | ||
``` | ||
@@ -55,3 +55,4 @@ | ||
```javascript | ||
```js | ||
require('any-date-format'); | ||
Date.fromString('2020-10-15'); | ||
@@ -61,7 +62,23 @@ // same as new Date(2020, 9, 15, 0, 0, 0, 0) | ||
2.) It also exports `parser` with function `parser.attempt(string, locale)` that | ||
2.) Use the parser object: | ||
- `parser.fromString(string, locale)` - Parses a string and returns a `Date` | ||
object. It is the same function as in option 1. | ||
- `parser.fromAny(any, locale)` - Return a `Date` object given a `Date`, | ||
`Number` or string to parse. It is the same function as in option 1. | ||
Example: | ||
```js | ||
const parser = require('any-date-format'); | ||
parser.fromString('2020-10-15'); | ||
// same as new Date(2020, 9, 15, 0, 0, 0, 0) | ||
``` | ||
3.) It also exports `parser` with function `parser.attempt(string, locale)` that | ||
returns an object with one or more integer values for the following keys: year, | ||
month, day, hour, minute, second, millisecond, offset. Example: | ||
```javascript | ||
```js | ||
const parser = require('any-date-format'); | ||
parser.attempt('15 Oct 2020 at 6pm'); | ||
@@ -77,7 +94,7 @@ // returns: | ||
3.) There are npm packages that integrate any-date-parser directly into popular | ||
4.) There are npm packages that integrate any-date-parser directly into popular | ||
date libraries: | ||
- Luxon: [luxon-parse](http://npmjs.com/packages/luxon-parse) _coming soon_ | ||
- DayJS: [dayjs-parse](http://npmjs.com/package/dayjs-parse) _coming soon_ | ||
- Luxon: [luxon-parser](http://npmjs.com/packages/luxon-parser) | ||
- DayJS: [dayjs-parser](http://npmjs.com/package/dayjs-parser) | ||
- Moment: [moment-parseplus](http://npmjs.com/package/moment-parseplus) | ||
@@ -134,8 +151,11 @@ | ||
```js | ||
const parser, { Format } = require('any-date-parser'); | ||
const parser, | ||
{ Format } = require('any-date-parser'); | ||
parser.addFormat(new Format({ | ||
matcher: /^(\d+) days? into month (\d+) in year (\d{4})$/, | ||
units: ['day','month','year'], | ||
})); | ||
parser.addFormat( | ||
new Format({ | ||
matcher: /^(\d+) days? into month (\d+) in year (\d{4})$/, | ||
units: ['day', 'month', 'year'], | ||
}) | ||
); | ||
``` | ||
@@ -151,12 +171,15 @@ | ||
```js | ||
const parser, { Format } = require('any-date-parser'); | ||
const parser, | ||
{ Format } = require('any-date-parser'); | ||
parser.addFormat(new Format({ | ||
matcher: /^Q([1-4]) (\d{4})$/, | ||
handler: function([, quarter, year]) { | ||
const monthByQuarter = { '1': 1, '2': 4, '3': 7, '4': 10}; | ||
const month = monthByQuarter[quarter]; | ||
return { year, month }; | ||
}, | ||
})); | ||
parser.addFormat( | ||
new Format({ | ||
matcher: /^Q([1-4]) (\d{4})$/, | ||
handler: function ([, quarter, year]) { | ||
const monthByQuarter = { 1: 1, 2: 4, 3: 7, 4: 10 }; | ||
const month = monthByQuarter[quarter]; | ||
return { year, month }; | ||
}, | ||
}) | ||
); | ||
``` | ||
@@ -167,8 +190,11 @@ | ||
```js | ||
const parser, { Format } = require('any-date-parser'); | ||
const parser, | ||
{ Format } = require('any-date-parser'); | ||
parser.addFormat(new Format({ | ||
template: 'The (_DAY_)(?:_ORDINAL_) day of (_MONTH_), (_YEAR_)', | ||
units: ['day', 'month', 'year'], | ||
})); | ||
parser.addFormat( | ||
new Format({ | ||
template: 'The (_DAY_)(?:_ORDINAL_) day of (_MONTH_), (_YEAR_)', | ||
units: ['day', 'month', 'year'], | ||
}) | ||
); | ||
``` | ||
@@ -179,12 +205,15 @@ | ||
```js | ||
const parser, { Format } = require('any-date-parser'); | ||
const parser, | ||
{ Format } = require('any-date-parser'); | ||
parser.addFormat(new Format({ | ||
template: '^Q([1-4]) (_YEAR_)$', | ||
handler: function([, quarter, year]) { | ||
const monthByQuarter = { '1': 1, '2': 4, '3': 7, '4': 10}; | ||
const month = monthByQuarter[quarter]; | ||
return { year, month }; | ||
}, | ||
})); | ||
parser.addFormat( | ||
new Format({ | ||
template: '^Q([1-4]) (_YEAR_)$', | ||
handler: function ([, quarter, year]) { | ||
const monthByQuarter = { 1: 1, 2: 4, 3: 7, 4: 10 }; | ||
const month = monthByQuarter[quarter]; | ||
return { year, month }; | ||
}, | ||
}) | ||
); | ||
``` | ||
@@ -191,0 +220,0 @@ |
@@ -23,36 +23,36 @@ const startCodes = { | ||
// full-width numbers, hanidec numbers, arabic numbers | ||
// full-width numbers, hanidec numbers, latin numbers (\d) | ||
const chineseGroup = '[0123456789〇一二三四五六七八九\\d]'; | ||
const defaultLookup = { | ||
0: '0', | ||
1: '1', | ||
2: '2', | ||
3: '3', | ||
4: '4', | ||
5: '5', | ||
6: '6', | ||
7: '7', | ||
8: '8', | ||
9: '9', | ||
'0': '0', | ||
'1': '1', | ||
'2': '2', | ||
'3': '3', | ||
'4': '4', | ||
'5': '5', | ||
'6': '6', | ||
'7': '7', | ||
'8': '8', | ||
'9': '9', | ||
〇: '0', | ||
一: '1', | ||
二: '2', | ||
三: '3', | ||
四: '4', | ||
五: '5', | ||
六: '6', | ||
七: '7', | ||
八: '8', | ||
九: '9', | ||
0: 0, | ||
1: 1, | ||
2: 2, | ||
3: 3, | ||
4: 4, | ||
5: 5, | ||
6: 6, | ||
7: 7, | ||
8: 8, | ||
9: 9, | ||
'0': 0, | ||
'1': 1, | ||
'2': 2, | ||
'3': 3, | ||
'4': 4, | ||
'5': 5, | ||
'6': 6, | ||
'7': 7, | ||
'8': 8, | ||
'9': 9, | ||
〇: 0, | ||
一: 1, | ||
二: 2, | ||
三: 3, | ||
四: 4, | ||
五: 5, | ||
六: 6, | ||
七: 7, | ||
八: 8, | ||
九: 9, | ||
}; | ||
@@ -76,3 +76,3 @@ | ||
const start = String.fromCharCode(startCode); | ||
const end = String.fromCharCode(startCode + 10); | ||
const end = String.fromCharCode(startCode + 9); | ||
const lookup = {}; | ||
@@ -79,0 +79,0 @@ for (let i = 0; i < 10; i++) { |
@@ -22,2 +22,3 @@ const timezoneNames = require('./timezoneNames.js'); | ||
MS: '\\d{9}|\\d{6}|\\d{3}', | ||
SPACE: '[\\s-]', | ||
}; | ||
@@ -24,0 +25,0 @@ |
@@ -14,2 +14,3 @@ const LocaleHelper = require('../LocaleHelper/LocaleHelper.js'); | ||
* @param {Function} handler A flexible alternative to units; must return an object | ||
* @param {Array} locales A list of locales that this format should be restricted to | ||
*/ | ||
@@ -21,2 +22,3 @@ constructor({ | ||
handler = null, | ||
locales = null, | ||
}) { | ||
@@ -37,2 +39,3 @@ if (!Array.isArray(units) && typeof handler !== 'function') { | ||
this.handler = handler; | ||
this.locales = locales; | ||
this.regexByLocale = {}; | ||
@@ -39,0 +42,0 @@ } |
@@ -8,4 +8,24 @@ const Format = require('../../Format/Format.js'); | ||
units: ['month', null, 'day', 'year'], | ||
// only certain locales use this date | ||
// see https://en.wikipedia.org/wiki/Date_format_by_country | ||
// see https://www.localeplanet.com/icu/ | ||
locales: [ | ||
'ee-TG', // Togo (Ewe) | ||
'en-AS', // American Samoa | ||
'en-CA', // Canada | ||
'en-FM', // Federated States of Micronesia | ||
'en-GH', // Ghana | ||
'en-GU', // Guam | ||
'en-KE', // Kenya | ||
'en-KY', // Cayman Islands | ||
'en-MH', // Marshall Islands | ||
'en-MP', // Northern Mariana Islands | ||
'en-US', // United States | ||
'en-VI', // US Virgin Islands | ||
'en-WS', // Western Samoa | ||
'sm-AS', // American Samoa (Samoan) | ||
'sm-SM', // Samoa | ||
], | ||
}); | ||
module.exports = monthDayYear; |
const testDates = require('../../../test-fixtures/testDates.js'); | ||
const parser = require('../../../index.js'); | ||
@@ -6,3 +7,19 @@ testDates({ | ||
expected: { year: 2020, month: 3, day: 14 }, | ||
locales: ['en-US'], | ||
locales: [ | ||
'ee-TG', // Togo (Ewe) | ||
'en-AS', // American Samoa | ||
'en-CA', // Canada | ||
'en-FM', // Federated States of Micronesia | ||
'en-GH', // Ghana | ||
'en-GU', // Guam | ||
'en-KE', // Kenya | ||
'en-KY', // Cayman Islands | ||
'en-MH', // Marshall Islands | ||
'en-MP', // Northern Mariana Islands | ||
'en-US', // United States | ||
'en-VI', // US Virgin Islands | ||
'en-WS', // Western Samoa | ||
'sm-AS', // American Samoa (Samoan) | ||
'sm-SM', // Samoa | ||
], | ||
formats: [ | ||
@@ -17,1 +34,16 @@ 'MM/dd/yyyy', | ||
}); | ||
describe('month day year for other locales', () => { | ||
it('should not support month day year', () => { | ||
const actual = parser.attempt('5/31/2021', 'FR'); | ||
expect(actual.invalid).toMatch(/^Unable to parse/); | ||
}); | ||
it('should recognize day month year instead', () => { | ||
const actual = parser.attempt('5/3/2021', 'FR'); | ||
expect(actual).toEqual({ | ||
month: 3, | ||
day: 5, | ||
year: 2021, | ||
}); | ||
}); | ||
}); |
@@ -7,4 +7,4 @@ const LocaleHelper = require('../../LocaleHelper/LocaleHelper.js'); | ||
/* prettier-ignore */ | ||
// $1 $2 $3 $4 $5 | ||
template: '^(?:(.+?) )?(?:at )?(_H12_)(?:\\:(_MIN_)(?:\\:(_SEC_))?)? ?(_MERIDIEM_)$', | ||
// $1 $2 $3 $4 $5 | ||
template: '^(.*?)_SPACE_*(?:at|on|T|)_SPACE_*(_H12_)(?:\\:(_MIN_)(?:\\:(_SEC_))?)?_SPACE_*(_MERIDIEM_)$', | ||
handler: function (matches, locale) { | ||
@@ -11,0 +11,0 @@ let [, dateExpr, hour, minute, second, ampm] = matches; |
@@ -8,4 +8,4 @@ const LocaleHelper = require('../../LocaleHelper/LocaleHelper.js'); | ||
/* prettier-ignore */ | ||
// $1 $2 $3 $4 $5 $6 $7 | ||
template: '^(?:(.+?)[ T])?(_H24_)\\:(_MIN_)(?:\\:(_SEC_)(?:[\\.,](_MS_))?)? ?(?:GMT)? ?(_OFFSET_)? ?(_ZONE_)?$', | ||
// $1 $2 $3 $4 $5 $6 $7 | ||
template: '^(.*?)_SPACE_*(?:at|on|T|)_SPACE_*(_H24_)\\:(_MIN_)(?:\\:(_SEC_)(?:[\\.,](_MS_))?)?_SPACE_*(?:GMT)?_SPACE_*(_OFFSET_)?_SPACE_*(_ZONE_)?$', | ||
handler: function (matches, locale) { | ||
@@ -12,0 +12,0 @@ let [, dateExpr, hour, minute, second, millisecond, offset, zone] = matches; |
@@ -58,2 +58,10 @@ const defaultLocale = require('../data/defaultLocale.js'); | ||
for (const format of this.formats) { | ||
if ( | ||
Array.isArray(format.locales) && | ||
format.locales.length > 0 && | ||
!format.locales.includes(new Intl.Locale(locale).baseName) | ||
) { | ||
// some formats only make sense for certain locales, e.g. month/day/year | ||
continue; | ||
} | ||
const dt = format.attempt(date, locale); | ||
@@ -60,0 +68,0 @@ if (dt) { |
const { DateTime, FixedOffsetZone } = require('luxon'); | ||
const localeList = require('./localeList.js'); | ||
const parser = require('../index.js'); | ||
function testDates({ name, formats, expected, locales }) { | ||
for (const locale of locales || localeList) { | ||
for (const locale of locales) { | ||
describe(`${name} (${locale})`, () => { | ||
@@ -8,0 +7,0 @@ for (const format of formats) { |
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
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
141421
69
3635
0
387