any-date-parser
Advanced tools
Comparing version 1.4.2 to 1.4.3
## Change Log | ||
### v1.4.3 on 2021-05-23 | ||
- Provide a way to export a parser as a function: `parser.exportAsFunction()` | ||
and `parser.exportAsFunctionAny()` | ||
[Addresses #7](https://github.com/kensnyder/any-date-parser/issues/7) | ||
### v1.4.2 on 2021-05-23 | ||
@@ -4,0 +10,0 @@ |
@@ -24,5 +24,2 @@ // import our main modules | ||
const yearMonthDay = require('./src/formats/yearMonthDay/yearMonthDay.js'); | ||
// import our Date.* extensions | ||
const fromString = require('./src/fromString/fromString.js'); | ||
const fromAny = require('./src/fromAny/fromAny.js'); | ||
@@ -54,3 +51,3 @@ // create a default parser instance and register all the default formats | ||
// make it easy to consume our other main modules | ||
// make it easy to consume our other main modules and functions | ||
parser.Parser = Parser; | ||
@@ -61,6 +58,6 @@ parser.Format = Format; | ||
// create functions on Date | ||
parser.fromString = Date.fromString = fromString(parser, defaultLocale); | ||
parser.fromAny = Date.fromAny = fromAny(parser.fromString); | ||
parser.fromString = Date.fromString = parser.exportAsFunction(); | ||
parser.fromAny = Date.fromAny = parser.exportAsFunctionAny(); | ||
// export our default parser | ||
module.exports = parser; |
{ | ||
"name": "any-date-parser", | ||
"version": "1.4.2", | ||
"version": "1.4.3", | ||
"description": "Parse a wide range of date formats including human-input dates", | ||
@@ -5,0 +5,0 @@ "tags": [ |
# any-date-parser | ||
[![NPM Link](https://img.shields.io/npm/v/any-date-parser?v=1.4.2)](https://npm.com/package/any-date-parser) | ||
[![Build Status](https://travis-ci.org/kensnyder/any-date-parser.svg?branch=master&v=1.4.2)](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.2)](https://codecov.io/gh/kensnyder/any-date-parser) | ||
[![ISC License](https://img.shields.io/npm/l/any-date-parser.svg?v=1.4.2)](https://opensource.org/licenses/ISC) | ||
[![NPM Link](https://img.shields.io/npm/v/any-date-parser?v=1.4.3)](https://npm.com/package/any-date-parser) | ||
[![Build Status](https://travis-ci.org/kensnyder/any-date-parser.svg?branch=master&v=1.4.3)](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.3)](https://codecov.io/gh/kensnyder/any-date-parser) | ||
[![ISC License](https://img.shields.io/npm/l/any-date-parser.svg?v=1.4.3)](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.4.2/dist/browser-bundle.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/any-date-parser@1.4.3/dist/browser-bundle.js"></script> | ||
``` | ||
@@ -31,2 +31,3 @@ | ||
1. [Removing parsing rules](#removing-parsing-rules) | ||
1. [Creating a custom parser](#creating-a-custom-parser) | ||
1. [Unit tests](#unit-tests) | ||
@@ -226,4 +227,7 @@ 1. [Contributing](#contributing) | ||
To create a new parser with a limited list of formats, use `new Parser` | ||
### Creating a custom parser | ||
To create a new parser with a limited list of formats or your own custom | ||
formats, use `new Parser` | ||
```js | ||
@@ -239,5 +243,17 @@ const { Parser } = require('any-date-parser'); | ||
You can convert your custom parser to a function. For example: | ||
```js | ||
const { Parser } = require('any-date-parser'); | ||
const parser = new Parser(); | ||
// .... | ||
parser.addFormats(/*...*/); | ||
// Pass locale if you want to override the detected default | ||
Date.fromString = parser.exportAsFunction(); | ||
Date.fromAny = parser.exportAsFunctionAny(); | ||
``` | ||
## Unit tests | ||
any-date-parser has 100% code coverage. | ||
`any-date-parser` has 100% code coverage. | ||
@@ -244,0 +260,0 @@ ### Two steps before testing: |
@@ -20,2 +20,6 @@ const normalizeLocale = require('./normalizeLocale.js'); | ||
}); | ||
it('should fall back to en-US on invalid locales', () => { | ||
const normalized = normalizeLocale('foobar 9000'); | ||
expect(normalized).toEqual('en-US'); | ||
}); | ||
}); |
const defaultLocale = require('../data/defaultLocale.js'); | ||
const fromString = require('../fromString/fromString.js'); | ||
const fromAny = require('../fromAny/fromAny.js'); | ||
@@ -75,4 +77,22 @@ class Parser { | ||
} | ||
/** | ||
* Export this parser as a single function that takes a string | ||
* @param {String} locale The default locale it should use | ||
* @returns {Function} | ||
*/ | ||
exportAsFunction(locale = defaultLocale) { | ||
return fromString(this, locale); | ||
} | ||
/** | ||
* Export this parser as a single function that takes a string or Date | ||
* @param {String} locale The default locale it should use | ||
* @returns {Function} | ||
*/ | ||
exportAsFunctionAny(locale = defaultLocale) { | ||
return fromAny(fromString(this, locale)); | ||
} | ||
} | ||
module.exports = Parser; |
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
144565
3704
403