Skip to content

Commit

Permalink
merge #151
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgibbons committed Oct 24, 2020
1 parent f031719 commit 36a4984
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 217 deletions.
5 changes: 3 additions & 2 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -35,15 +35,15 @@
"@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",
"yargs-parser": ">=13.1.2"
},
"dependencies": {
"@hapi/joi": "^17.1.1",
"uuid": "^3.3.3",
"dayjs": "^1.8.33"
"uuid": "^3.3.3"
},
"files": [
"dist/",
Expand Down
19 changes: 3 additions & 16 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -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
61 changes: 33 additions & 28 deletions src/utils/format-date.js
Original file line number Diff line number Diff line change
@@ -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('')
}
26 changes: 0 additions & 26 deletions src/utils/format-local-date-as-local.js

This file was deleted.

37 changes: 0 additions & 37 deletions src/utils/format-local-date-as-utc.js

This file was deleted.

29 changes: 0 additions & 29 deletions src/utils/format-utc-date-as-local.js

This file was deleted.

31 changes: 0 additions & 31 deletions src/utils/format-utc-date-as-utc.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -15,10 +11,6 @@ import setLocation from './set-location'

export {
formatDate,
formatLocalDateAsUTC,
formatLocalDateAsLocal,
formatUTCDateAsUTC,
formatUTCDateAsLocal,
setGeolocation,
setContact,
setOrganizer,
Expand Down
2 changes: 1 addition & 1 deletion test/pipeline/format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down
23 changes: 21 additions & 2 deletions test/utils/format-date.spec.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
19 changes: 0 additions & 19 deletions test/utils/format-local-date-as-local.spec.js

This file was deleted.

15 changes: 0 additions & 15 deletions test/utils/format-utc-date-as-utc.spec.js

This file was deleted.

0 comments on commit 36a4984

Please sign in to comment.