First upload
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
import { tokenRegex, revFormat, formats, } from "./formatting";
|
||||
import { defaults } from "../types/options";
|
||||
import { english } from "../l10n/default";
|
||||
export const createDateFormatter = ({ config = defaults, l10n = english, isMobile = false, }) => (dateObj, frmt, overrideLocale) => {
|
||||
const locale = overrideLocale || l10n;
|
||||
if (config.formatDate !== undefined && !isMobile) {
|
||||
return config.formatDate(dateObj, frmt, locale);
|
||||
}
|
||||
return frmt
|
||||
.split("")
|
||||
.map((c, i, arr) => formats[c] && arr[i - 1] !== "\\"
|
||||
? formats[c](dateObj, locale, config)
|
||||
: c !== "\\"
|
||||
? c
|
||||
: "")
|
||||
.join("");
|
||||
};
|
||||
export const createDateParser = ({ config = defaults, l10n = english }) => (date, givenFormat, timeless, customLocale) => {
|
||||
if (date !== 0 && !date)
|
||||
return undefined;
|
||||
const locale = customLocale || l10n;
|
||||
let parsedDate;
|
||||
const dateOrig = date;
|
||||
if (date instanceof Date)
|
||||
parsedDate = new Date(date.getTime());
|
||||
else if (typeof date !== "string" &&
|
||||
date.toFixed !== undefined)
|
||||
parsedDate = new Date(date);
|
||||
else if (typeof date === "string") {
|
||||
const format = givenFormat || (config || defaults).dateFormat;
|
||||
const datestr = String(date).trim();
|
||||
if (datestr === "today") {
|
||||
parsedDate = new Date();
|
||||
timeless = true;
|
||||
}
|
||||
else if (/Z$/.test(datestr) ||
|
||||
/GMT$/.test(datestr))
|
||||
parsedDate = new Date(date);
|
||||
else if (config && config.parseDate)
|
||||
parsedDate = config.parseDate(date, format);
|
||||
else {
|
||||
parsedDate =
|
||||
!config || !config.noCalendar
|
||||
? new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0)
|
||||
: new Date(new Date().setHours(0, 0, 0, 0));
|
||||
let matched, ops = [];
|
||||
for (let i = 0, matchIndex = 0, regexStr = ""; i < format.length; i++) {
|
||||
const token = format[i];
|
||||
const isBackSlash = token === "\\";
|
||||
const escaped = format[i - 1] === "\\" || isBackSlash;
|
||||
if (tokenRegex[token] && !escaped) {
|
||||
regexStr += tokenRegex[token];
|
||||
const match = new RegExp(regexStr).exec(date);
|
||||
if (match && (matched = true)) {
|
||||
ops[token !== "Y" ? "push" : "unshift"]({
|
||||
fn: revFormat[token],
|
||||
val: match[++matchIndex],
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (!isBackSlash)
|
||||
regexStr += ".";
|
||||
ops.forEach(({ fn, val }) => (parsedDate = fn(parsedDate, val, locale) || parsedDate));
|
||||
}
|
||||
parsedDate = matched ? parsedDate : undefined;
|
||||
}
|
||||
}
|
||||
if (!(parsedDate instanceof Date && !isNaN(parsedDate.getTime()))) {
|
||||
config.errorHandler(new Error(`Invalid date provided: ${dateOrig}`));
|
||||
return undefined;
|
||||
}
|
||||
if (timeless === true)
|
||||
parsedDate.setHours(0, 0, 0, 0);
|
||||
return parsedDate;
|
||||
};
|
||||
export function compareDates(date1, date2, timeless = true) {
|
||||
if (timeless !== false) {
|
||||
return (new Date(date1.getTime()).setHours(0, 0, 0, 0) -
|
||||
new Date(date2.getTime()).setHours(0, 0, 0, 0));
|
||||
}
|
||||
return date1.getTime() - date2.getTime();
|
||||
}
|
||||
export function compareTimes(date1, date2) {
|
||||
return (3600 * (date1.getHours() - date2.getHours()) +
|
||||
60 * (date1.getMinutes() - date2.getMinutes()) +
|
||||
date1.getSeconds() -
|
||||
date2.getSeconds());
|
||||
}
|
||||
export const isBetween = (ts, ts1, ts2) => {
|
||||
return ts > Math.min(ts1, ts2) && ts < Math.max(ts1, ts2);
|
||||
};
|
||||
export const duration = {
|
||||
DAY: 86400000,
|
||||
};
|
||||
export function getDefaultHours(config) {
|
||||
let hours = config.defaultHour;
|
||||
let minutes = config.defaultMinute;
|
||||
let seconds = config.defaultSeconds;
|
||||
if (config.minDate !== undefined) {
|
||||
const minHour = config.minDate.getHours();
|
||||
const minMinutes = config.minDate.getMinutes();
|
||||
const minSeconds = config.minDate.getSeconds();
|
||||
if (hours < minHour) {
|
||||
hours = minHour;
|
||||
}
|
||||
if (hours === minHour && minutes < minMinutes) {
|
||||
minutes = minMinutes;
|
||||
}
|
||||
if (hours === minHour && minutes === minMinutes && seconds < minSeconds)
|
||||
seconds = config.minDate.getSeconds();
|
||||
}
|
||||
if (config.maxDate !== undefined) {
|
||||
const maxHr = config.maxDate.getHours();
|
||||
const maxMinutes = config.maxDate.getMinutes();
|
||||
hours = Math.min(hours, maxHr);
|
||||
if (hours === maxHr)
|
||||
minutes = Math.min(maxMinutes, minutes);
|
||||
if (hours === maxHr && minutes === maxMinutes)
|
||||
seconds = config.maxDate.getSeconds();
|
||||
}
|
||||
return { hours, minutes, seconds };
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
export function toggleClass(elem, className, bool) {
|
||||
if (bool === true)
|
||||
return elem.classList.add(className);
|
||||
elem.classList.remove(className);
|
||||
}
|
||||
export function createElement(tag, className, content) {
|
||||
const e = window.document.createElement(tag);
|
||||
className = className || "";
|
||||
content = content || "";
|
||||
e.className = className;
|
||||
if (content !== undefined)
|
||||
e.textContent = content;
|
||||
return e;
|
||||
}
|
||||
export function clearNode(node) {
|
||||
while (node.firstChild)
|
||||
node.removeChild(node.firstChild);
|
||||
}
|
||||
export function findParent(node, condition) {
|
||||
if (condition(node))
|
||||
return node;
|
||||
else if (node.parentNode)
|
||||
return findParent(node.parentNode, condition);
|
||||
return undefined;
|
||||
}
|
||||
export function createNumberInput(inputClassName, opts) {
|
||||
const wrapper = createElement("div", "numInputWrapper"), numInput = createElement("input", "numInput " + inputClassName), arrowUp = createElement("span", "arrowUp"), arrowDown = createElement("span", "arrowDown");
|
||||
if (navigator.userAgent.indexOf("MSIE 9.0") === -1) {
|
||||
numInput.type = "number";
|
||||
}
|
||||
else {
|
||||
numInput.type = "text";
|
||||
numInput.pattern = "\\d*";
|
||||
}
|
||||
if (opts !== undefined)
|
||||
for (const key in opts)
|
||||
numInput.setAttribute(key, opts[key]);
|
||||
wrapper.appendChild(numInput);
|
||||
wrapper.appendChild(arrowUp);
|
||||
wrapper.appendChild(arrowDown);
|
||||
return wrapper;
|
||||
}
|
||||
export function getEventTarget(event) {
|
||||
try {
|
||||
if (typeof event.composedPath === "function") {
|
||||
const path = event.composedPath();
|
||||
return path[0];
|
||||
}
|
||||
return event.target;
|
||||
}
|
||||
catch (error) {
|
||||
return event.target;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import { int, pad } from "../utils";
|
||||
const doNothing = () => undefined;
|
||||
export const monthToStr = (monthNumber, shorthand, locale) => locale.months[shorthand ? "shorthand" : "longhand"][monthNumber];
|
||||
export const revFormat = {
|
||||
D: doNothing,
|
||||
F: function (dateObj, monthName, locale) {
|
||||
dateObj.setMonth(locale.months.longhand.indexOf(monthName));
|
||||
},
|
||||
G: (dateObj, hour) => {
|
||||
dateObj.setHours(parseFloat(hour));
|
||||
},
|
||||
H: (dateObj, hour) => {
|
||||
dateObj.setHours(parseFloat(hour));
|
||||
},
|
||||
J: (dateObj, day) => {
|
||||
dateObj.setDate(parseFloat(day));
|
||||
},
|
||||
K: (dateObj, amPM, locale) => {
|
||||
dateObj.setHours((dateObj.getHours() % 12) +
|
||||
12 * int(new RegExp(locale.amPM[1], "i").test(amPM)));
|
||||
},
|
||||
M: function (dateObj, shortMonth, locale) {
|
||||
dateObj.setMonth(locale.months.shorthand.indexOf(shortMonth));
|
||||
},
|
||||
S: (dateObj, seconds) => {
|
||||
dateObj.setSeconds(parseFloat(seconds));
|
||||
},
|
||||
U: (_, unixSeconds) => new Date(parseFloat(unixSeconds) * 1000),
|
||||
W: function (dateObj, weekNum, locale) {
|
||||
const weekNumber = parseInt(weekNum);
|
||||
const date = new Date(dateObj.getFullYear(), 0, 2 + (weekNumber - 1) * 7, 0, 0, 0, 0);
|
||||
date.setDate(date.getDate() - date.getDay() + locale.firstDayOfWeek);
|
||||
return date;
|
||||
},
|
||||
Y: (dateObj, year) => {
|
||||
dateObj.setFullYear(parseFloat(year));
|
||||
},
|
||||
Z: (_, ISODate) => new Date(ISODate),
|
||||
d: (dateObj, day) => {
|
||||
dateObj.setDate(parseFloat(day));
|
||||
},
|
||||
h: (dateObj, hour) => {
|
||||
dateObj.setHours(parseFloat(hour));
|
||||
},
|
||||
i: (dateObj, minutes) => {
|
||||
dateObj.setMinutes(parseFloat(minutes));
|
||||
},
|
||||
j: (dateObj, day) => {
|
||||
dateObj.setDate(parseFloat(day));
|
||||
},
|
||||
l: doNothing,
|
||||
m: (dateObj, month) => {
|
||||
dateObj.setMonth(parseFloat(month) - 1);
|
||||
},
|
||||
n: (dateObj, month) => {
|
||||
dateObj.setMonth(parseFloat(month) - 1);
|
||||
},
|
||||
s: (dateObj, seconds) => {
|
||||
dateObj.setSeconds(parseFloat(seconds));
|
||||
},
|
||||
u: (_, unixMillSeconds) => new Date(parseFloat(unixMillSeconds)),
|
||||
w: doNothing,
|
||||
y: (dateObj, year) => {
|
||||
dateObj.setFullYear(2000 + parseFloat(year));
|
||||
},
|
||||
};
|
||||
export const tokenRegex = {
|
||||
D: "(\\w+)",
|
||||
F: "(\\w+)",
|
||||
G: "(\\d\\d|\\d)",
|
||||
H: "(\\d\\d|\\d)",
|
||||
J: "(\\d\\d|\\d)\\w+",
|
||||
K: "",
|
||||
M: "(\\w+)",
|
||||
S: "(\\d\\d|\\d)",
|
||||
U: "(.+)",
|
||||
W: "(\\d\\d|\\d)",
|
||||
Y: "(\\d{4})",
|
||||
Z: "(.+)",
|
||||
d: "(\\d\\d|\\d)",
|
||||
h: "(\\d\\d|\\d)",
|
||||
i: "(\\d\\d|\\d)",
|
||||
j: "(\\d\\d|\\d)",
|
||||
l: "(\\w+)",
|
||||
m: "(\\d\\d|\\d)",
|
||||
n: "(\\d\\d|\\d)",
|
||||
s: "(\\d\\d|\\d)",
|
||||
u: "(.+)",
|
||||
w: "(\\d\\d|\\d)",
|
||||
y: "(\\d{2})",
|
||||
};
|
||||
export const formats = {
|
||||
Z: (date) => date.toISOString(),
|
||||
D: function (date, locale, options) {
|
||||
return locale.weekdays.shorthand[formats.w(date, locale, options)];
|
||||
},
|
||||
F: function (date, locale, options) {
|
||||
return monthToStr(formats.n(date, locale, options) - 1, false, locale);
|
||||
},
|
||||
G: function (date, locale, options) {
|
||||
return pad(formats.h(date, locale, options));
|
||||
},
|
||||
H: (date) => pad(date.getHours()),
|
||||
J: function (date, locale) {
|
||||
return locale.ordinal !== undefined
|
||||
? date.getDate() + locale.ordinal(date.getDate())
|
||||
: date.getDate();
|
||||
},
|
||||
K: (date, locale) => locale.amPM[int(date.getHours() > 11)],
|
||||
M: function (date, locale) {
|
||||
return monthToStr(date.getMonth(), true, locale);
|
||||
},
|
||||
S: (date) => pad(date.getSeconds()),
|
||||
U: (date) => date.getTime() / 1000,
|
||||
W: function (date, _, options) {
|
||||
return options.getWeek(date);
|
||||
},
|
||||
Y: (date) => pad(date.getFullYear(), 4),
|
||||
d: (date) => pad(date.getDate()),
|
||||
h: (date) => (date.getHours() % 12 ? date.getHours() % 12 : 12),
|
||||
i: (date) => pad(date.getMinutes()),
|
||||
j: (date) => date.getDate(),
|
||||
l: function (date, locale) {
|
||||
return locale.weekdays.longhand[date.getDay()];
|
||||
},
|
||||
m: (date) => pad(date.getMonth() + 1),
|
||||
n: (date) => date.getMonth() + 1,
|
||||
s: (date) => date.getSeconds(),
|
||||
u: (date) => date.getTime(),
|
||||
w: (date) => date.getDay(),
|
||||
y: (date) => String(date.getFullYear()).substring(2),
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
export const pad = (number, length = 2) => `000${number}`.slice(length * -1);
|
||||
export const int = (bool) => (bool === true ? 1 : 0);
|
||||
export function debounce(fn, wait) {
|
||||
let t;
|
||||
return function () {
|
||||
clearTimeout(t);
|
||||
t = setTimeout(() => fn.apply(this, arguments), wait);
|
||||
};
|
||||
}
|
||||
export const arrayify = (obj) => obj instanceof Array ? obj : [obj];
|
||||
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
if (typeof Object.assign !== "function") {
|
||||
Object.assign = function (target, ...args) {
|
||||
if (!target) {
|
||||
throw TypeError("Cannot convert undefined or null to object");
|
||||
}
|
||||
for (const source of args) {
|
||||
if (source) {
|
||||
Object.keys(source).forEach((key) => (target[key] = source[key]));
|
||||
}
|
||||
}
|
||||
return target;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user