Comparing version
23
index.js
@@ -1,18 +0,5 @@ | ||
'use strict' | ||
const edtf = require('./src/edtf') | ||
const Bitmask = require('./src/bitmask') | ||
const types = require('./src/types') | ||
const { parse, defaults } = require('./src/parser') | ||
const { format } = require('./src/format') | ||
module.exports = Object.assign(edtf, types, { | ||
Bitmask, | ||
defaults, | ||
parse, | ||
format, | ||
get sample() { | ||
return require('./src/sample').sample | ||
}, | ||
types: Object.keys(types) | ||
}) | ||
export { edtf as default } from './src/edtf.js' | ||
export * from './src/types.js' | ||
export { Bitmask } from './src/bitmask.js' | ||
export { parse, defaults } from './src/parser.js' | ||
export { format } from './src/format.js' |
{ | ||
"name": "edtf", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"description": "Extended Date Time Format (EDTF) / ISO 8601-2 Parser and Library", | ||
"type": "commonjs", | ||
"main": "./index.js", | ||
"module": "./index.mjs", | ||
"type": "module", | ||
"exports": { | ||
"import": "./index.mjs", | ||
"require": "./index.js" | ||
".": "./index.js", | ||
"./sample": "./src/sample.js", | ||
"./types": "./src/types.js" | ||
}, | ||
@@ -15,7 +14,6 @@ "scripts": { | ||
"compile": "nearleyc -o src/grammar.js src/edtf.ne || true", | ||
"sample": "nearley-unparse -s edtf -n 20 src/grammar.js && echo || true", | ||
"coverage": "istanbul cover _mocha", | ||
"prepare": "npm run compile", | ||
"pretest": "npm run lint", | ||
"test": "mocha", | ||
"coverage": "nyc --reporter=lcov --reporter=text-summary npm test", | ||
"debug": "node inspect $(npm bin)/_mocha" | ||
@@ -34,7 +32,6 @@ }, | ||
], | ||
"author": "Center for History and New Media, George Mason University", | ||
"contributors": [ | ||
"author": [ | ||
"Sylvester Keil <sylvester@keil.or.at>" | ||
], | ||
"license": "AGPL-3.0", | ||
"license": "BSD-2-Clause", | ||
"bugs": { | ||
@@ -53,5 +50,5 @@ "url": "https://github.com/inukshuk/edtf.js/issues" | ||
"eslint": "^7.12.1", | ||
"istanbul": "^0.4.3", | ||
"mocha": "^8.2.1" | ||
"mocha": "^9.0.0", | ||
"nyc": "^15.1.0" | ||
} | ||
} |
105
README.md
# EDTF.js | ||
[](https://travis-ci.org/inukshuk/edtf.js) | ||
[](https://coveralls.io/github/inukshuk/edtf.js?branch=master) | ||
 | ||
[](https://codecov.io/gh/inukshuk/edtf.js) | ||
[](https://www.npmjs.com/packages/edtf) | ||
[](https://opensource.org/licenses/AGPL-3.0) | ||
@@ -21,19 +20,7 @@ An Extended Date Time Format (EDTF) / ISO 8601-2 parser and toolkit for | ||
### ES6 | ||
EDTF.js is written as a standard Nodes.js / CommonJS module using many ES6 | ||
features. It therefore requires Node.js 6 or later (for Nodes.js 4/5 use the | ||
appropriate `--harmony` flags as necessary). | ||
### ESM | ||
Since version 4.0 EDTF.js uses Nodes.js native ESM module implementation. | ||
If you require CommonJS modules, please use package version '< 4.0'. | ||
### Bundling using RollUp | ||
To bundle EDTF.js with RollUp you'll need to enable the following plugins | ||
* rollup-plugin-json | ||
* rollup-plugin-node-resolve | ||
* rollup-plugin-commonjs | ||
* rollup-plugin-node-globals (optional) | ||
* rollup-plugin-node-builtins (optional) | ||
The last two are optional; you need them only if you want to include Node.js | ||
`assert` in your bundle. | ||
## Installation | ||
@@ -50,11 +37,12 @@ $ npm install edtf | ||
const edtf = require('edtf') | ||
import edtf, { Date, Interval } from 'edtf' | ||
edtf('2016-XX') #-> returns an edtf.Date | ||
edtf(new Date()) #-> returns an edtf.Date | ||
edtf('2016-04~/2016-05') #-> returns an edtf.Interval | ||
edtf('2016-XX') #-> returns a EDTF Date | ||
edtf(new Date()) #-> returns a EDTF Date | ||
edtf('2016-04~/2016-05') #-> returns a EDTF Interval | ||
For a list of all types supported by EDTF.js see: | ||
edtf.types | ||
import * as types from 'edtf/types' | ||
Object.keys(types) | ||
#-> ['Date', 'Year', 'Decade', 'Century', 'Season', 'Interval', 'List', 'Set'] | ||
@@ -92,3 +80,3 @@ | ||
in binary yes or no, you can always convert a bitmask's value to boolean. For | ||
more fine-grained information, the `edtf.Bitmask` class provides a convenient | ||
more fine-grained information, the `Bitmask` class provides a convenient | ||
interface for accessing these states: | ||
@@ -161,9 +149,9 @@ | ||
[...edtf('2016-05').between('2016-07')] | ||
[...edtf('2016-05').between(edtf('2016-07'))] | ||
#-> [2016-06] | ||
[...edtf('2016-05').until('2016-07')] | ||
[...edtf('2016-05').until(edtf('2016-07'))] | ||
#-> [2016-05, 2016-06] | ||
[...edtf('2016-05').through('2016-07')] | ||
[...edtf('2016-05').through(edtf('2016-07'))] | ||
#-> [2016-05, 2016-06, 2016-07] | ||
@@ -173,9 +161,9 @@ | ||
[...edtf('2016-07').through('2016-05')] | ||
[...edtf('2016-07').through(edtf('2016-05'))] | ||
#-> [2016-07, 2016-06, 2016-05] | ||
[...edtf('2016-07').until('2016-05')] | ||
[...edtf('2016-07').until(edtf('2016-05'))] | ||
#-> [2016-07, 2016-06] | ||
[...edtf('2016-07').between('2016-05')] | ||
[...edtf('2016-07').between(edtf('2016-05'))] | ||
#-> [2016-06] | ||
@@ -205,3 +193,3 @@ | ||
To use EDTF.js' date parser directly, call `edtf.parse()` with an input | ||
To use EDTF.js' date parser directly, call `parse()` with an input | ||
string and optional parser constraints. The parser will always return | ||
@@ -211,3 +199,4 @@ plain JavaScript objects which you can pass to `edtf()` for conversion | ||
edtf.parse('2016-02') | ||
import { parse } from 'edtf' | ||
parse('2016-02') | ||
#-> { type: 'Date', level: 0, values: [2016, 1] } | ||
@@ -221,8 +210,8 @@ | ||
Unspecified, uncertain, or approximate dates are returned as a numeric | ||
value representing a bitmask; refer to the `edtf.Bitmask` class for details. | ||
value representing a bitmask; refer to the `Bitmask` class for details. | ||
edtf.parse('2016?-~02') | ||
parse('2016?-~02') | ||
#-> { type: 'Date', level: 2, values: [2016, 1], uncertain: 15, approximate: 48 } | ||
edtf.parse('20XX-02') | ||
parse('20XX-02') | ||
#-> { type: 'Date', level: 2, values: [2000, 1], unspecified: 12 } | ||
@@ -235,5 +224,5 @@ | ||
edtf.parse('2016-02-3X') #-> A day in February cannot start with a 3 | ||
edtf.parse('2016-2X-XX') #-> There are only 12 months | ||
edtf.parse('2016-XX-32') #-> No month has 32 days | ||
parse('2016-02-3X') #-> A day in February cannot start with a 3 | ||
parse('2016-2X-XX') #-> There are only 12 months | ||
parse('2016-XX-32') #-> No month has 32 days | ||
@@ -243,3 +232,3 @@ Intervals, Sets, and Lists will contain their parsed constituent dates in | ||
edtf.parse('2015/2016') | ||
parse('2015/2016') | ||
#-> { type: 'Interval', level: 0, values: [{..}, {..}] } | ||
@@ -250,14 +239,14 @@ | ||
edtf.parse('2016?', { level: 0 }) #-> parse error | ||
edtf.parse('2016?', { level: 1 }) #-> ok | ||
parse('2016?', { level: 0 }) #-> parse error | ||
parse('2016?', { level: 1 }) #-> ok | ||
edtf.parse('2016?-02', { level: 1 }) #-> parse error | ||
edtf.parse('2016?-02', { level: 2 }) #-> ok | ||
parse('2016?-02', { level: 1 }) #-> parse error | ||
parse('2016?-02', { level: 2 }) #-> ok | ||
edtf.parse('2016-21', { types: ['Date'] }) #-> parse error | ||
edtf.parse('2016-21', { types: ['Date', 'Season'] }) #-> ok | ||
parse('2016-21', { types: ['Date'] }) #-> parse error | ||
parse('2016-21', { types: ['Date', 'Season'] }) #-> ok | ||
edtf.parse('2016?', { level: 0, types: ['Date'] }) #-> parse error | ||
edtf.parse('2016?', { level: 1, types: ['Date'] }) #-> ok | ||
parse('2016?', { level: 0, types: ['Date'] }) #-> parse error | ||
parse('2016?', { level: 1, types: ['Date'] }) #-> ok | ||
@@ -267,6 +256,8 @@ | ||
EDTF.js can generate random EDTF strings for you. Simply call | ||
`edtf.sample()` to create a new iterator: | ||
EDTF.js can generate random EDTF strings for you. Simply add | ||
`randexp` to your NPM dependencies and import `edtf/sample` | ||
to create a new iterator: | ||
let it = edtf.sample() | ||
import { sample } from 'edtf/sample' | ||
let it = sample() | ||
@@ -279,3 +270,3 @@ it.next() #-> { value: '0097-26', done: false } | ||
[...edtf.sample({ count: 3 }] | ||
[...sample({ count: 3 })] | ||
#-> ['-003%', '-0070-07-31%', '[-0080-10..]'] | ||
@@ -285,9 +276,9 @@ | ||
[...edtf.sample({ count: 3, level: 0 }] | ||
[...sample({ count: 3, level: 0 })] | ||
#-> ['0305/0070-04-30', '-07', '0000/0013'] | ||
[...edtf.sample({ count: 3, level: 1 }] | ||
[...sample({ count: 3, level: 1 })] | ||
#-> ['00XX', 'Y80105', '0000~'] | ||
[...edtf.sample({ count: 3, level: 2 }] | ||
[...sample({ count: 3, level: 2 })] | ||
#-> ['Y1E30', '-8110S2', '{%0401}'] | ||
@@ -301,3 +292,3 @@ | ||
[...edtf.sample({ count: 3, level: 2, type: 'Decade' }] | ||
[...sample({ count: 3, level: 2, type: 'Decade' })] | ||
#-> ['003', '030~', '000'] | ||
@@ -310,6 +301,6 @@ | ||
The EDTF.js generator uses the ingenious | ||
The EDTF.js sample generator uses the ingenious | ||
[randexp](https://github.com/fent/randexp.js). | ||
## License | ||
AGPL-3.0 | ||
EDTF.js is licensed under the terms of the BSD-2-Clause license. |
@@ -1,3 +0,1 @@ | ||
'use strict' | ||
const DAY = /^days?$/i | ||
@@ -28,3 +26,3 @@ const MONTH = /^months?$/i | ||
*/ | ||
class Bitmask { | ||
export class Bitmask { | ||
@@ -309,3 +307,1 @@ static test(a, b) { | ||
] | ||
module.exports = Bitmask |
@@ -1,11 +0,9 @@ | ||
'use strict' | ||
import assert from 'assert' | ||
import { Date as ExtDate } from './date.js' | ||
import { ExtDateTime } from './interface.js' | ||
const assert = require('assert') | ||
const ExtDate = require('./date') | ||
const ExtDateTime = require('./interface') | ||
const { abs, floor } = Math | ||
const V = new WeakMap() | ||
class Century extends ExtDateTime { | ||
export class Century extends ExtDateTime { | ||
constructor(input) { | ||
@@ -105,3 +103,1 @@ super() | ||
} | ||
module.exports = Century |
@@ -1,10 +0,7 @@ | ||
'use strict' | ||
import assert from 'assert' | ||
import { Bitmask } from './bitmask.js' | ||
import { ExtDateTime } from './interface.js' | ||
import { mixin } from './mixin.js' | ||
import { format } from './format.js' | ||
const assert = require('assert') | ||
const Bitmask = require('./bitmask') | ||
const ExtDateTime = require('./interface') | ||
const mixin = require('./mixin') | ||
const { format } = require('./format') | ||
const { abs } = Math | ||
@@ -20,3 +17,3 @@ const { isArray } = Array | ||
class Date extends global.Date { | ||
export class Date extends global.Date { | ||
constructor(...args) { // eslint-disable-line complexity | ||
@@ -271,2 +268,2 @@ let precision = 0 | ||
module.exports = Date | ||
export const pad = Date.pad |
@@ -1,12 +0,10 @@ | ||
'use strict' | ||
import assert from 'assert' | ||
import { Date as ExtDate } from './date.js' | ||
import { ExtDateTime } from './interface.js' | ||
const assert = require('assert') | ||
const ExtDate = require('./date') | ||
const ExtDateTime = require('./interface') | ||
const { abs, floor } = Math | ||
const V = new WeakMap() | ||
class Decade extends ExtDateTime { | ||
export class Decade extends ExtDateTime { | ||
constructor(input) { | ||
@@ -107,3 +105,1 @@ super() | ||
} | ||
module.exports = Decade |
@@ -1,9 +0,7 @@ | ||
'use strict' | ||
import * as types from './types.js' | ||
import { parse } from './parser.js' | ||
const types = require('./types') | ||
const { parse } = require('./parser') | ||
const UNIX_TIME = /^\d{5,}$/ | ||
function edtf(...args) { | ||
export function edtf(...args) { | ||
if (!args.length) | ||
@@ -27,3 +25,1 @@ return new types.Date() | ||
} | ||
module.exports = edtf |
@@ -1,5 +0,4 @@ | ||
'use strict' | ||
import LC from '../locale-data/index.cjs' | ||
const { assign } = Object | ||
const LC = require('../locale-data') | ||
@@ -48,3 +47,3 @@ const noTime = { | ||
function getFormat(date, locale, options) { | ||
export function getFormat(date, locale, options) { | ||
let opts = {} | ||
@@ -106,3 +105,3 @@ | ||
function format(date, locale = 'en-US', options = {}) { | ||
export function format(date, locale = 'en-US', options = {}) { | ||
const fmt = getFormat(date, locale, options) | ||
@@ -132,7 +131,1 @@ const pat = getPatternsFor(fmt) | ||
format.cache = new Map() | ||
module.exports = { | ||
getFormat, | ||
format | ||
} |
@@ -1,17 +0,17 @@ | ||
// Generated automatically by nearley, version 2.19.7 | ||
// Generated automatically by nearley, version 2.20.1 | ||
// http://github.com/Hardmath123/nearley | ||
(function () { | ||
function id(x) { return x[0]; } | ||
const { | ||
import { | ||
num, zero, nothing, pick, pluck, join, concat, merge, century, | ||
interval, list, masked, date, datetime, season, qualify, year, decade | ||
} = require('./util') | ||
} from './util.js' | ||
import { Bitmask } from './bitmask.js' | ||
const { | ||
DAY, MONTH, YEAR, YMD, YM, MD, YYXX, YYYX, XXXX | ||
} = require('./bitmask') | ||
var grammar = { | ||
Lexer: undefined, | ||
ParserRules: [ | ||
} = Bitmask | ||
let Lexer = undefined; | ||
let ParserRules = [ | ||
{"name": "edtf", "symbols": ["L0"], "postprocess": id}, | ||
@@ -149,2 +149,3 @@ {"name": "edtf", "symbols": ["L1"], "postprocess": id}, | ||
{"name": "L2", "symbols": ["L2D"], "postprocess": id}, | ||
{"name": "L2", "symbols": ["L2C"], "postprocess": id}, | ||
{"name": "L2", "symbols": ["L2i"], "postprocess": id}, | ||
@@ -157,2 +158,4 @@ {"name": "L2", "symbols": ["set"], "postprocess": id}, | ||
{"name": "L2D", "symbols": ["decade", "UA"], "postprocess": merge(0, 1)}, | ||
{"name": "L2C", "symbols": ["century"], "postprocess": id}, | ||
{"name": "L2C", "symbols": ["century", "UA"], "postprocess": merge(0, 1, {level: 2})}, | ||
{"name": "ua_date", "symbols": ["ua_year"], "postprocess": qualify}, | ||
@@ -388,10 +391,4 @@ {"name": "ua_date", "symbols": ["ua_year_month"], "postprocess": qualify}, | ||
{"name": "_", "symbols": ["_$ebnf$1"]} | ||
] | ||
, ParserStart: "edtf" | ||
} | ||
if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') { | ||
module.exports = grammar; | ||
} else { | ||
window.grammar = grammar; | ||
} | ||
})(); | ||
]; | ||
let ParserStart = "edtf"; | ||
export default { Lexer, ParserRules, ParserStart }; |
@@ -1,7 +0,5 @@ | ||
'use strict' | ||
import { parse } from './parser.js' | ||
const { parse } = require('./parser') | ||
export class ExtDateTime { | ||
class ExtDateTime { | ||
static get type() { | ||
@@ -125,3 +123,1 @@ return this.name | ||
} | ||
module.exports = ExtDateTime |
@@ -1,12 +0,10 @@ | ||
'use strict' | ||
import assert from 'assert' | ||
import { Date as ExtDate } from './date.js' | ||
import { ExtDateTime } from './interface.js' | ||
import { Season } from './season.js' | ||
const assert = require('assert') | ||
const ExtDate = require('./date') | ||
const ExtDateTime = require('./interface') | ||
const Season = require('./season') | ||
const V = new WeakMap() | ||
class Interval extends ExtDateTime { | ||
export class Interval extends ExtDateTime { | ||
constructor(...args) { | ||
@@ -143,3 +141,1 @@ super() | ||
} | ||
module.exports = Interval |
@@ -1,12 +0,10 @@ | ||
'use strict' | ||
import assert from 'assert' | ||
import { Date } from './date.js' | ||
import { ExtDateTime } from './interface.js' | ||
const assert = require('assert') | ||
const Date = require('./date') | ||
const ExtDateTime = require('./interface') | ||
const { isArray } = Array | ||
const V = new WeakMap() | ||
class List extends ExtDateTime { | ||
export class List extends ExtDateTime { | ||
constructor(...args) { | ||
@@ -124,3 +122,1 @@ super() | ||
} | ||
module.exports = List |
@@ -1,3 +0,1 @@ | ||
'use strict' | ||
const keys = Reflect.ownKeys.bind(Reflect) | ||
@@ -8,3 +6,3 @@ const descriptor = Object.getOwnPropertyDescriptor.bind(Object) | ||
function mixin(target, ...mixins) { | ||
export function mixin(target, ...mixins) { | ||
for (let source of mixins) { | ||
@@ -25,3 +23,1 @@ inherit(target, source) | ||
} | ||
module.exports = mixin |
@@ -1,7 +0,5 @@ | ||
'use strict' | ||
import nearley from 'nearley' | ||
import grammar from './grammar.js' | ||
const nearley = require('nearley') | ||
const grammar = require('./grammar') | ||
const defaults = { | ||
export const defaults = { | ||
level: 2, | ||
@@ -31,23 +29,19 @@ types: [] | ||
module.exports = { | ||
defaults, | ||
export function parse(input, constraints = {}) { | ||
try { | ||
let nep = parser() | ||
let res = best(limit(nep.feed(input).results, constraints)) | ||
parse(input, constraints = {}) { | ||
try { | ||
let nep = module.exports.parser() | ||
let res = best(limit(nep.feed(input).results, constraints)) | ||
if (!res) throw new Error('edtf: No possible parsings (@EOS)') | ||
if (!res) throw new Error('edtf: No possible parsings (@EOS)') | ||
return res | ||
return res | ||
} catch (error) { | ||
error.message += ` for "${input}"` | ||
throw error | ||
} | ||
}, | ||
parser() { | ||
return new nearley.Parser(grammar.ParserRules, grammar.ParserStart) | ||
} catch (error) { | ||
error.message += ` for "${input}"` | ||
throw error | ||
} | ||
} | ||
export function parser() { | ||
return new nearley.Parser(grammar.ParserRules, grammar.ParserStart) | ||
} |
@@ -7,9 +7,10 @@ /* | ||
*/ | ||
'use strict' | ||
const assert = require('assert') | ||
const randexp = require('randexp') | ||
import assert from 'assert' | ||
import randexp from 'randexp' | ||
import * as TYPES from './types.js' | ||
import grammar from './grammar.js' | ||
const types = Object.keys(require('./types')) | ||
const { ParserRules: Rules } = require('./grammar') | ||
const types = Object.keys(TYPES) | ||
const { ParserRules: Rules } = grammar | ||
const { floor, random } = Math | ||
@@ -30,66 +31,64 @@ | ||
module.exports = { | ||
*sample({ count, level, type } = {}) { | ||
let name = 'edtf' | ||
export function *sample({ count, level, type } = {}) { | ||
let name = 'edtf' | ||
if (typeof count !== 'number') | ||
count = Infinity | ||
if (typeof count !== 'number') | ||
count = Infinity | ||
assert(count > 0, `invalid count ${count}`) | ||
assert(count > 0, `invalid count ${count}`) | ||
if (typeof level !== 'undefined') { | ||
assert([0, 1, 2].includes(level), `invalid level ${level}`) | ||
if (typeof level !== 'undefined') { | ||
assert([0, 1, 2].includes(level), `invalid level ${level}`) | ||
if (typeof type !== 'undefined') | ||
assert(types.includes(type), `invalid type ${type}`) | ||
if (typeof type !== 'undefined') | ||
assert(types.includes(type), `invalid type ${type}`) | ||
name = NAMES[level][type || 'any'] | ||
name = NAMES[level][type || 'any'] | ||
if (!name) | ||
throw new Error(`impossible to generate ${type} at level ${level}`) | ||
} | ||
if (!name) | ||
throw new Error(`impossible to generate ${type} at level ${level}`) | ||
} | ||
for (var i = 0; i < count; ++i) yield module.exports.gen(name) | ||
}, | ||
for (var i = 0; i < count; ++i) yield gen(name) | ||
} | ||
gen(root = 'edtf') { | ||
let output = [] | ||
let stack = [root] | ||
export function gen(root = 'edtf') { | ||
let output = [] | ||
let stack = [root] | ||
while (stack.length > 0) { | ||
let name = stack.pop() | ||
while (stack.length > 0) { | ||
let name = stack.pop() | ||
if (typeof name === 'string') { | ||
let rules = Rules.filter(r => r.name === name) | ||
if (typeof name === 'string') { | ||
let rules = Rules.filter(r => r.name === name) | ||
if (rules.length > 0) { | ||
let sample = rules[ | ||
floor(random() * rules.length) | ||
] | ||
if (rules.length > 0) { | ||
let { symbols } = rules[ | ||
floor(random() * rules.length) | ||
] | ||
for (let j = sample.symbols.length - 1; j >= 0; --j) | ||
stack.push(sample.symbols[j]) | ||
for (let j = symbols.length - 1; j >= 0; --j) | ||
stack.push(symbols[j]) | ||
} else throw new Error(`No match for rule "${name}"!`) | ||
} else throw new Error(`No match for rule "${name}"!`) | ||
continue | ||
} | ||
continue | ||
} | ||
if (name.test) { | ||
output.push(new randexp(name).gen()) | ||
continue | ||
} | ||
if (name.test) { | ||
output.push(new randexp(name).gen()) | ||
continue | ||
} | ||
if (name.literal) { | ||
output.push(name.literal) | ||
continue | ||
} | ||
if (name.literal) { | ||
output.push(name.literal) | ||
continue | ||
} | ||
} | ||
return output | ||
.join('') | ||
.replace(/ +/g, '') // remove excessive whitespace | ||
.replace(/-0000/, '0000') // filter negative year zero | ||
} | ||
return output | ||
.join('') | ||
.replace(/ +/g, '') // remove excessive whitespace | ||
.replace(/-0000/, '0000') // filter negative year zero | ||
} |
@@ -1,10 +0,8 @@ | ||
'use strict' | ||
import assert from 'assert' | ||
import { ExtDateTime } from './interface.js' | ||
import { pad } from './date.js' | ||
const assert = require('assert') | ||
const ExtDateTime = require('./interface') | ||
const { pad } = require('./date') | ||
const V = new WeakMap() | ||
class Season extends ExtDateTime { | ||
export class Season extends ExtDateTime { | ||
constructor(input) { | ||
@@ -194,3 +192,1 @@ super() | ||
} | ||
module.exports = Season |
@@ -1,7 +0,5 @@ | ||
'use strict' | ||
import { List } from './list.js' | ||
import { parse } from './parser.js' | ||
const List = require('./list') | ||
const { parse } = require('./parser') | ||
class Set extends List { | ||
export class Set extends List { | ||
static parse(input) { | ||
@@ -19,3 +17,1 @@ return parse(input, { types: ['Set'] }) | ||
} | ||
module.exports = Set |
@@ -1,12 +0,8 @@ | ||
'use strict' | ||
module.exports = { | ||
Date: require('./date'), | ||
Year: require('./year'), | ||
Decade: require('./decade'), | ||
Century: require('./century'), | ||
Season: require('./season'), | ||
Interval: require('./interval'), | ||
List: require('./list'), | ||
Set: require('./set') | ||
} | ||
export { Date } from './date.js' | ||
export { Year } from './year.js' | ||
export { Decade } from './decade.js' | ||
export { Century } from './century.js' | ||
export { Season } from './season.js' | ||
export { Interval } from './interval.js' | ||
export { List } from './list.js' | ||
export { Set } from './set.js' |
225
src/util.js
@@ -1,147 +0,140 @@ | ||
'use strict' | ||
const Bitmask = require('./bitmask') | ||
import { Bitmask } from './bitmask.js' | ||
const { assign } = Object | ||
const util = { | ||
num(data) { | ||
return Number(Array.isArray(data) ? data.join('') : data) | ||
}, | ||
export function num(data) { | ||
return Number(Array.isArray(data) ? data.join('') : data) | ||
} | ||
join(data) { | ||
return data.join('') | ||
}, | ||
export function join(data) { | ||
return data.join('') | ||
} | ||
zero() { return 0 }, | ||
export function zero() { return 0 } | ||
nothing() { return null }, | ||
export function nothing() { return null } | ||
pick(...args) { | ||
return args.length === 1 ? | ||
data => data[args[0]] : | ||
data => util.concat(data, args) | ||
}, | ||
export function pick(...args) { | ||
return args.length === 1 ? | ||
data => data[args[0]] : | ||
data => concat(data, args) | ||
} | ||
pluck(...args) { | ||
return data => args.map(i => data[i]) | ||
}, | ||
export function pluck(...args) { | ||
return data => args.map(i => data[i]) | ||
} | ||
concat(data, idx = data.keys()) { | ||
return Array.from(idx) | ||
.reduce((memo, i) => data[i] !== null ? memo.concat(data[i]) : memo, []) | ||
}, | ||
export function concat(data, idx = data.keys()) { | ||
return Array.from(idx) | ||
.reduce((memo, i) => data[i] !== null ? memo.concat(data[i]) : memo, []) | ||
} | ||
merge(...args) { | ||
if (typeof args[args.length - 1] === 'object') | ||
var extra = args.pop() | ||
export function merge(...args) { | ||
if (typeof args[args.length - 1] === 'object') | ||
var extra = args.pop() | ||
return data => assign(args.reduce((a, i) => assign(a, data[i]), {}), extra) | ||
}, | ||
return data => assign(args.reduce((a, i) => assign(a, data[i]), {}), extra) | ||
} | ||
interval(level) { | ||
return data => ({ | ||
values: [data[0], data[2]], | ||
type: 'Interval', | ||
level | ||
}) | ||
}, | ||
export function interval(level) { | ||
return data => ({ | ||
values: [data[0], data[2]], | ||
type: 'Interval', | ||
level | ||
}) | ||
} | ||
masked(type = 'unspecified', symbol = 'X') { | ||
return (data, _, reject) => { | ||
data = data.join('') | ||
export function masked(type = 'unspecified', symbol = 'X') { | ||
return (data, _, reject) => { | ||
data = data.join('') | ||
let negative = data.startsWith('-') | ||
let mask = data.replace(/-/g, '') | ||
let negative = data.startsWith('-') | ||
let mask = data.replace(/-/g, '') | ||
if (mask.indexOf(symbol) === -1) return reject | ||
if (mask.indexOf(symbol) === -1) return reject | ||
let values = Bitmask.values(mask, 0) | ||
let values = Bitmask.values(mask, 0) | ||
if (negative) values[0] = -values[0] | ||
if (negative) values[0] = -values[0] | ||
return { | ||
values, [type]: Bitmask.compute(mask) | ||
} | ||
return { | ||
values, [type]: Bitmask.compute(mask) | ||
} | ||
}, | ||
} | ||
} | ||
date(values, level = 0, extra = null) { | ||
return assign({ | ||
type: 'Date', | ||
level, | ||
values: Bitmask.normalize(values.map(Number)) | ||
}, extra) | ||
}, | ||
export function date(values, level = 0, extra = null) { | ||
return assign({ | ||
type: 'Date', | ||
level, | ||
values: Bitmask.normalize(values.map(Number)) | ||
}, extra) | ||
} | ||
year(values, level = 1, extra = null) { | ||
return assign({ | ||
type: 'Year', | ||
level, | ||
values: values.map(Number) | ||
}, extra) | ||
}, | ||
export function year(values, level = 1, extra = null) { | ||
return assign({ | ||
type: 'Year', | ||
level, | ||
values: values.map(Number) | ||
}, extra) | ||
} | ||
century(century, level = 0) { | ||
return { | ||
type: 'Century', | ||
level, | ||
values: [century] | ||
} | ||
}, | ||
export function century(value, level = 0) { | ||
return { | ||
type: 'Century', | ||
level, | ||
values: [value] | ||
} | ||
} | ||
decade(decade, level = 2) { | ||
return { | ||
type: 'Decade', | ||
level, | ||
values: [decade] | ||
} | ||
}, | ||
export function decade(value, level = 2) { | ||
return { | ||
type: 'Decade', | ||
level, | ||
values: [value] | ||
} | ||
} | ||
datetime(data) { | ||
let offset = data[3] | ||
if (offset == null) offset = new Date().getTimezoneOffset() | ||
export function datetime(data) { | ||
let offset = data[3] | ||
if (offset == null) offset = new Date().getTimezoneOffset() | ||
return { | ||
values: Bitmask.normalize(data[0].map(Number)).concat(data[2]), | ||
offset, | ||
type: 'Date', | ||
level: 0 | ||
} | ||
}, | ||
return { | ||
values: Bitmask.normalize(data[0].map(Number)).concat(data[2]), | ||
offset, | ||
type: 'Date', | ||
level: 0 | ||
} | ||
} | ||
season(data, level = 1) { | ||
return { | ||
type: 'Season', | ||
level, | ||
values: [Number(data[0]), Number(data[2])] | ||
} | ||
}, | ||
export function season(data, level = 1) { | ||
return { | ||
type: 'Season', | ||
level, | ||
values: [Number(data[0]), Number(data[2])] | ||
} | ||
} | ||
list(data) { | ||
return assign({ values: data[1], level: 2 }, data[0], data[2]) | ||
}, | ||
export function list(data) { | ||
return assign({ values: data[1], level: 2 }, data[0], data[2]) | ||
} | ||
qualify([parts], _, reject) { | ||
let q = { | ||
uncertain: new Bitmask(), approximate: new Bitmask() | ||
} | ||
export function qualify([parts], _, reject) { | ||
let q = { | ||
uncertain: new Bitmask(), approximate: new Bitmask() | ||
} | ||
let values = parts | ||
.map(([lhs, part, rhs], idx) => { | ||
for (let ua in lhs) q[ua].qualify(idx * 2) | ||
for (let ua in rhs) q[ua].qualify(1 + idx * 2) | ||
return part | ||
}) | ||
let values = parts | ||
.map(([lhs, part, rhs], idx) => { | ||
for (let ua in lhs) q[ua].qualify(idx * 2) | ||
for (let ua in rhs) q[ua].qualify(1 + idx * 2) | ||
return part | ||
}) | ||
return (!q.uncertain.value && !q.approximate.value) ? | ||
reject : | ||
assign(util.date(values, 2), { | ||
uncertain: q.uncertain.value, | ||
approximate: q.approximate.value | ||
}) | ||
} | ||
return (!q.uncertain.value && !q.approximate.value) ? | ||
reject : { | ||
...date(values, 2), | ||
uncertain: q.uncertain.value, | ||
approximate: q.approximate.value | ||
} | ||
} | ||
module.exports = util |
@@ -1,7 +0,5 @@ | ||
'use strict' | ||
import assert from 'assert' | ||
import { ExtDateTime } from './interface.js' | ||
import { pad } from './date.js' | ||
const assert = require('assert') | ||
const ExtDate = require('./date') | ||
const ExtDateTime = require('./interface') | ||
const { pad } = ExtDate | ||
const { abs } = Math | ||
@@ -12,3 +10,3 @@ | ||
class Year extends ExtDateTime { | ||
export class Year extends ExtDateTime { | ||
constructor(input) { | ||
@@ -92,3 +90,1 @@ super() | ||
} | ||
module.exports = Year |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
773160
508.95%73
128.13%0
-100%100
42.86%2475
22.16%Yes
NaN1
Infinity%291
-3%