Skip to content
Snippets Groups Projects

using consts instead of an object

Merged Böhm, Jean requested to merge 877-time-functions-reuse into master
1 file
+ 107
90
Compare changes
  • Side-by-side
  • Inline
+ 107
90
@@ -2,14 +2,12 @@ import Vue from 'vue'
import { DateTime } from 'luxon'
import qs from 'qs'
// Description
// prefixes:
// get => returns something to work with (obj, iso etc.)
// display => formats dates and time to be displayed in the interface
export default ({ app }) => {
// Preset for displaying milliseconds
// Preset for displaying milliseconds
const DATETIME_PRESET_FORMAT = {
year: 'numeric',
month: 'short',
@@ -20,97 +18,116 @@ export default ({ app }) => {
fractionalSecondDigits: 3
}
Vue.prototype.$time = {
// helpers for q-input -----------------------------------------------------------------------------------------------
getInputTimeFromISO: (isoString) => {
DateTime.fromISO(isoString).toFormat('TT.SSS')
},
getInputDateFromISO: (isoString) => {
return DateTime.fromISO(isoString).toFormat('y-LL-dd')
},
getInputTimeFromQuery: (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return DateTime.fromISO(
parsed['date-time:t'],
{ setZone: true }
).toFormat('TT.SSS')
},
getInputDateFromQuery: (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return DateTime.fromISO(
parsed['date-time:t'],
{ setZone: true }
).toFormat('y-LL-dd')
},
getInputCurrentDate: () => {
return DateTime.local().toFormat('y-LL-dd')
},
getInputCurrentTime: () => {
return DateTime.local().toFormat('TT.SSS')
},
inputTimeAndDateToQuery: (time, date) => {
const combined = DateTime.fromISO(`${date}T${time}`)
return { 'date-time:t': combined.toLocal().toISO() }
},
inputTimeAndDateToISO: (time, date) => {
return DateTime.fromISO(`${date}T${time}`).toLocal().toISO()
},
// helpers for q-input -----------------------------------------------------------------------------------------------
const getInputTimeFromISO = (isoString) => {
DateTime.fromISO(isoString).toFormat('TT.SSS')
}
const getInputDateFromISO = (isoString) => {
return DateTime.fromISO(isoString).toFormat('y-LL-dd')
}
const getInputTimeFromQuery = (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return DateTime.fromISO(
parsed['date-time:t'],
{ setZone: true }
).toFormat('TT.SSS')
}
const getInputDateFromQuery = (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return DateTime.fromISO(
parsed['date-time:t'],
{ setZone: true }
).toFormat('y-LL-dd')
}
const getInputCurrentDate = () => {
return DateTime.local().toFormat('y-LL-dd')
}
const getInputCurrentTime = () => {
return DateTime.local().toFormat('TT.SSS')
}
const inputTimeAndDateToQuery = (time, date) => {
const combined = DateTime.fromISO(`${date}T${time}`)
return { 'date-time:t': combined.toLocal().toISO() }
}
const inputTimeAndDateToISO = (time, date) => {
return DateTime.fromISO(`${date}T${time}`).toLocal().toISO()
}
// Current times getters ---------------------------------------------------------------------------------------------
getCurrentDateTime: () => {
return DateTime.local()
},
getCurrentDateTimeISO: () => {
return DateTime.local().toISO()
},
// Current times getters ---------------------------------------------------------------------------------------------
const getCurrentDateTime = () => {
return DateTime.local()
}
const getCurrentDateTimeISO = () => {
return DateTime.local().toISO()
}
// Helpers for parsing time query from API ---------------------------------------------------------------------------
getDateTimeFromQuery: (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return DateTime.fromISO(
parsed['date-time:t'],
{ setZone: true }
)
},
getISOFromQuery: (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return parsed['date-time:t']
},
displayTimeQuery: (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return DateTime.fromISO(
parsed['date-time:t'],
{ setZone: true }
).toLocaleString(DATETIME_PRESET_FORMAT)
},
// Helpers for parsing time query from API ---------------------------------------------------------------------------
const getDateTimeFromQuery = (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return DateTime.fromISO(
parsed['date-time:t'],
{ setZone: true }
)
}
const getISOFromQuery = (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return parsed['date-time:t']
}
const displayTimeQuery = (queryString) => {
const parsed = qs.parse(queryString, { decoder: val => val })
return DateTime.fromISO(
parsed['date-time:t'],
{ setZone: true }
).toLocaleString(DATETIME_PRESET_FORMAT)
}
// Helper to display ISO time ----------------------------------------------------------------------------------------
displayTimeISO: (isoString) => {
return DateTime.fromISO(
isoString,
{ setZone: true }
).toLocaleString(DATETIME_PRESET_FORMAT)
},
displayISODaysAgo (isoString) {
const past = DateTime.fromISO(isoString)
return past.toRelativeCalendar({ locale: app.i18n.locale })
},
// Helper to display ISO time ----------------------------------------------------------------------------------------
const displayTimeISO = (isoString) => {
return DateTime.fromISO(
isoString,
{ setZone: true }
).toLocaleString(DATETIME_PRESET_FORMAT)
}
const displayISODaysAgo = (isoString) => {
const past = DateTime.fromISO(isoString)
return past.toRelativeCalendar({ locale: app.i18n.locale })
}
// Helper to display Millis time -----------------------------------------------------------------------------------
displayTimeFromMillis: (millisString) => {
return DateTime.fromMillis(
millisString,
{ setZone: true }
).toLocaleString(DATETIME_PRESET_FORMAT)
},
isoToMillis: (iso) => {
return DateTime.fromISO(iso, { setZone: true }).toMillis()
},
millisFromQuery: (query) => {
const parsed = qs.parse(query, { decoder: val => val })
if (typeof parsed['date-time:t'] === 'string') {
return DateTime.fromISO(parsed['date-time:t'], { setZone: true }).toMillis()
}
// Helper to display Millis time -----------------------------------------------------------------------------------
const displayTimeFromMillis = (millisString) => {
return DateTime.fromMillis(
millisString,
{ setZone: true }
).toLocaleString(DATETIME_PRESET_FORMAT)
}
const isoToMillis = (iso) => {
return DateTime.fromISO(iso, { setZone: true }).toMillis()
}
const millisFromQuery = (query) => {
const parsed = qs.parse(query, { decoder: val => val })
if (typeof parsed['date-time:t'] === 'string') {
return DateTime.fromISO(parsed['date-time:t'], { setZone: true }).toMillis()
}
}
Vue.prototype.$time = {
getInputTimeFromISO,
getInputDateFromISO,
getInputTimeFromQuery,
getInputDateFromQuery,
getInputCurrentDate,
getInputCurrentTime,
inputTimeAndDateToQuery,
inputTimeAndDateToISO,
getCurrentDateTime,
getCurrentDateTimeISO,
getDateTimeFromQuery,
getISOFromQuery,
displayTimeQuery,
displayTimeISO,
displayISODaysAgo,
displayTimeFromMillis,
isoToMillis,
millisFromQuery
}
}
Loading