Skip to content

Commit

Permalink
feat: Updating to 2.0.0-alpha.20 of date-fns
Browse files Browse the repository at this point in the history
date-fns contains lots of API improvements in 2.0.0, including better
support for weeks.
  • Loading branch information
aholstenson committed Oct 4, 2018
1 parent 0627663 commit fca8318
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 72 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions test/language/en/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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
})
);
});
Expand Down
59 changes: 29 additions & 30 deletions time/dates.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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'
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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') {
Expand Down
28 changes: 14 additions & 14 deletions time/intervals.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions time/months.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
16 changes: 8 additions & 8 deletions time/times.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
6 changes: 3 additions & 3 deletions time/years.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down

0 comments on commit fca8318

Please sign in to comment.