From 2815d9397a21dfc9c5bc9d98c9d010e90a5d5f44 Mon Sep 17 00:00:00 2001 From: upstream Date: Fri, 2 Oct 2026 16:52:45 +0200 Subject: [PATCH] Upstream spencermountain/compromise state of the touched files before 9542b9c97a --- data/lexicon/dates/durations.js | 35 +++ .../src/api/parse/one/01-tokenize/01-shift.js | 3 +- plugins/dates/tests/to-iso.test.js | 233 ++++++++++++++++++ 3 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 data/lexicon/dates/durations.js create mode 100644 plugins/dates/tests/to-iso.test.js diff --git a/data/lexicon/dates/durations.js b/data/lexicon/dates/durations.js new file mode 100644 index 0000000..c8b7d0c --- /dev/null +++ b/data/lexicon/dates/durations.js @@ -0,0 +1,35 @@ +export default [ + 'century', + 'centuries', + 'day', + 'days', + 'decade', + 'decades', + 'hour', + 'hours', + 'hr', + 'hrs', + 'millisecond', + 'milliseconds', + 'minute', + 'minutes', + // 'min', + 'month', + 'months', + 'sec', + 'secs', + // 'week end', + 'weekend', + 'week', + 'weeks', + 'wk', + 'wks', + 'year', + 'years', + 'yr', + 'yrs', + 'quarter', + // 'quarters', + 'qtr', + 'season', +] diff --git a/plugins/dates/src/api/parse/one/01-tokenize/01-shift.js b/plugins/dates/src/api/parse/one/01-tokenize/01-shift.js index 5c2a0a8..0aa8488 100644 --- a/plugins/dates/src/api/parse/one/01-tokenize/01-shift.js +++ b/plugins/dates/src/api/parse/one/01-tokenize/01-shift.js @@ -32,8 +32,7 @@ const halfUnits = { const parseUnit = function (m) { let unit = m.match('#Duration').text('normal') - // 'weeks', or "a month's time" - unit = unit.replace(/'?s$/, '') + unit = unit.replace(/s$/, '') // support shorthands like 'min' if (Object.hasOwn(aliases, unit)) { unit = aliases[unit] diff --git a/plugins/dates/tests/to-iso.test.js b/plugins/dates/tests/to-iso.test.js new file mode 100644 index 0000000..6d5ca51 --- /dev/null +++ b/plugins/dates/tests/to-iso.test.js @@ -0,0 +1,233 @@ +import test from 'tape' +import nlp from './_lib.js' + +const context = { + today: '2019-02-02T03:40:00.000Z', + timezone: false, +} + +test('date-parse :', function (t) { + const arr = [ + ['june 5th 1999', '1999-06-05T00:00:00.000Z'], + ['june 5th 1999', '1999-06-05T00:00:00.000Z'], + ['january 1st 1644', '1644-01-01T00:00:00.000Z'], + ['jan 1st 1644', '1644-01-01T00:00:00.000Z'], + ['June 4th 1993', '1993-06-04T00:00:00.000Z'], + ['March 1st 1987', '1987-03-01T00:00:00.000Z'], + ['June 22nd 2014', '2014-06-22T00:00:00.000Z'], + ['may 22nd 2014', '2014-05-22T00:00:00.000Z'], + ['sep 22nd 2014', '2014-09-22T00:00:00.000Z'], + ['apr 22nd 2014', '2014-04-22T00:00:00.000Z'], + ['June 22nd 1997', '1997-06-22T00:00:00.000Z'], + ['january 5th 1998', '1998-01-05T00:00:00.000Z'], + ['3rd of March 1969', '1969-03-03T00:00:00.000Z'], + ['2nd of April 1929', '1929-04-02T00:00:00.000Z'], + ['2nd of jul 1929', '1929-07-02T00:00:00.000Z'], + ['March 1969', '1969-03-01T00:00:00.000Z'], + ['jan 1921', '1921-01-01T00:00:00.000Z'], + ['March 18th', '2019-03-18T00:00:00.000Z'], + ['August 28th', '2019-08-28T00:00:00.000Z'], + ['18th of March', '2019-03-18T00:00:00.000Z'], + ['27th of March', '2019-03-27T00:00:00.000Z'], + ['february 10th', '2019-02-10T00:00:00.000Z'], + ['february 28th', '2019-02-28T00:00:00.000Z'], + ['first day of 2019', '2019-01-01T00:00:00.000Z'], + ['last day of 2019', '2019-12-31T00:00:00.000Z'], + ['7th hour of 2019', '2019-01-01T06:00:00.000Z'], + ['7th day of 2019', '2019-01-07T00:00:00.000Z'], + ['second quarter of 2019', '2019-04-01T00:00:00.000Z'], + ['30th minute of 2019', '2019-01-01T00:30:00.000Z'], + ['2019', '2019-01-01T00:00:00.000Z'], + ['2028', '2028-01-01T00:00:00.000Z'], + ['in 2028', '2028-01-01T00:00:00.000Z'], + ['2nd month in 2028', '2028-02-01T00:00:00.000Z'], + ['first day of march 2019', '2019-03-01T00:00:00.000Z'], + ['5th day of march 2019', '2019-03-05T00:00:00.000Z'], + ['5th day of q1 2002', '2002-01-05T00:00:00.000Z'], + ['5th hour of March 3rd 2002', '2002-03-03T04:00:00.000Z'], + ['last hour of March 2021', '2021-03-31T23:00:00.000Z'], + ['may to august 1996', '1996-05-01T00:00:00.000Z'], + ['half past 4', '2019-02-02T16:30:00.000Z'], + ['20 past 2', '2019-02-02T14:20:00.000Z'], + ['at 20 past', '2019-02-02T04:20:00.000Z'], + ['at half past', '2019-02-02T04:30:00.000Z'], + ['at quarter to', '2019-02-02T03:45:00.000Z'], + ['at quarter after', '2019-02-02T04:15:00.000Z'], + ['december 25th 2000', '2000-12-25T00:00:00.000Z'], + ['dec 25 2000', '2000-12-25T00:00:00.000Z'], + ['25th of december 2000', '2000-12-25T00:00:00.000Z'], + ['1st of january 2000', '2000-01-01T00:00:00.000Z'], + ['1 jan 2000', '2000-01-01T00:00:00.000Z'], + ['01/15/2000', '2000-01-15T00:00:00.000Z'], + ['15/01/2000', '2000-01-15T00:00:00.000Z'], + ['2000-06-15', '2000-06-15T00:00:00.000Z'], + ['june 15 2000', '2000-06-15T00:00:00.000Z'], + ['june 15th, 2000', '2000-06-15T00:00:00.000Z'], + ['15 june 2000', '2000-06-15T00:00:00.000Z'], + ['february 2nd 2019', '2019-02-02T00:00:00.000Z'], + ['feb 2 2019', '2019-02-02T00:00:00.000Z'], + ['2nd february 2019', '2019-02-02T00:00:00.000Z'], + ['march 3rd', '2019-03-03T00:00:00.000Z'], + ['march 3', '2019-03-03T00:00:00.000Z'], + ['3 march', '2019-03-03T00:00:00.000Z'], + ['3rd march', '2019-03-03T00:00:00.000Z'], + ['1st of february', '2019-02-01T00:00:00.000Z'], + ['2nd of february', '2019-02-02T00:00:00.000Z'], + ['february 1st', '2019-02-01T00:00:00.000Z'], + ['february 2nd', '2019-02-02T00:00:00.000Z'], + ['january 31st', '2020-01-31T00:00:00.000Z'], + ['january 1st', '2020-01-01T00:00:00.000Z'], + ['december 31st', '2019-12-31T00:00:00.000Z'], + ['last day of february 2019', '2019-02-28T00:00:00.000Z'], + ['first day of february 2019', '2019-02-01T00:00:00.000Z'], + ['last day of february', '2019-02-28T00:00:00.000Z'], + ['first day of february', '2019-02-01T00:00:00.000Z'], + ['last day of march 2019', '2019-03-31T00:00:00.000Z'], + ['first day of april 2019', '2019-04-01T00:00:00.000Z'], + ['3rd day of 2019', '2019-01-03T00:00:00.000Z'], + ['100th day of 2019', '2019-04-10T00:00:00.000Z'], + ['365th day of 2019', '2019-12-31T00:00:00.000Z'], + ['1st quarter of 2019', '2019-01-01T00:00:00.000Z'], + ['third quarter of 2019', '2019-07-01T00:00:00.000Z'], + ['q4 2019', '2019-10-01T00:00:00.000Z'], + ['q1 2019', '2019-01-01T00:00:00.000Z'], + ['first quarter 2019', '2019-01-01T00:00:00.000Z'], + ['second quarter 2019', '2019-04-01T00:00:00.000Z'], + ['fourth quarter 2019', '2019-10-01T00:00:00.000Z'], + ['january 2019', '2019-01-01T00:00:00.000Z'], + ['february 2019', '2019-02-01T00:00:00.000Z'], + ['march 2019', '2019-03-01T00:00:00.000Z'], + ['april 2019', '2019-04-01T00:00:00.000Z'], + ['may 2019', '2019-05-01T00:00:00.000Z'], + ['june 2019', '2019-06-01T00:00:00.000Z'], + ['july 2019', '2019-07-01T00:00:00.000Z'], + ['august 2019', '2019-08-01T00:00:00.000Z'], + ['september 2019', '2019-09-01T00:00:00.000Z'], + ['october 2019', '2019-10-01T00:00:00.000Z'], + ['november 2019', '2019-11-01T00:00:00.000Z'], + ['december 2019', '2019-12-01T00:00:00.000Z'], + ['jan 2019', '2019-01-01T00:00:00.000Z'], + ['feb 2019', '2019-02-01T00:00:00.000Z'], + ['mar 2019', '2019-03-01T00:00:00.000Z'], + ['apr 2019', '2019-04-01T00:00:00.000Z'], + ['in 2019', '2019-01-01T00:00:00.000Z'], + ['in 1990', '1990-01-01T00:00:00.000Z'], + ['2005', '2005-01-01T00:00:00.000Z'], + ['2nd month of 2019', '2019-02-01T00:00:00.000Z'], + ['3rd month of 2019', '2019-03-01T00:00:00.000Z'], + ['12th month of 2019', '2019-12-01T00:00:00.000Z'], + ['6th month in 2020', '2020-06-01T00:00:00.000Z'], + ['10th day of june 2019', '2019-06-10T00:00:00.000Z'], + ['15th day of march 2019', '2019-03-15T00:00:00.000Z'], + ['last day of march 2019', '2019-03-31T00:00:00.000Z'], + ['first hour of 2019', '2019-01-01T00:00:00.000Z'], + ['last hour of 2019', '2019-12-31T23:00:00.000Z'], + ['first minute of 2019', '2019-01-01T00:01:00.000Z'], + ['last minute of 2019', '2019-12-31T23:59:00.000Z'], + ['5th hour of february 2nd 2019', '2019-02-02T04:00:00.000Z'], + ['noon on february 2nd 2019', '2019-02-02T12:00:00.000Z'], + ['midnight on february 2nd 2019', '2019-02-02T00:00:00.000Z'], + ['january to march 2019', '2019-01-01T00:00:00.000Z'], + ['march to may 2019', '2019-03-01T00:00:00.000Z'], + ['june to august 2019', '2019-06-01T00:00:00.000Z'], + ['september to november 2019', '2019-09-01T00:00:00.000Z'], + ['jan to mar 2019', '2019-01-01T00:00:00.000Z'], + ['feb to apr 2019', '2019-02-01T00:00:00.000Z'], + ['today', '2019-02-02T00:00:00.000Z'], + ['tomorrow', '2019-02-03T00:00:00.000Z'], + ['yesterday', '2019-02-01T00:00:00.000Z'], + ['next monday', '2019-02-04T00:00:00.000Z'], + ['last monday', '2019-01-21T00:00:00.000Z'], + ['this monday', '2019-02-04T00:00:00.000Z'], + ['next friday', '2019-02-08T00:00:00.000Z'], + ['last friday', '2019-01-25T00:00:00.000Z'], + ['next week', '2019-02-04T00:00:00.000Z'], + ['last week', '2019-01-21T00:00:00.000Z'], + ['this week', '2019-01-28T00:00:00.000Z'], + ['next month', '2019-03-01T00:00:00.000Z'], + ['last month', '2019-01-01T00:00:00.000Z'], + ['this month', '2019-02-01T00:00:00.000Z'], + ['next year', '2020-01-01T00:00:00.000Z'], + ['last year', '2018-01-01T00:00:00.000Z'], + ['this year', '2019-01-01T00:00:00.000Z'], + ['in 3 days', '2019-02-05T00:00:00.000Z'], + ['in 2 weeks', '2019-02-16T00:00:00.000Z'], + ['in 1 month', '2019-03-02T00:00:00.000Z'], + ['in 1 year', '2020-02-02T00:00:00.000Z'], + ['3 days ago', '2019-01-30T00:00:00.000Z'], + ['2 weeks ago', '2019-01-19T00:00:00.000Z'], + ['1 month ago', '2019-01-02T00:00:00.000Z'], + ['1 year ago', '2018-02-02T00:00:00.000Z'], + ['at 4pm', '2019-02-02T16:00:00.000Z'], + ['at 4:30pm', '2019-02-02T16:30:00.000Z'], + ['4pm', '2019-02-02T16:00:00.000Z'], + ['4:30pm', '2019-02-02T16:30:00.000Z'], + ['at noon', '2019-02-02T12:00:00.000Z'], + ['at midnight', '2019-02-02T00:00:00.000Z'], + ['noon', '2019-02-02T12:00:00.000Z'], + ['midnight', '2019-02-02T00:00:00.000Z'], + ['ten past 4', '2019-02-02T16:10:00.000Z'], + ['quarter past 3', '2019-02-02T15:15:00.000Z'], + ['half past 3', '2019-02-02T15:30:00.000Z'], + ['quarter to 5', '2019-02-02T16:45:00.000Z'], + ['at ten past 4', '2019-02-02T16:10:00.000Z'], + ['at quarter past 3', '2019-02-02T15:15:00.000Z'], + ['5 past 4', '2019-02-02T16:05:00.000Z'], + ['february 2nd at 4pm', '2019-02-02T16:00:00.000Z'], + ['feb 2 at 4:30pm', '2019-02-02T16:30:00.000Z'], + ['march 18th at 9am', '2019-03-18T09:00:00.000Z'], + ['march 18th at noon', '2019-03-18T12:00:00.000Z'], + ['the 18th of march', '2019-03-18T00:00:00.000Z'], + ['the first of january 2020', '2020-01-01T00:00:00.000Z'], + ['end of 2019', '2019-12-31T23:59:59.999Z'], + ['beginning of 2019', '2019-01-01T00:00:00.000Z'], + ['start of 2019', '2019-01-01T00:00:00.000Z'], + ['middle of february 2019', '2019-02-15T00:00:00.000Z'], + ['mid february 2019', '2019-02-01T00:00:00.000Z'], + ['spring 2019', '2019-03-01T00:00:00.000Z'], + ['summer 2019', '2019-06-01T00:00:00.000Z'], + ['fall 2019', '2019-09-01T00:00:00.000Z'], + ['winter 2019', '2019-12-01T00:00:00.000Z'], + ['spring of 2019', '2019-03-01T00:00:00.000Z'], + ['christmas 2019', '2019-12-25T00:00:00.000Z'], + ['christmas 2000', '2000-12-25T00:00:00.000Z'], + ['easter 2019', '2019-04-21T00:00:00.000Z'], + ['thanksgiving 2019', '2019-11-28T00:00:00.000Z'], + ['monday', '2019-02-04T00:00:00.000Z'], + ['tuesday', '2019-02-05T00:00:00.000Z'], + ['wednesday', '2019-02-06T00:00:00.000Z'], + ['thursday', '2019-02-07T00:00:00.000Z'], + ['friday', '2019-02-08T00:00:00.000Z'], + ['saturday', '2019-02-02T00:00:00.000Z'], + ['sunday', '2019-02-03T00:00:00.000Z'], + ['monday february 4th', '2019-02-04T00:00:00.000Z'], + ['february 4th monday', '2019-02-04T00:00:00.000Z'], + ['2019-02-02', '2019-02-02T00:00:00.000Z'], + ['02/02/2019', '2019-02-02T00:00:00.000Z'], + ['2/2/2019', '2019-02-02T00:00:00.000Z'], + ['february 2', '2019-02-02T00:00:00.000Z'], + ['2 february 2019', '2019-02-02T00:00:00.000Z'], + ['5th hour of 2020', '2020-01-01T04:00:00.000Z'], + ['10th day of 2020', '2020-01-10T00:00:00.000Z'], + ['last day of q1 2019', '2019-03-31T00:00:00.000Z'], + ['first day of q2 2019', '2019-04-01T00:00:00.000Z'], + ['5th day of q2 2019', '2019-04-05T00:00:00.000Z'], + ['last hour of february 2019', '2019-02-28T23:00:00.000Z'], + ['first hour of march 2019', '2019-03-01T00:00:00.000Z'], + ['august 1st 2019', '2019-08-01T00:00:00.000Z'], + ['sept 1 2019', '2019-09-01T00:00:00.000Z'], + ['oct 31 2019', '2019-10-31T00:00:00.000Z'], + ['nov 30th 2019', '2019-11-30T00:00:00.000Z'], + ['dec 1 2019', '2019-12-01T00:00:00.000Z'], + ['july 4th 1776', '1776-07-04T00:00:00.000Z'], + ['july 4 1776', '1776-07-04T00:00:00.000Z'], + ['4th of july 1776', '1776-07-04T00:00:00.000Z'], + ['4 july 1776', '1776-07-04T00:00:00.000Z'], + // ['august to may 1996', '1996-05-01T00:00:00.000Z'], + ] + arr.forEach(function (a) { + const json = nlp(a[0]).dates(context).json()[0] || {} + t.equal(json.dates.start, a[1], a[0]) + }) + t.end() +})