Browse Source

luxon single date formatting. tests for moment/luxon formatting

Adam Shaw 7 năm trước cách đây
mục cha
commit
c424cacf2c

+ 45 - 22
plugins/luxon/main.ts

@@ -24,34 +24,57 @@ import * as fc from 'fullcalendar'
 class LuxonNamedTimeZone extends fc.NamedTimeZoneImpl {
 
   offsetForArray(a: number[]): number {
-    return DateTime.fromObject({
-      zone: this.name,
-      year: a[0],
-      month: a[1] + 1, // convert 0-based to 1-based
-      day: a[2],
-      hour: a[3],
-      minute: a[4],
-      second: a[5],
-      millisecond: a[6]
-    }).offset
+    return arrayToLuxon(a, this.name).offset
   }
 
   timestampToArray(ms: number): number[] {
-    let obj = DateTime.fromMillis(ms, {
-      zone: this.name
-    })
-    return [
-      obj.year,
-      obj.month - 1, // convert 1-based to 0-based
-      obj.day,
-      obj.hour,
-      obj.minute,
-      obj.second,
-      obj.millisecond
-    ]
+    return luxonToArray(
+      DateTime.fromMillis(ms, {
+        zone: this.name
+      })
+    )
   }
 
 }
 
 
 fc.registerNamedTimeZoneImpl('luxon', LuxonNamedTimeZone)
+
+
+
+// TODO: what about range!!??
+
+fc.registerCmdFormatter('luxon', function(cmdStr: string, arg: fc.VerboseFormattingArg) {
+  return arrayToLuxon(
+    arg.date.array,
+    arg.timeZone,
+    arg.localeCodes[0]
+  ).toFormat(cmdStr)
+})
+
+
+function luxonToArray(datetime: DateTime): number[] {
+  return [
+    datetime.year,
+    datetime.month - 1, // convert 1-based to 0-based
+    datetime.day,
+    datetime.hour,
+    datetime.minute,
+    datetime.second,
+    datetime.millisecond
+  ]
+}
+
+function arrayToLuxon(arr: number[], timeZone: string, locale?: string): DateTime {
+  return DateTime.fromObject({
+    zone: timeZone,
+    locale: locale,
+    year: arr[0],
+    month: arr[1] + 1, // convert 0-based to 1-based
+    day: arr[2],
+    hour: arr[3],
+    minute: arr[4],
+    second: arr[5],
+    millisecond: arr[6]
+  })
+}

+ 1 - 1
src/datelib/formatting.ts

@@ -23,7 +23,7 @@ export interface ExpandedZonedMarker extends ZonedMarker {
   millisecond: number
 }
 
-export interface VerboseFormattingArg {
+export interface VerboseFormattingArg { // TODO: kill this
   date: ExpandedZonedMarker
   start: ExpandedZonedMarker
   end?: ExpandedZonedMarker

+ 1 - 1
src/datelib/timezone.ts

@@ -1,7 +1,7 @@
 
 export abstract class NamedTimeZoneImpl {
 
-  name: string
+  name: string // bad name for this. is it the impl or the timeZone name?
 
   constructor(name: string) {
     this.name = name

+ 30 - 2
tests/automated/datelib/luxon.js

@@ -1,10 +1,9 @@
+import { getSingleEl, getEventElTimeText } from '../event-render/EventRenderUtils'
 
 describe('luxon plugin', function() {
   let toDateTime = FullCalendar.Luxon.toDateTime
   let toDuration = FullCalendar.Luxon.toDuration
 
-  // TODO: test formatting
-
   // NOTE: timezone offset converting is done in timeZoneImpl
 
   describe('toDateTime', function() {
@@ -97,4 +96,33 @@ describe('luxon plugin', function() {
 
   })
 
+  describe('formatting', function() {
+
+    it('produces event time text', function() {
+      initCalendar({
+        defaultView: 'month',
+        now: '2018-09-06',
+        displayEventEnd: false,
+        cmdFormatter: 'luxon',
+        eventTimeFormat: 'HH:mm:ss\'abc\'',
+        events: [
+          { title: 'my event', start: '2018-09-06T13:30:20' }
+        ]
+      })
+      expect(getEventElTimeText(getSingleEl())).toBe('13:30:20abc')
+    })
+
+    xit('produces title with titleRangeSeparator', function() {
+      initCalendar({ // need to render the calendar to get view.title :(
+        defaultView: 'basicWeek',
+        now: '2018-09-06',
+        cmdFormatter: 'luxon',
+        titleFormat: 'MMMM {d} yy \'abc\'',
+        titleRangeSeparator: ' to '
+      })
+      expect(currentCalendar.view.title).toBe('September 2 to 8 18 abc')
+    })
+
+  })
+
 })

+ 30 - 2
tests/automated/datelib/moment.js

@@ -1,10 +1,9 @@
+import { getSingleEl, getEventElTimeText } from '../event-render/EventRenderUtils'
 
 describe('moment plugin', function() {
   let toMoment = FullCalendar.Moment.toMoment
   let toDuration = FullCalendar.Moment.toDuration
 
-  // TODO: test formatting
-
   describe('toMoment', function() {
 
     describe('timezone handling', function() {
@@ -65,4 +64,33 @@ describe('moment plugin', function() {
 
   })
 
+  describe('formatting', function() {
+
+    it('produces event time text', function() {
+      initCalendar({
+        defaultView: 'month',
+        now: '2018-09-06',
+        displayEventEnd: false,
+        cmdFormatter: 'moment',
+        eventTimeFormat: 'HH:mm:ss[!]',
+        events: [
+          { title: 'my event', start: '2018-09-06T13:30:20' }
+        ]
+      })
+      expect(getEventElTimeText(getSingleEl())).toBe('13:30:20!')
+    })
+
+    xit('produces title with titleRangeSeparator', function() {
+      initCalendar({ // need to render the calendar to get view.title :(
+        defaultView: 'basicWeek',
+        now: '2018-09-06',
+        cmdFormatter: 'moment',
+        titleFormat: 'MMMM {D} YY [yup]',
+        titleRangeSeparator: ' to '
+      })
+      expect(currentCalendar.view.title).toBe('September 2 to 8 18 yup')
+    })
+
+  })
+
 })