Quellcode durchsuchen

public formatting api

Adam Shaw vor 7 Jahren
Ursprung
Commit
dd0e2b6029
5 geänderte Dateien mit 101 neuen und 1 gelöschten Zeilen
  1. 1 1
      src/datelib/env.ts
  2. 1 0
      src/datelib/formatting.ts
  3. 2 0
      src/exports.ts
  4. 49 0
      src/formatting-api.ts
  5. 48 0
      tests/automated/datelib/formatting-api.js

+ 1 - 1
src/datelib/env.ts

@@ -19,7 +19,7 @@ export interface DateEnvSettings {
   locale: Locale
   locale: Locale
   weekNumberCalculation?: any
   weekNumberCalculation?: any
   firstDay?: any,
   firstDay?: any,
-  weekLabel: string
+  weekLabel?: string
 }
 }
 
 
 export type DateInput = Date | string | number | number[]
 export type DateInput = Date | string | number | number[]

+ 1 - 0
src/datelib/formatting.ts

@@ -43,6 +43,7 @@ export interface DateFormatter {
   formatRange(start: ZonedMarker, end: ZonedMarker, context: DateFormattingContext)
   formatRange(start: ZonedMarker, end: ZonedMarker, context: DateFormattingContext)
 }
 }
 
 
+// TODO: use Intl.DateTimeFormatOptions
 export type FormatterInput = object | string | FuncFormatterFunc
 export type FormatterInput = object | string | FuncFormatterFunc
 
 
 
 

+ 2 - 0
src/exports.ts

@@ -112,3 +112,5 @@ export { default as ElementDragging } from './dnd/ElementDragging'
 
 
 export { default as Draggable } from './interactions-external/Draggable'
 export { default as Draggable } from './interactions-external/Draggable'
 export { default as GenericDragging } from './interactions-external/generic-dragging' // singleton
 export { default as GenericDragging } from './interactions-external/generic-dragging' // singleton
+
+export { formatDate, formatRange } from './formatting-api'

+ 49 - 0
src/formatting-api.ts

@@ -0,0 +1,49 @@
+import { DateEnv, DateInput } from './datelib/env'
+import { assignTo } from './util/object'
+import { createFormatter } from './datelib/formatting'
+import { getLocale } from './datelib/locale';
+
+export function formatDate(dateInput: DateInput, settings = {}) {
+  let dateEnv = buildDateEnv(settings)
+  let formatter = createFormatter(settings)
+  let dateMeta = dateEnv.createMarkerMeta(dateInput)
+
+  if (!dateMeta) { // TODO: warning?
+    return ''
+  }
+
+  return dateEnv.format(dateMeta.marker, formatter, {
+    forcedTzo: dateMeta.forcedTzo
+  })
+}
+
+export function formatRange(startInput: DateInput, endInput: DateInput, settings = {}) {
+  let dateEnv = buildDateEnv(settings)
+  let formatter = createFormatter(settings)
+  let startMeta = dateEnv.createMarkerMeta(startInput)
+  let endMeta = dateEnv.createMarkerMeta(endInput)
+
+  if (!startMeta || !endMeta) { // TODO: warning?
+    return ''
+  }
+
+  return dateEnv.formatRange(startMeta.marker, endMeta.marker, formatter, {
+    forcedStartTzo: startMeta.forcedTzo,
+    forcedEndTzo: endMeta.forcedTzo
+  })
+}
+
+function buildDateEnv(settings) {
+  let locale = settings.locale || 'en'
+
+  // ensure required settings
+  // TODO: use constants
+  settings = assignTo({
+    timeZone: 'UTC',
+    calendarSystem: 'gregory'
+  }, settings, {
+    locale: getLocale(locale)
+  })
+
+  return new DateEnv(settings)
+}

+ 48 - 0
tests/automated/datelib/formatting-api.js

@@ -0,0 +1,48 @@
+
+describe('formatDate', function() {
+
+  it('works with no timezone offset', function() {
+    let str = FullCalendar.formatDate('2018-09-04', {
+      month: 'long',
+      day: 'numeric',
+      year: 'numeric'
+    })
+    expect(str).toBe('September 4, 2018')
+  })
+
+  it('works with timezone offset', function() {
+    let str = FullCalendar.formatDate('2018-09-04T00:00:00-05:00', {
+      month: 'long',
+      day: 'numeric',
+      year: 'numeric',
+      timeZoneName: 'short',
+      timeZone: 'America/New_York' // but with no timeZoneImpl
+    })
+    expect(str).toBe('September 4, 2018, GMT+5')
+  })
+
+})
+
+describe('formatRange', function() {
+
+  it('works with no timezone offset', function() {
+    let str = FullCalendar.formatRange('2018-09-04', '2018-10-04', {
+      month: 'long',
+      day: 'numeric',
+      year: 'numeric'
+    })
+    expect(str).toBe('September 4 - October 4, 2018')
+  })
+
+  it('works with timezone offset', function() {
+    let str = FullCalendar.formatRange('2018-09-04T00:00:00-05:00', '2018-10-04T00:00:00-05:00', {
+      month: 'long',
+      day: 'numeric',
+      year: 'numeric',
+      timeZoneName: 'short',
+      timeZone: 'America/New_York' // but with no timeZoneImpl
+    })
+    expect(str).toBe('September 4 - October 4, 2018, GMT+5')
+  })
+
+})