From fca8318ce060e8676ac6331a78279bcda86b95e1 Mon Sep 17 00:00:00 2001 From: Andreas Holstenson Date: Thu, 4 Oct 2018 13:21:54 +0200 Subject: [PATCH] feat: Updating to 2.0.0-alpha.20 of `date-fns` date-fns contains lots of API improvements in 2.0.0, including better support for weeks. --- README.md | 34 +++++++++++++++++--- package-lock.json | 6 ++-- package.json | 2 +- test/language/en/date.test.js | 8 ++--- time/dates.js | 59 +++++++++++++++++------------------ time/intervals.js | 28 ++++++++--------- time/months.js | 8 ++--- time/times.js | 16 +++++----- time/years.js | 6 ++-- 9 files changed, 95 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index bfacaef..4c35af3 100644 --- a/README.md +++ b/README.md @@ -68,11 +68,35 @@ intents.match('turn lights', { partial: true }) ## Options -Option | Default | Description ----------------|--------------|------------- -`partial` | `false` | If partial matching should be performed -`now` | `new Date()` | Date to use as a base for times and dates parsed -`weekStartsOn` | `0` (Sunday) | The day the week starts on +Option | Default | Description +------------------------|--------------|------------- +`partial` | `false` | If partial matching should be performed +`now` | `new Date()` | Date to use as a base for times and dates parsed +`weekStartsOn` | `0` (Sunday) | The day the week starts on +`firstWeekContainsDate` | `1` | The day of January which is always in the first week of the year. + +### A note about weeks + +It's important to set `weekStartsOn` and `firstWeekContainsDate` to something +expected by the user. The default value for `weekStartsOn` is `0` which +indicates that weeks start on Sunday. + +`firstWeekContainsDate` defaults to `1` which is commonly used in North America +and Islamic date systems. Countries that use this week numbering include +Canada, United States, India, Japan, Taiwan, Hong Kong, Macau, Israel, +Egypt, South Africa, the Phillippines and most of Latin America. + +For EU countries most of them use Mondays as the start of the week and the ISO +week system. Settings `weekStartsOn` to `1` and `firstWeekContainsDate` to `4` +will set weeks to a style used in EU and most other European countries, most +of Acia and Oceania. + +Middle Eastern countries commonly use Saturday as their first day of week and +a week numbering system where the first week of the year contains January 1st. +Set `weekStartsOn` to `6` and `firstWeekContainsDate` to `1` to use this +style of week. + +For more information about week numbering see the [Week article on Wikipedia](https://en.wikipedia.org/wiki/Week#Week_numbering). ## Values diff --git a/package-lock.json b/package-lock.json index 425b9a8..77d1729 100644 --- a/package-lock.json +++ b/package-lock.json @@ -478,9 +478,9 @@ } }, "date-fns": { - "version": "1.29.0", - "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-1.29.0.tgz", - "integrity": "sha512-lbTXWZ6M20cWH8N9S6afb0SBm6tMk+uUg6z3MqHPKE9atmsY3kJkTm8vKe93izJ2B2+q5MV990sM2CHgtAZaOw==" + "version": "2.0.0-alpha.20", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.0.0-alpha.20.tgz", + "integrity": "sha512-6QqnkCeMBrlnGUXKrNMvYbj0h9Nv+51hNxwg54aa4Z7JWVycssKINjoPbc56tV0qTQ27hWTLz3ixi4Pigpq07Q==" }, "debug": { "version": "4.0.1", diff --git a/package.json b/package.json index e5d5bde..d4ffb0a 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "node": ">=6.0.0" }, "dependencies": { - "date-fns": "^1.29.0", + "date-fns": "^2.0.0-alpha.20", "fast-clone": "^1.5.3", "lodash.isequal": "^4.5.0", "talisman": "^0.21.0" diff --git a/test/language/en/date.test.js b/test/language/en/date.test.js index 8f89ca9..f62af00 100644 --- a/test/language/en/date.test.js +++ b/test/language/en/date.test.js @@ -335,7 +335,7 @@ describe('English', function() { }); it('this week - Sunday start', function() { - return date('this week', { now: new Date(2010, 0, 1), weekStartsOn: 7 }) + return date('this week', { now: new Date(2010, 0, 1), weekStartsOn: 0 }) .then(v => expect(v).to.deep.equal({ period: 'week', @@ -347,7 +347,7 @@ describe('English', function() { }); it('end of week - Sunday start', function() { - return date('end of week', { now: new Date(2010, 0, 1), weekStartsOn: 7 }) + return date('end of week', { now: new Date(2010, 0, 1), weekStartsOn: 0 }) .then(v => expect(v).to.deep.equal({ period: 'week', @@ -627,13 +627,13 @@ describe('English', function() { }); it('week 12 in 1 year', function() { - return date('week 12 in 1 year', { now: new Date(2010, 0, 1) }) + return date('week 12 in 1 year', { now: new Date(2010, 0, 1), weekStartsOn: 1, firstWeekContainsDate: 4 }) .then(v => expect(v).to.deep.equal({ period: 'week', year: 2011, month: 2, - day: 27 + day: 21 }) ); }); diff --git a/time/dates.js b/time/dates.js index 6d4624b..aecdc07 100644 --- a/time/dates.js +++ b/time/dates.js @@ -1,23 +1,23 @@ 'use strict'; -const addMonths = require('date-fns/add_months') -const addWeeks = require('date-fns/add_weeks'); -const addDays = require('date-fns/add_days') -const addYears = require('date-fns/add_years'); +const addMonths = require('date-fns/addMonths') +const addWeeks = require('date-fns/addWeeks'); +const addDays = require('date-fns/addDays') +const addYears = require('date-fns/addYears'); -const setISODay = require('date-fns/set_iso_day'); -const getISODay = require('date-fns/get_iso_day'); +const setISODay = require('date-fns/setISODay'); +const getISODay = require('date-fns/getISODay'); -const setISOWeek = require('date-fns/set_iso_week'); -const getISOWeek = require('date-fns/get_iso_week'); +const setWeek = require('date-fns/setWeek'); +const getWeek = require('date-fns/getWeek'); -const setYear = require('date-fns/set_year'); -const setMonth = require('date-fns/set_month'); -const setDate = require('date-fns/set_date'); +const setYear = require('date-fns/setYear'); +const setMonth = require('date-fns/setMonth'); +const setDate = require('date-fns/setDate'); -const startOfYear = require('date-fns/start_of_year'); -const startOfWeek = require('date-fns/start_of_week'); -const startOfMonth = require('date-fns/start_of_month'); +const startOfYear = require('date-fns/startOfYear'); +const startOfWeek = require('date-fns/startOfWeek'); +const startOfMonth = require('date-fns/startOfMonth'); const currentTime = require('./currentTime'); const DateValue = require('./date-value'); @@ -27,8 +27,8 @@ const { toStart, toEnd } = require('./intervals'); const WEEK = { field: 'week', - get: getISOWeek, - set: setISOWeek, + get: getWeek, + set: setWeek, adjuster: addYears, parentData: r => typeof r.year !== 'undefined' @@ -66,34 +66,33 @@ const DAY = { * @param {*} def * definition describing the field to modify */ -function adjust(time, r, def) { +function adjust(r, e, time, def) { const requested = r[def.field]; - const current = def.get(time); + const current = def.get(time, e.options); if(r.relationToCurrent === 'auto') { if(requested < current) { - time = def.set(def.adjuster(time, 1), requested); + time = def.set(def.adjuster(time, 1, e.options), requested, e.options); } else { - time = def.set(time, requested); + time = def.set(time, requested, e.options); } } else if(r.relationToCurrent === 'current-period') { - // TODO: Does all interval if(def.parentData(r) && requested < current) { - time = def.set(def.adjuster(time, 1), requested); + time = def.set(def.adjuster(time, 1, e.options), requested, e.options); } else { - time = def.set(time, requested); + time = def.set(time, requested, e.options); } } else if(r.relationToCurrent === 'future') { if(requested <= current) { - time = def.set(def.adjuster(time, 1), requested); + time = def.set(def.adjuster(time, 1, e.options), requested, e.options); } else { - time = def.set(time, requested); + time = def.set(time, requested, e.options); } } else if(r.relationToCurrent === 'past') { if(requested >= current) { - time = def.set(def.adjuster(time, -1), requested); + time = def.set(def.adjuster(time, -1, e.options), requested, e.options); } else { - time = def.set(time, requested); + time = def.set(time, requested, e.options); } } @@ -141,7 +140,7 @@ module.exports.map = function(r, e, options={}) { // Exact week - set it and reset to start of week result.period = 'week'; - time = adjust(time, r, WEEK); + time = adjust(r, e, time, WEEK); time = startOfWeek(time, e.options); } @@ -154,7 +153,7 @@ module.exports.map = function(r, e, options={}) { // Exact month - set the day to the start of the month result.period = 'month'; - time = adjust(time, r, MONTH); + time = adjust(r, e, time, MONTH); time = startOfMonth(time); } @@ -167,7 +166,7 @@ module.exports.map = function(r, e, options={}) { // If there is an explicit day set it result.period = 'day'; - time = adjust(time, r, DAY); + time = adjust(r, e, time, DAY); } if(typeof r.dayOfWeek !== 'undefined') { diff --git a/time/intervals.js b/time/intervals.js index 79fcd24..79b8982 100644 --- a/time/intervals.js +++ b/time/intervals.js @@ -1,20 +1,20 @@ 'use strict'; -const startOfYear = require('date-fns/start_of_year'); -const startOfWeek = require('date-fns/start_of_week'); -const startOfMonth = require('date-fns/start_of_month'); -const startOfDay = require('date-fns/start_of_day'); -const startOfHour = require('date-fns/start_of_hour'); -const startOfMinute = require('date-fns/start_of_minute'); -const startOfSecond = require('date-fns/start_of_second'); +const startOfYear = require('date-fns/startOfYear'); +const startOfWeek = require('date-fns/startOfWeek'); +const startOfMonth = require('date-fns/startOfMonth'); +const startOfDay = require('date-fns/startOfDay'); +const startOfHour = require('date-fns/startOfHour'); +const startOfMinute = require('date-fns/startOfMinute'); +const startOfSecond = require('date-fns/startOfSecond'); -const endOfYear = require('date-fns/end_of_year'); -const endOfWeek = require('date-fns/end_of_week'); -const endOfMonth = require('date-fns/end_of_month'); -const endOfDay = require('date-fns/end_of_day'); -const endOfHour = require('date-fns/end_of_hour'); -const endOfMinute = require('date-fns/end_of_minute'); -const endOfSecond = require('date-fns/end_of_second'); +const endOfYear = require('date-fns/endOfYear'); +const endOfWeek = require('date-fns/endOfWeek'); +const endOfMonth = require('date-fns/endOfMonth'); +const endOfDay = require('date-fns/endOfDay'); +const endOfHour = require('date-fns/endOfHour'); +const endOfMinute = require('date-fns/endOfMinute'); +const endOfSecond = require('date-fns/endOfSecond'); module.exports.toStart = function(time, period, options=undefined) { switch(period) { diff --git a/time/months.js b/time/months.js index b837761..a21e7ad 100644 --- a/time/months.js +++ b/time/months.js @@ -1,10 +1,10 @@ 'use strict'; -const setYear = require('date-fns/set_year'); +const setYear = require('date-fns/setYear'); -const addMonths = require('date-fns/add_months') -const setMonth = require('date-fns/set_month'); -const startOfMonth = require('date-fns/start_of_month'); +const addMonths = require('date-fns/addMonths') +const setMonth = require('date-fns/setMonth'); +const startOfMonth = require('date-fns/startOfMonth'); const currentTime = require('./currentTime'); const DateValue = require('./date-value'); diff --git a/time/times.js b/time/times.js index 89ac36e..29e4ac9 100644 --- a/time/times.js +++ b/time/times.js @@ -1,15 +1,15 @@ 'use strict'; -const addHours = require('date-fns/add_hours') -const addMinutes = require('date-fns/add_minutes'); -const addSeconds = require('date-fns/add_seconds') +const addHours = require('date-fns/addHours') +const addMinutes = require('date-fns/addMinutes'); +const addSeconds = require('date-fns/addSeconds') -const setHours = require('date-fns/set_hours') -const setMinutes = require('date-fns/set_minutes') -const setSeconds = require('date-fns/set_seconds') +const setHours = require('date-fns/setHours') +const setMinutes = require('date-fns/setMinutes') +const setSeconds = require('date-fns/setSeconds') -const startOfHour = require('date-fns/start_of_hour'); -const startOfMinute = require('date-fns/start_of_minute'); +const startOfHour = require('date-fns/startOfHour'); +const startOfMinute = require('date-fns/startOfMinute'); const currentTime = require('./currentTime'); const DateValue = require('./date-value'); diff --git a/time/years.js b/time/years.js index 90ce123..9d170db 100644 --- a/time/years.js +++ b/time/years.js @@ -1,8 +1,8 @@ 'use strict'; -const addYears = require('date-fns/add_years'); -const setYear = require('date-fns/set_year'); -const startOfYear = require('date-fns/start_of_year'); +const addYears = require('date-fns/addYears'); +const setYear = require('date-fns/setYear'); +const startOfYear = require('date-fns/startOfYear'); const currentTime = require('./currentTime'); const DateValue = require('./date-value');