Adam Shaw 7 лет назад
Родитель
Сommit
4f9940adc7
2 измененных файлов с 130 добавлено и 8 удалено
  1. 74 4
      plugins/luxon/main.ts
  2. 56 4
      tests/automated/datelib/luxon.js

+ 74 - 4
plugins/luxon/main.ts

@@ -37,19 +37,36 @@ class LuxonNamedTimeZone extends fc.NamedTimeZoneImpl {
 
 }
 
-
 fc.registerNamedTimeZoneImpl('luxon', LuxonNamedTimeZone)
 
 
+fc.registerCmdFormatter('luxon', function(cmdStr: string, arg: fc.VerboseFormattingArg) {
+  let cmd = parseCmdStr(cmdStr)
 
-// TODO: what about range!!??
+  if (arg.end) {
+    let start = arrayToLuxon(
+      arg.start.array,
+      arg.timeZone,
+      arg.localeCodes[0]
+    )
+    let end = arrayToLuxon(
+      arg.end.array,
+      arg.timeZone,
+      arg.localeCodes[0]
+    )
+    return formatRange(
+      cmd,
+      start.toFormat.bind(start),
+      end.toFormat.bind(end),
+      arg.separator
+    )
+  }
 
-fc.registerCmdFormatter('luxon', function(cmdStr: string, arg: fc.VerboseFormattingArg) {
   return arrayToLuxon(
     arg.date.array,
     arg.timeZone,
     arg.localeCodes[0]
-  ).toFormat(cmdStr)
+  ).toFormat(cmd.whole)
 })
 
 
@@ -78,3 +95,56 @@ function arrayToLuxon(arr: number[], timeZone: string, locale?: string): DateTim
     millisecond: arr[6]
   })
 }
+
+
+/* Range Formatting (duplicate code as other date plugins)
+----------------------------------------------------------------------------------------------------*/
+
+interface CmdParts {
+  head: string | null
+  middle: CmdParts | null
+  tail: string | null
+  whole: string
+}
+
+function parseCmdStr(cmdStr: string): CmdParts {
+  let parts = cmdStr.match(/^(.*?)\{(.*)\}(.*)$/) // TODO: lookbehinds for escape characters
+
+  if (parts) {
+    let middle = parseCmdStr(parts[2])
+
+    return {
+      head: parts[1],
+      middle,
+      tail: parts[3],
+      whole: parts[1] + middle.whole + parts[3]
+    }
+  } else {
+    return {
+      head: null,
+      middle: null,
+      tail: null,
+      whole: cmdStr
+    }
+  }
+}
+
+function formatRange(cmd: CmdParts, formatStart: (cmdStr: string) => string, formatEnd: (cmdStr: string) => string, separator: string): string {
+  if (cmd.middle) {
+    let startHead = formatStart(cmd.head)
+    let startMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator)
+    let startTail = formatStart(cmd.tail)
+
+    let endHead = formatEnd(cmd.head)
+    let endMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator)
+    let endTail = formatEnd(cmd.tail)
+
+    if (startHead === endHead && startTail === endTail) {
+      return startHead +
+        (startMiddle === endMiddle ? startMiddle : startMiddle + separator + endMiddle) +
+        startTail
+    }
+  }
+
+  return formatStart(cmd.whole) + separator + formatEnd(cmd.whole)
+}

+ 56 - 4
tests/automated/datelib/luxon.js

@@ -96,7 +96,7 @@ describe('luxon plugin', function() {
 
   })
 
-  describe('formatting', function() {
+  describe('date formatting', function() {
 
     it('produces event time text', function() {
       initCalendar({
@@ -112,15 +112,67 @@ describe('luxon plugin', function() {
       expect(getEventElTimeText(getSingleEl())).toBe('13:30:20abc')
     })
 
-    xit('produces title with titleRangeSeparator', function() {
+  })
+
+  describe('range formatting', function() {
+
+    it('renders with same month', function() {
+      let calendar = new FullCalendar.Calendar(document.createElement('div'), {
+        cmdFormatter: 'luxon'
+      })
+      let s
+
+      s = calendar.formatRange('2018-09-03', '2018-09-05', 'MMMM {d}, yyyy \'asdf\'')
+      expect(s).toEqual('September 3 - 5, 2018 asdf')
+
+      s = calendar.formatRange('2018-09-03', '2018-09-05', '{d} MMMM, yyyy \'asdf\'')
+      expect(s).toEqual('3 - 5 September, 2018 asdf')
+    })
+
+    it('renders with same year but different month', function() {
+      let calendar = new FullCalendar.Calendar(document.createElement('div'), {
+        cmdFormatter: 'luxon'
+      })
+      let s
+
+      s = calendar.formatRange('2018-09-03', '2018-10-05', '{MMMM {d}}, yyyy \'asdf\'')
+      expect(s).toEqual('September 3 - October 5, 2018 asdf')
+
+      s = calendar.formatRange('2018-09-03', '2018-10-05', '{{d} MMMM}, yyyy \'asdf\'')
+      expect(s).toEqual('3 September - 5 October, 2018 asdf')
+    })
+
+    it('renders with different years', function() {
+      let calendar = new FullCalendar.Calendar(document.createElement('div'), {
+        cmdFormatter: 'luxon'
+      })
+      let s
+
+      s = calendar.formatRange('2018-09-03', '2019-10-05', '{MMMM {d}}, yyyy \'asdf\'')
+      expect(s).toEqual('September 3, 2018 asdf - October 5, 2019 asdf')
+
+      s = calendar.formatRange('2018-09-03', '2019-10-05', '{{d} MMMM}, yyyy \'asdf\'')
+      expect(s).toEqual('3 September, 2018 asdf - 5 October, 2019 asdf')
+    })
+
+    it('inherits defaultRangeSeparator', function() {
+      let calendar = new FullCalendar.Calendar(document.createElement('div'), {
+        cmdFormatter: 'luxon',
+        defaultRangeSeparator: ' to '
+      })
+      let s = calendar.formatRange('2018-09-03', '2018-09-05', 'MMMM d, yyyy \'asdf\'')
+      expect(s).toEqual('September 3, 2018 asdf to September 5, 2018 asdf')
+    })
+
+    it('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\'',
+        titleFormat: 'MMMM {d} yy \'yup\'',
         titleRangeSeparator: ' to '
       })
-      expect(currentCalendar.view.title).toBe('September 2 to 8 18 abc')
+      expect(currentCalendar.view.title).toBe('September 2 to 8 18 yup')
     })
 
   })