diff --git a/package-lock.json b/package-lock.json index 468d75d..bf4663f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ics", - "version": "2.25.1", + "version": "2.26.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1761,7 +1761,8 @@ "dayjs": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.9.1.tgz", - "integrity": "sha512-01NCTBg8cuMJG1OQc6PR7T66+AFYiPwgDvdJmvJBn29NGzIG+DIFxPLNjHzwz3cpFIvG+NcwIjP9hSaPVoOaDg==" + "integrity": "sha512-01NCTBg8cuMJG1OQc6PR7T66+AFYiPwgDvdJmvJBn29NGzIG+DIFxPLNjHzwz3cpFIvG+NcwIjP9hSaPVoOaDg==", + "dev": true }, "debug": { "version": "4.2.0", diff --git a/package.json b/package.json index 9364593..e208857 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ics", - "version": "2.25.1", + "version": "2.26.0", "description": "iCal (ics) file generator", "main": "index.js", "types": "index.d.ts", @@ -35,6 +35,7 @@ "@babel/preset-env": "^7.6.2", "@babel/register": "^7.6.2", "chai": "^4.2.0", + "dayjs": "^1.8.33", "gulp": "^4.0.2", "gulp-babel": "^8.0.0", "mocha": "^6.2.1", @@ -42,8 +43,7 @@ }, "dependencies": { "@hapi/joi": "^17.1.1", - "uuid": "^3.3.3", - "dayjs": "^1.8.33" + "uuid": "^3.3.3" }, "files": [ "dist/", diff --git a/src/defaults.js b/src/defaults.js index 5480dd4..5fd3cc0 100644 --- a/src/defaults.js +++ b/src/defaults.js @@ -1,26 +1,13 @@ import uuidv1 from 'uuid/v1' -import dayjs from 'dayjs' -import utc from 'dayjs/plugin/utc'; -import { formatUTCDateAsUTC } from './utils' - -dayjs.extend(utc); - -const now = dayjs().utc() +import { formatDate } from './utils' const defaults = { title: 'Untitled event', productId: 'adamgibbons/ics', method: 'PUBLISH', uid: uuidv1(), - timestamp: formatUTCDateAsUTC([ - now.get('year'), - now.get('month') + 1, - now.get('date'), - now.get('hours'), - now.get('minutes'), - now.get('seconds') - ]), - start: formatUTCDateAsUTC() + timestamp: formatDate(null, 'utc'), + start: formatDate(null, 'utc') } export default defaults diff --git a/src/utils/format-date.js b/src/utils/format-date.js index 842cfed..34dcff7 100644 --- a/src/utils/format-date.js +++ b/src/utils/format-date.js @@ -1,36 +1,41 @@ -import dayjs from 'dayjs' -import { - formatLocalDateAsLocal, - formatLocalDateAsUTC, - formatUTCDateAsLocal, - formatUTCDateAsUTC -} from './index' - -function formatLocalDate(args = [], outputType) { - if (outputType == 'utc') { - return formatLocalDateAsUTC(args, outputType) - } - return formatLocalDateAsLocal(args, outputType) -} - -function formatUTCDate(args = [], outputType) { - if (outputType == 'utc') { - return formatUTCDateAsUTC(args, outputType) - } - return formatUTCDateAsLocal(args, outputType) -} +const pad = n => n < 10 ? `0${n}` : `${n}` export default function formatDate(args = [], outputType = 'utc', inputType = 'local') { - const [year, month, date, hours, minutes, seconds] = args + if (Array.isArray(args) && args.length === 3) { + const [year, month, date] = args + return `${year}${pad(month)}${pad(date)}` + } - if (args.length === 3) { - return dayjs(new Date(year, month - 1, date)).format('YYYYMMDD') + let outDate = new Date(new Date().setUTCSeconds(0, 0)) + if (Array.isArray(args) && args.length > 0 && args[0]) { + const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args + if (inputType === 'local') { + outDate = new Date(year, month - 1, date, hours, minutes, seconds) + } else { + outDate = new Date(Date.UTC(year, month - 1, date, hours, minutes, seconds)) + } } - if (inputType === 'local') { - return formatLocalDate([year, month, date, hours, minutes, seconds || 0], outputType); + if (outputType === 'local') { + return [ + outDate.getFullYear(), + pad(outDate.getMonth() + 1), + pad(outDate.getDate()), + 'T', + pad(outDate.getHours()), + pad(outDate.getMinutes()), + pad(outDate.getSeconds()) + ].join('') } - // type === 'utc' - return formatUTCDate([year, month, date, hours, minutes, seconds || 0], outputType); + return [ + outDate.getUTCFullYear(), + pad(outDate.getUTCMonth() + 1), + pad(outDate.getUTCDate()), + 'T', + pad(outDate.getUTCHours()), + pad(outDate.getUTCMinutes()), + pad(outDate.getUTCSeconds()), + 'Z' + ].join('') } diff --git a/src/utils/format-local-date-as-local.js b/src/utils/format-local-date-as-local.js deleted file mode 100644 index 545e1ed..0000000 --- a/src/utils/format-local-date-as-local.js +++ /dev/null @@ -1,26 +0,0 @@ -// -// DATE-TIME -// FORM #1: DATE WITH LOCAL TIME -// -// The date with local time form is simply a DATE-TIME value that -// does not contain the UTC designator nor does it reference a time -// zone. DATE-TIME values of this type are said to be "floating" -// and are not bound to any time zone in particular. -// -// For example, the following represents -// January 18, 1998, at 11 PM: -// -// 19980118T230000 -// - -import dayjs from 'dayjs' - -export default function formatLocalDateAsLocal(args = []) { - if (args.length > 0) { - const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args - const formattedDate = dayjs(new Date(year, month - 1, date, hours, minutes, seconds)).format('YYYYMMDDTHHmmss') - return formattedDate - } - - return dayjs().format('YYYYMMDDTHHmmss') -} diff --git a/src/utils/format-local-date-as-utc.js b/src/utils/format-local-date-as-utc.js deleted file mode 100644 index 9b7c6a9..0000000 --- a/src/utils/format-local-date-as-utc.js +++ /dev/null @@ -1,37 +0,0 @@ -// -// FORM #2: DATE WITH UTC TIME -// -// The date with UTC time, or absolute time, is identified by a LATIN -// CAPITAL LETTER Z suffix character, the UTC designator, appended to -// the time value. For example, the following represents January 19, -// 1998, at 0700 UTC: -// -// 19980119T070000Z -// -// The "TZID" property parameter MUST NOT be applied to DATE-TIME -// properties whose time values are specified in UTC. -// - -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; - -dayjs.extend(utc); - -export default function formatLocalDateAsUTC(args = []) { - if (args.length > 0) { - const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args - - const formattedDate = dayjs(new Date( - year, - month - 1, - date, - hours, - minutes, - seconds - )).utc().format('YYYYMMDDTHHmmss') + 'Z' - - return formattedDate - } - - return dayjs.utc().format('YYYYMMDDTHHmmss') + 'Z' -} diff --git a/src/utils/format-utc-date-as-local.js b/src/utils/format-utc-date-as-local.js deleted file mode 100644 index 6d7bb57..0000000 --- a/src/utils/format-utc-date-as-local.js +++ /dev/null @@ -1,29 +0,0 @@ -// -// DATE-TIME -// FORM #1: DATE WITH LOCAL TIME -// -// The date with local time form is simply a DATE-TIME value that -// does not contain the UTC designator nor does it reference a time -// zone. DATE-TIME values of this type are said to be "floating" -// and are not bound to any time zone in particular. -// -// For example, the following represents -// January 18, 1998, at 11 PM: -// -// 19980118T230000 -// - -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; - -dayjs.extend(utc); - -export default function formatLocalDateAsLocal(args = []) { - if (args.length > 0) { - const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args - const formattedDate = dayjs.utc(Date.UTC(year, month - 1, date, hours, minutes, seconds)).format('YYYYMMDDTHHmmss') - return formattedDate - } - - return dayjs().utc().format('YYYYMMDDTHHmmss') -} diff --git a/src/utils/format-utc-date-as-utc.js b/src/utils/format-utc-date-as-utc.js deleted file mode 100644 index bd5863a..0000000 --- a/src/utils/format-utc-date-as-utc.js +++ /dev/null @@ -1,31 +0,0 @@ -// -// FORM #2: DATE WITH UTC TIME -// -// The date with UTC time, or absolute time, is identified by a LATIN -// CAPITAL LETTER Z suffix character, the UTC designator, appended to -// the time value. For example, the following represents January 19, -// 1998, at 0700 UTC: -// -// 19980119T070000Z -// -// The "TZID" property parameter MUST NOT be applied to DATE-TIME -// properties whose time values are specified in UTC. -// - -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; - -dayjs.extend(utc); - -export default function formatUTCDateAsUTC(args = []) { - if (args.length > 0) { - const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args - - const formattedDate = dayjs.utc(Date.UTC(year, month - 1, date, hours, minutes, seconds)) - .format('YYYYMMDDTHHmmss') + 'Z' - - return formattedDate - } - - return dayjs.utc().format('YYYYMMDDTHHmm00') + 'Z' -} diff --git a/src/utils/index.js b/src/utils/index.js index 4b5663c..0ecdd04 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,8 +1,4 @@ import formatDate from './format-date' -import formatLocalDateAsUTC from './format-local-date-as-utc' -import formatLocalDateAsLocal from './format-local-date-as-local' -import formatUTCDateAsUTC from './format-utc-date-as-utc' -import formatUTCDateAsLocal from './format-utc-date-as-local' import setGeolocation from './set-geolocation' import setContact from './set-contact' import setOrganizer from './set-organizer' @@ -15,10 +11,6 @@ import setLocation from './set-location' export { formatDate, - formatLocalDateAsUTC, - formatLocalDateAsLocal, - formatUTCDateAsUTC, - formatUTCDateAsLocal, setGeolocation, setContact, setOrganizer, diff --git a/test/pipeline/format.spec.js b/test/pipeline/format.spec.js index baa4768..dd1e830 100644 --- a/test/pipeline/format.spec.js +++ b/test/pipeline/format.spec.js @@ -85,7 +85,7 @@ describe('pipeline.formatEvent', () => { it('writes a start date-time, taking the given date as UTC if requested and outputting is as Local (floating) if requested', () => { const event = buildEvent({ start: [2017, 5, 15, 10, 0], startInputType: 'utc', startOutputType: 'local' }) const formattedEvent = formatEvent(event) - const now = dayjs(new Date(2017, 5 - 1, 15, 10, 0)).format('YYYYMMDDTHHmm00') + const now = dayjs(Date.UTC(2017, 5 - 1, 15, 10, 0)).format('YYYYMMDDTHHmm00') expect(formattedEvent).to.contain('DTSTART:'+now) expect(formattedEvent).to.not.contain('DTSTART:'+now+'Z') }) diff --git a/test/utils/format-date.spec.js b/test/utils/format-date.spec.js index 604eab2..9f08149 100644 --- a/test/utils/format-date.spec.js +++ b/test/utils/format-date.spec.js @@ -1,17 +1,36 @@ import dayjs from 'dayjs' +import utc from 'dayjs/plugin/utc' + +dayjs.extend(utc) import { formatDate } from '../../src/utils' import { expect } from 'chai' -describe('utils.formatDate', () => { +describe('utils.formatDate', () => { it('defaults to local time input and UTC time output when no type passed', () => { const now = dayjs(new Date(2017, 7-1, 16, 22, 30)).utc().format('YYYYMMDDTHHmm00') expect(formatDate([2017, 7, 16, 22, 30])).to.equal(now+'Z') }) it('sets a local (i.e. floating) time when specified', () => { - expect(formatDate([1998, 1, 18, 23, 0], 'local')).to.equal('19980118T230000') + expect(formatDate([1998, 6, 18, 23, 0], 'local', 'local')).to.equal('19980618T230000') }) it('sets a date value when passed only three args', () => { expect(formatDate([2018, 2, 11])).to.equal('20180211') }) + it('defaults to NOW in UTC date-time when no args passed', () => { + const now = dayjs().utc().format('YYYYMMDDTHHmm00') + 'Z' + expect(formatDate(undefined, 'utc')).to.equal(now) + }) + it('sets a UTC date-time when passed well-formed args', () => { + expect(formatDate([2017, 9, 25, 0, 30], 'utc', 'utc')).to.equal('20170925T003000Z') + expect(formatDate([2017, 1, 31], 'utc','utc')).to.equal('20170131') + }) + it('sets a local DATE-TIME value to NOW when passed nothing', () => { + const now = dayjs().format('YYYYMMDDTHHmm00') + expect(formatDate(undefined, 'local', 'local')).to.equal(now) + }) + it('sets a local DATE-TIME value when passed args', () => { + expect(formatDate([1998, 1, 18, 23, 9, 59], 'local', 'local')) + .to.equal('19980118T230959') + }) }) diff --git a/test/utils/format-local-date-as-local.spec.js b/test/utils/format-local-date-as-local.spec.js deleted file mode 100644 index 055fecf..0000000 --- a/test/utils/format-local-date-as-local.spec.js +++ /dev/null @@ -1,19 +0,0 @@ -// FORM #1: DATE WITH LOCAL TIME - -import dayjs from 'dayjs' -import { formatLocalDateAsLocal } from '../../src/utils' -import { expect } from 'chai' - -describe('utils.formatLocalDateAsLocal', () => { - it('exists', () => { - expect(formatLocalDateAsLocal).to.exist - }) - it('sets a DATE-TIME value to NOW when passed nothing', () => { - const now = dayjs().format('YYYYMMDDTHHmmss') - expect(formatLocalDateAsLocal()).to.equal(now) - }) - it('sets a DATE-TIME value when passed args', () => { - expect(formatLocalDateAsLocal([1998, 1, 18, 23, 9, 59])) - .to.equal('19980118T230959') - }) -}) diff --git a/test/utils/format-utc-date-as-utc.spec.js b/test/utils/format-utc-date-as-utc.spec.js deleted file mode 100644 index 1b3729a..0000000 --- a/test/utils/format-utc-date-as-utc.spec.js +++ /dev/null @@ -1,15 +0,0 @@ -import dayjs from 'dayjs' -import { formatUTCDateAsUTC } from '../../src/utils' -import { expect } from 'chai' - -describe('utils.formatUTCDateAsUTC', () => { - it('defaults to NOW in UTC date-time when no args passed', () => { - const now = dayjs().utc().format('YYYYMMDDTHHmm00') + 'Z' - expect(now).to.equal(formatUTCDateAsUTC()) - }) - - it('sets a UTC date-time when passed well-formed args', () => { - expect(formatUTCDateAsUTC([2017, 9, 25, 0, 30])).to.equal('20170925T003000Z') - expect(formatUTCDateAsUTC([2017, 1, 31])).to.equal('20170131T000000Z') - }) -})