Przeglądaj źródła

tweak moment plugin. basics of luxon plugin

Adam Shaw 7 lat temu
rodzic
commit
068db61770

+ 2 - 0
karma.config.js

@@ -20,6 +20,7 @@ module.exports = function(config) {
       'node_modules/moment/moment.js',
       'node_modules/moment/locale/es.js', // only spanish for testing
       'node_modules/moment-timezone/moment-timezone.js',
+      'node_modules/luxon/build/global/luxon.js',
       'node_modules/jquery/dist/jquery.js',
       'node_modules/components-jqueryui/jquery-ui.js',
       'node_modules/components-jqueryui/themes/cupertino/jquery-ui.css',
@@ -34,6 +35,7 @@ module.exports = function(config) {
       'dist/fullcalendar.js',
       'dist/fullcalendar.css',
       'dist/fullcalendar-gcal.js',
+      'dist/fullcalendar-luxon.js',
       'dist/fullcalendar-moment.js',
       'dist/fullcalendar-moment-timezone.js',
       'dist/locale-all.js',

+ 11 - 0
package-lock.json

@@ -10,6 +10,12 @@
       "integrity": "sha512-mQjDxyOM1Cpocd+vm1kZBP7smwKZ4TNokFeds9LV7OZibmPJFEzY3+xZMrKfUdNT71lv8GoCPD6upKwHxubClw==",
       "dev": true
     },
+    "@types/luxon": {
+      "version": "1.2.2",
+      "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-1.2.2.tgz",
+      "integrity": "sha512-JziyQbl0YIE36lVLDMLhkEhZ1h3Do/+H6x908tXRhzPFrcGAyh7mJ44rhDff+R230RaeIUKeWnhoB8lH5SdsPA==",
+      "dev": true
+    },
     "abbrev": {
       "version": "1.1.1",
       "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@@ -6485,6 +6491,11 @@
         "yallist": "2.1.2"
       }
     },
+    "luxon": {
+      "version": "1.3.3",
+      "resolved": "https://registry.npmjs.org/luxon/-/luxon-1.3.3.tgz",
+      "integrity": "sha512-3CM0jpS3mbHwWoPYprX1/Zsd5esni0LkhMfSiSY6xQ3/M3pnct3OPWbWkQdEEl9MO9593k6PvDn1DhxCkpuZEw=="
+    },
     "macaddress": {
       "version": "0.2.8",
       "resolved": "https://registry.npmjs.org/macaddress/-/macaddress-0.2.8.tgz",

+ 2 - 0
package.json

@@ -22,6 +22,7 @@
   },
   "copyright": "2018 Adam Shaw",
   "dependencies": {
+    "luxon": "^1.3.3",
     "moment": "^2.22.2",
     "moment-timezone": "^0.5.21",
     "superagent": "^3.8.2"
@@ -29,6 +30,7 @@
   "version": "0.0.0",
   "releaseDate": "1970-01-01",
   "devDependencies": {
+    "@types/luxon": "^1.2.2",
     "awesome-typescript-loader": "^3.5.0",
     "bootstrap": "^3.3.7",
     "components-jqueryui": "github:components/jqueryui",

+ 17 - 0
plugins/luxon/main.ts

@@ -0,0 +1,17 @@
+import { DateTime, Duration } from 'luxon'
+import * as fc from 'fullcalendar'
+
+(fc as any).Luxon = {
+
+  toDateTime: function(calendar: fc.Calendar, date: Date): DateTime {
+    return DateTime.fromJSDate(date, {
+      zone: calendar.dateEnv.timeZone,
+      locale: calendar.dateEnv.locale.codes[0]
+    })
+  },
+
+  toDuration: function(duration: fc.Duration): Duration {
+    return Duration.fromObject(duration)
+  }
+
+}

+ 17 - 16
plugins/moment/main.ts

@@ -1,30 +1,31 @@
 import * as moment from 'moment'
 import * as fc from 'fullcalendar'
 
+(fc as any).Moment = {
 
-(fc as any).toMoment = function(calendar: fc.Calendar, date: Date): moment.Moment {
-  let timeZone = calendar.dateEnv.timeZone
-  let mom: moment.Moment
+  toMoment: function(calendar: fc.Calendar, date: Date): moment.Moment {
+    let timeZone = calendar.dateEnv.timeZone
+    let mom: moment.Moment
 
-  if (timeZone === 'local') {
-    mom = moment(date)
-  } else if (timeZone === 'UTC' || !(moment as any).tz) {
-    mom = moment.utc(date)
-  } else {
-    mom = (moment as any).tz(date, timeZone)
-  }
+    if (timeZone === 'local') {
+      mom = moment(date)
+    } else if (timeZone === 'UTC' || !(moment as any).tz) {
+      mom = moment.utc(date)
+    } else {
+      mom = (moment as any).tz(date, timeZone)
+    }
 
-  mom.locale(calendar.dateEnv.locale.codes[0])
+    mom.locale(calendar.dateEnv.locale.codes[0])
 
-  return mom
-};
+    return mom
+  },
 
+  toDuration: function(fcDuration: fc.Duration): moment.Duration {
+    return moment.duration(fcDuration) // i think all props are accepted?
+  }
 
-(fc as any).toDuration = function(fcDuration: fc.Duration): moment.Duration {
-  return moment.duration(fcDuration) // i think all props are accepted?
 }
 
-
 // TODO: what about range!!??
 
 fc.registerCmdFormatter('moment', function(cmdStr: string, arg: fc.VerboseFormattingArg) {

+ 73 - 0
tests/automated/datelib/luxon.js

@@ -0,0 +1,73 @@
+
+describe('luxon plugin', function() {
+  let toDateTime = FullCalendar.Luxon.toDateTime
+  let toDuration = FullCalendar.Luxon.toDuration
+
+  // TODO: test formatting
+  // TODO: test named timezones
+
+  describe('toDateTime', function() {
+
+    describe('timezone handling', function() {
+
+      it('transfers UTC', function() {
+        let calendar = new FullCalendar.Calendar(document.createElement('div'), {
+          events: [ { start: '2018-09-05T12:00:00', end: '2018-09-05T18:00:00' } ],
+          timeZone: 'UTC'
+        })
+        let event = calendar.getEvents()[0]
+        var start = toDateTime(calendar, event.start)
+        var end = toDateTime(calendar, event.end)
+        expect(start.toISO()).toBe('2018-09-05T12:00:00.000Z')
+        expect(start.zoneName).toBe('UTC')
+        expect(end.toISO()).toBe('2018-09-05T18:00:00.000Z')
+        expect(end.zoneName).toBe('UTC')
+      })
+
+      it('transfers local', function() {
+        let calendar = new FullCalendar.Calendar(document.createElement('div'), {
+          events: [ { start: '2018-09-05T12:00:00', end: '2018-09-05T18:00:00' } ],
+          timeZone: 'local'
+        })
+        let event = calendar.getEvents()[0]
+        var start = toDateTime(calendar, event.start)
+        var end = toDateTime(calendar, event.end)
+        expect(start.toJSDate()).toEqualDate('2018-09-05T12:00:00') // compare to local
+        expect(start.zoneName).toMatch('/') // has a named timezone
+        expect(end.toJSDate()).toEqualDate('2018-09-05T18:00:00') // compare to local
+        expect(end.zoneName).toMatch('/') // has a named timezone
+      })
+
+    })
+
+    it('transfers locale', function() {
+      let calendar = new FullCalendar.Calendar(document.createElement('div'), {
+        events: [ { start: '2018-09-05T12:00:00', end: '2018-09-05T18:00:00' } ],
+        locale: 'es'
+      })
+      let event = calendar.getEvents()[0]
+      var datetime = toDateTime(calendar, event.start)
+      expect(datetime.locale).toEqual('es')
+    })
+
+  })
+
+  describe('toDuration', function() {
+
+    it('converts correctly', function() {
+      let calendar = new FullCalendar.Calendar(document.createElement('div'), {
+        defaultTimedEventDuration: '05:00',
+        defaultAllDayEventDuration: { days: 3 }
+      })
+
+      // hacky way to have a duration parsed
+      let timedDuration = toDuration(calendar.defaultTimedEventDuration)
+      let allDayDuration = toDuration(calendar.defaultAllDayEventDuration)
+
+      expect(timedDuration.as('hours')).toBe(5)
+      expect(allDayDuration.as('days')).toBe(3)
+    })
+
+  })
+
+})

+ 9 - 7
tests/automated/datelib/moment.js

@@ -1,5 +1,7 @@
 
 describe('moment plugin', function() {
+  let toMoment = FullCalendar.Moment.toMoment
+  let toDuration = FullCalendar.Moment.toDuration
 
   // TODO: test formatting
 
@@ -13,8 +15,8 @@ describe('moment plugin', function() {
           timeZone: 'UTC'
         })
         let event = calendar.getEvents()[0]
-        var startMom = FullCalendar.toMoment(calendar, event.start)
-        var endMom = FullCalendar.toMoment(calendar, event.end)
+        var startMom = toMoment(calendar, event.start)
+        var endMom = toMoment(calendar, event.end)
         expect(startMom.format()).toEqual('2018-09-05T12:00:00Z')
         expect(endMom.format()).toEqual('2018-09-05T18:00:00Z')
       })
@@ -25,8 +27,8 @@ describe('moment plugin', function() {
           timeZone: 'local'
         })
         let event = calendar.getEvents()[0]
-        var startMom = FullCalendar.toMoment(calendar, event.start)
-        var endMom = FullCalendar.toMoment(calendar, event.end)
+        var startMom = toMoment(calendar, event.start)
+        var endMom = toMoment(calendar, event.end)
         expect(startMom.toDate()).toEqualDate('2018-09-05T12:00:00') // compare to local
         expect(endMom.toDate()).toEqualDate('2018-09-05T18:00:00') // compare to local
       })
@@ -39,7 +41,7 @@ describe('moment plugin', function() {
         locale: 'es'
       })
       let event = calendar.getEvents()[0]
-      var mom = FullCalendar.toMoment(calendar, event.start)
+      var mom = toMoment(calendar, event.start)
       expect(mom.locale()).toEqual('es')
     })
 
@@ -54,8 +56,8 @@ describe('moment plugin', function() {
       })
 
       // hacky way to have a duration parsed
-      let timedDuration = FullCalendar.toDuration(calendar.defaultTimedEventDuration)
-      let allDayDuration = FullCalendar.toDuration(calendar.defaultAllDayEventDuration)
+      let timedDuration = toDuration(calendar.defaultTimedEventDuration)
+      let allDayDuration = toDuration(calendar.defaultAllDayEventDuration)
 
       expect(timedDuration.asHours()).toBe(5)
       expect(allDayDuration.asDays()).toBe(3)

+ 2 - 0
webpack.config.js

@@ -16,6 +16,7 @@ const MODULES = {
   'dist/fullcalendar-gcal': './plugins/gcal/main.ts',
   'dist/fullcalendar-moment': './plugins/moment/main.ts',
   'dist/fullcalendar-moment-timezone': './plugins/moment-timezone/main.ts',
+  'dist/fullcalendar-luxon': './plugins/luxon/main.ts',
   'tmp/automated-tests': './tests/automated/index'
 }
 
@@ -31,6 +32,7 @@ module.exports = {
   externals: {
     moment: 'moment',
     'moment-timezone': 'moment-timezone',
+    luxon: 'luxon',
     superagent: 'superagent',
 
     // for plugins that might need jQuery