@@ -0,1 +1,5 @@ | ||
2.4.0 / 2018-11-12 | ||
================== | ||
* Add DateTimeFormat cache | ||
2.2.0 / 2017-09-06 | ||
@@ -9,2 +13,3 @@ ================== | ||
* Support negative masked years | ||
2.0.0 / 2016-08-17 | ||
@@ -11,0 +16,0 @@ ================== |
{ | ||
"name": "edtf", | ||
"version": "2.3.2", | ||
"version": "2.4.0", | ||
"description": "Extended Date Time Format (EDTF) / ISO 8601-2 Parser and Library", | ||
@@ -37,11 +37,11 @@ "main": "index.js", | ||
"dependencies": { | ||
"nearley": "^2.11.1", | ||
"randexp": "^0.4.8" | ||
"nearley": "^2.15.1", | ||
"randexp": "^0.4.9" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"eslint": "^4.17.0", | ||
"chai": "^4.2.0", | ||
"eslint": "^4.19.1", | ||
"istanbul": "^0.4.3", | ||
"mocha": "^5.0.1" | ||
"mocha": "^5.2.0" | ||
} | ||
} |
@@ -21,21 +21,53 @@ 'use strict' | ||
function getCacheId(...args) { | ||
let id = [] | ||
for (let arg of args) { | ||
if (arg && typeof arg === 'object') { | ||
id.push(getOrderedProps(arg)) | ||
} else { | ||
id.push(arg) | ||
} | ||
} | ||
return JSON.stringify(id) | ||
} | ||
function getOrderedProps(obj) { | ||
let props = [] | ||
let keys = Object.getOwnPropertyNames(obj) | ||
for (let key of keys.sort()) { | ||
props.push({ [key]: obj[key] }) | ||
} | ||
return props | ||
} | ||
function getFormat(date, locale, options) { | ||
const defaults = {} | ||
let opts = {} | ||
switch (date.precision) { | ||
case 3: | ||
defaults.day = 'numeric' | ||
opts.day = 'numeric' | ||
// eslint-disable-next-line no-fallthrough | ||
case 2: | ||
defaults.month = 'numeric' | ||
opts.month = 'numeric' | ||
// eslint-disable-next-line no-fallthrough | ||
case 1: | ||
defaults.year = 'numeric' | ||
opts.year = 'numeric' | ||
break | ||
} | ||
return new Intl.DateTimeFormat( | ||
locale, | ||
assign(defaults, options, DEFAULTS[date.precision]) | ||
) | ||
assign(opts, options, DEFAULTS[date.precision]) | ||
let id = getCacheId(locale, opts) | ||
if (!format.cache.has(id)) { | ||
format.cache.set(id, new Intl.DateTimeFormat(locale, opts)) | ||
} | ||
return format.cache.get(id) | ||
} | ||
@@ -62,2 +94,14 @@ | ||
function mask(date, parts) { | ||
let string = '' | ||
for (let { type, value } of parts) { | ||
string += (isDMY(type) && date.unspecified.is(type)) ? | ||
value.replace(/./g, 'X') : | ||
value | ||
} | ||
return string | ||
} | ||
function format(date, locale = 'en-US', options = {}) { | ||
@@ -71,15 +115,7 @@ const fmt = getFormat(date, locale, options) | ||
let string = '' | ||
let string = (!date.unspecified.value || !fmt.formatToParts) ? | ||
fmt.format(date) : | ||
mask(date, fmt.formatToParts(date)) | ||
if (date.unspecified.value && typeof fmt.formatToParts === 'function') { | ||
for (let { type, value } of fmt.formatToParts(date)) { | ||
string += (isDMY(type) && date.unspecified.is(type)) ? | ||
value.replace(/./g, 'X') : | ||
value | ||
} | ||
} else { | ||
string = fmt.format(date) | ||
} | ||
if (date.approximate.value) { | ||
@@ -96,3 +132,5 @@ string = pat.approximate.replace('%D', string) | ||
format.cache = new Map() | ||
module.exports = { | ||
@@ -99,0 +137,0 @@ getFormat, |
@@ -1,2 +0,2 @@ | ||
// Generated automatically by nearley, version 2.11.1 | ||
// Generated automatically by nearley, version 2.15.1 | ||
// http://github.com/Hardmath123/nearley | ||
@@ -3,0 +3,0 @@ (function () { |
194848
0.32%3552
0.68%