2024-02-21 19:40:50 +01:00
|
|
|
import dayjs from "dayjs"
|
2024-02-21 21:10:53 +01:00
|
|
|
|
|
|
|
import dayjsDurationPlugin from "dayjs/plugin/duration"
|
|
|
|
import dayjsAdvancedFormatPlugin from "dayjs/plugin/advancedFormat"
|
|
|
|
import dayjsIsoWeekPlugin from "dayjs/plugin/isoWeek"
|
|
|
|
import dayjsWeekYearPlugin from "dayjs/plugin/weekYear"
|
|
|
|
import dayjsWeekOfYearPlugin from "dayjs/plugin/weekOfYear"
|
|
|
|
import dayjsRelativeTimePlugin from "dayjs/plugin/relativeTime"
|
|
|
|
import dayjsUtcPlugin from "dayjs/plugin/utc"
|
|
|
|
import dayjsTimezonePlugin from "dayjs/plugin/timezone"
|
|
|
|
|
|
|
|
dayjs.extend(dayjsDurationPlugin)
|
|
|
|
dayjs.extend(dayjsAdvancedFormatPlugin)
|
|
|
|
dayjs.extend(dayjsIsoWeekPlugin)
|
|
|
|
dayjs.extend(dayjsWeekYearPlugin)
|
|
|
|
dayjs.extend(dayjsWeekOfYearPlugin)
|
|
|
|
dayjs.extend(dayjsRelativeTimePlugin)
|
|
|
|
dayjs.extend(dayjsUtcPlugin)
|
|
|
|
dayjs.extend(dayjsTimezonePlugin)
|
2021-02-03 14:55:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This file was largely taken from the helper-date package - we did this for two reasons:
|
|
|
|
* 1. It made use of both moment of date.js - this caused some weird bugs with some relatively simple
|
|
|
|
* syntax and didn't offer much in return.
|
|
|
|
* 2. Replacing moment with dayjs helps massively reduce bundle size.
|
|
|
|
* The original package can be found here:
|
|
|
|
* https://github.com/helpers/helper-date
|
|
|
|
*/
|
|
|
|
|
2024-03-15 09:35:38 +01:00
|
|
|
function isOptions(val: any) {
|
2021-02-03 14:56:01 +01:00
|
|
|
return typeof val === "object" && typeof val.hash === "object"
|
2021-02-03 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
2024-03-15 09:35:38 +01:00
|
|
|
function isApp(thisArg: any) {
|
2021-02-03 14:56:01 +01:00
|
|
|
return (
|
|
|
|
typeof thisArg === "object" &&
|
|
|
|
typeof thisArg.options === "object" &&
|
|
|
|
typeof thisArg.app === "object"
|
|
|
|
)
|
2021-02-03 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
2024-03-15 09:35:38 +01:00
|
|
|
function getContext(thisArg: any, locals: any, options: any) {
|
2021-02-03 14:55:33 +01:00
|
|
|
if (isOptions(thisArg)) {
|
|
|
|
return getContext({}, locals, thisArg)
|
|
|
|
}
|
|
|
|
// ensure args are in the correct order
|
|
|
|
if (isOptions(locals)) {
|
|
|
|
return getContext(thisArg, options, locals)
|
|
|
|
}
|
|
|
|
const appContext = isApp(thisArg) ? thisArg.context : {}
|
|
|
|
options = options || {}
|
|
|
|
|
|
|
|
// if "options" is not handlebars options, merge it onto locals
|
|
|
|
if (!isOptions(options)) {
|
|
|
|
locals = Object.assign({}, locals, options)
|
|
|
|
}
|
|
|
|
// merge handlebars root data onto locals if specified on the hash
|
|
|
|
if (isOptions(options) && options.hash.root === true) {
|
|
|
|
locals = Object.assign({}, options.data.root, locals)
|
|
|
|
}
|
|
|
|
let context = Object.assign({}, appContext, locals, options.hash)
|
|
|
|
if (!isApp(thisArg)) {
|
|
|
|
context = Object.assign({}, thisArg, context)
|
|
|
|
}
|
|
|
|
if (isApp(thisArg) && thisArg.view && thisArg.view.data) {
|
|
|
|
context = Object.assign({}, context, thisArg.view.data)
|
|
|
|
}
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
2024-03-15 09:35:38 +01:00
|
|
|
function initialConfig(str: any, pattern: any, options?: any) {
|
2021-02-03 14:55:33 +01:00
|
|
|
if (isOptions(pattern)) {
|
|
|
|
options = pattern
|
|
|
|
pattern = null
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isOptions(str)) {
|
|
|
|
options = str
|
|
|
|
pattern = null
|
|
|
|
str = null
|
|
|
|
}
|
2021-02-04 11:25:04 +01:00
|
|
|
return { str, pattern, options }
|
2021-02-04 11:17:10 +01:00
|
|
|
}
|
|
|
|
|
2024-03-15 09:35:38 +01:00
|
|
|
function setLocale(str: any, pattern: any, options?: any) {
|
2021-02-04 11:17:10 +01:00
|
|
|
// if options is null then it'll get updated here
|
2021-02-04 11:41:25 +01:00
|
|
|
const config = initialConfig(str, pattern, options)
|
|
|
|
const defaults = { lang: "en", date: new Date(config.str) }
|
2021-06-09 12:44:24 +02:00
|
|
|
// for now don't allow this to be configurable, don't pass in options
|
|
|
|
const opts = getContext(this, defaults, {})
|
2021-02-04 11:17:10 +01:00
|
|
|
|
|
|
|
// set the language to use
|
|
|
|
dayjs.locale(opts.lang || opts.language)
|
|
|
|
}
|
|
|
|
|
2024-03-15 09:35:38 +01:00
|
|
|
export const date = (str: any, pattern: any, options: any) => {
|
2021-02-04 11:41:25 +01:00
|
|
|
const config = initialConfig(str, pattern, options)
|
2021-02-03 14:55:33 +01:00
|
|
|
|
|
|
|
// if no args are passed, return a formatted date
|
2021-02-04 11:41:25 +01:00
|
|
|
if (config.str == null && config.pattern == null) {
|
2021-02-03 14:55:33 +01:00
|
|
|
dayjs.locale("en")
|
|
|
|
return dayjs().format("MMMM DD, YYYY")
|
|
|
|
}
|
|
|
|
|
2021-02-04 11:41:25 +01:00
|
|
|
setLocale(config.str, config.pattern, config.options)
|
2021-02-03 14:55:33 +01:00
|
|
|
|
2021-06-09 12:44:24 +02:00
|
|
|
let date = dayjs(new Date(config.str))
|
|
|
|
if (typeof config.options === "string") {
|
|
|
|
date =
|
|
|
|
config.options.toLowerCase() === "utc"
|
|
|
|
? date.utc()
|
|
|
|
: date.tz(config.options)
|
|
|
|
} else {
|
|
|
|
date = date.tz(dayjs.tz.guess())
|
|
|
|
}
|
2021-05-27 16:21:00 +02:00
|
|
|
if (config.pattern === "") {
|
|
|
|
return date.toISOString()
|
|
|
|
}
|
|
|
|
return date.format(config.pattern)
|
2021-02-03 14:56:01 +01:00
|
|
|
}
|
2021-02-04 11:17:10 +01:00
|
|
|
|
2024-03-15 09:35:38 +01:00
|
|
|
export const duration = (str: any, pattern: any, format: any) => {
|
2021-02-04 11:41:25 +01:00
|
|
|
const config = initialConfig(str, pattern)
|
2021-02-04 11:17:10 +01:00
|
|
|
|
2021-02-04 11:41:25 +01:00
|
|
|
setLocale(config.str, config.pattern)
|
2021-02-04 11:17:10 +01:00
|
|
|
|
2021-02-04 11:41:25 +01:00
|
|
|
const duration = dayjs.duration(config.str, config.pattern)
|
2024-01-25 17:35:00 +01:00
|
|
|
if (format && !isOptions(format)) {
|
2021-02-04 11:17:10 +01:00
|
|
|
return duration.format(format)
|
|
|
|
} else {
|
|
|
|
return duration.humanize()
|
|
|
|
}
|
|
|
|
}
|