Comparing version 2.1.0 to 2.2.0
@@ -0,1 +1,9 @@ | ||
2.2.0 / 2017-09-06 | ||
================== | ||
* Implement date formatting (non-standard) | ||
2.1.0 / 2017-02-17 | ||
================== | ||
* Support negative masked dates | ||
* Support negative masked years | ||
2.0.0 / 2016-08-17 | ||
@@ -2,0 +10,0 @@ ================== |
{ | ||
"name": "edtf", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Extended Date Time Format (EDTF) / ISO 8601-2 Parser and Library", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"coverage": "istanbul cover _mocha", | ||
"prepublish": "npm run compile", | ||
"prepare": "npm run compile", | ||
"pretest": "npm run lint", | ||
@@ -37,11 +37,11 @@ "test": "mocha" | ||
"dependencies": { | ||
"nearley": "^2.5.0", | ||
"randexp": "^0.4.2" | ||
"nearley": "^2.11.0", | ||
"randexp": "^0.4.6" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"eslint": "^3.3.1", | ||
"eslint": "^3.19.0", | ||
"istanbul": "^0.4.3", | ||
"mocha": "^3.0.2" | ||
"mocha": "^3.5.0" | ||
} | ||
} |
@@ -12,3 +12,2 @@ 'use strict' | ||
const { isArray } = Array | ||
const { defineProperty: prop } = Object | ||
@@ -90,6 +89,2 @@ const P = new WeakMap() | ||
this.unspecified = unspecified | ||
prop(this, 'format', { | ||
value: format.bind(null, this) | ||
}) | ||
} | ||
@@ -259,4 +254,4 @@ | ||
localize(...args) { | ||
return this.format(...args).format(this) | ||
format(...args) { | ||
return format(this, ...args) | ||
} | ||
@@ -263,0 +258,0 @@ |
'use strict' | ||
const { assign } = Object | ||
const LC = require('../locale-data') | ||
function format(date, locale, options = {}) { | ||
const noTime = { | ||
timeZoneName: undefined, | ||
hour: undefined, | ||
minute: undefined, | ||
second: undefined | ||
} | ||
const DEFAULTS = [ | ||
assign({ weekday: undefined, day: undefined, month: undefined }, noTime), | ||
assign({ weekday: undefined, day: undefined }, noTime), | ||
assign({}, noTime), | ||
] | ||
function getFormat(date, locale, options) { | ||
const defaults = {} | ||
@@ -10,28 +24,73 @@ | ||
case 3: | ||
if (options.weekday) defaults.weekday = options.weekday | ||
else defaults.day = options.day || 'numeric' | ||
defaults.day = 'numeric' | ||
// eslint-disable-next-line no-fallthrough | ||
case 2: | ||
defaults.month = options.month || 'numeric' | ||
defaults.month = 'numeric' | ||
// eslint-disable-next-line no-fallthrough | ||
case 1: | ||
defaults.year = options.year || 'numeric' | ||
defaults.year = 'numeric' | ||
break | ||
} | ||
defaults.hour = undefined | ||
defaults.minute = undefined | ||
defaults.second = undefined | ||
defaults.timeZoneName = undefined | ||
return new Intl.DateTimeFormat( | ||
locale, | ||
assign(defaults, options, DEFAULTS[date.precision]) | ||
) | ||
} | ||
break | ||
function getPatternsFor(fmt) { | ||
const { locale, weekday, month, year } = fmt.resolvedOptions() | ||
const lc = LC[locale] | ||
if (lc == null) return null | ||
const variant = (weekday || month === 'long') ? 'long' : | ||
(!month || year === '2-digit') ? 'short' : 'medium' | ||
return { | ||
approximate: lc.date.approximate[variant], | ||
uncertain: lc.date.uncertain[variant] | ||
} | ||
} | ||
return new Intl.DateTimeFormat(locale, assign({}, options, defaults)) | ||
function isDMY(type) { | ||
return type === 'day' || type === 'month' || type === 'year' | ||
} | ||
function format(date, locale = 'en-US', options = {}) { | ||
const fmt = getFormat(date, locale, options) | ||
const pat = getPatternsFor(fmt) | ||
if (!date.isEDTF || pat == null) { | ||
return fmt.format(date) | ||
} | ||
let string = '' | ||
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) { | ||
string = pat.approximate.replace('%D', string) | ||
} | ||
if (date.uncertain.value) { | ||
string = pat.uncertain.replace('%D', string) | ||
} | ||
return string | ||
} | ||
module.exports = { | ||
getFormat, | ||
format | ||
} |
@@ -15,2 +15,3 @@ // Generated automatically by nearley | ||
var grammar = { | ||
Lexer: undefined, | ||
ParserRules: [ | ||
@@ -353,3 +354,3 @@ {"name": "edtf", "symbols": ["L0"], "postprocess": id}, | ||
{"name": "_$ebnf$1", "symbols": []}, | ||
{"name": "_$ebnf$1", "symbols": [{"literal":" "}, "_$ebnf$1"], "postprocess": function arrconcat(d) {return [d[0]].concat(d[1]);}}, | ||
{"name": "_$ebnf$1", "symbols": ["_$ebnf$1", {"literal":" "}], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "_", "symbols": ["_$ebnf$1"]} | ||
@@ -356,0 +357,0 @@ ] |
@@ -37,2 +37,6 @@ 'use strict' | ||
get isEDTF() { | ||
return true | ||
} | ||
toJSON() { | ||
@@ -39,0 +43,0 @@ return this.toEDTF() |
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
129183
29
1780
Updatednearley@^2.11.0
Updatedrandexp@^0.4.6