@zohodesk/components
Version:
In this Package, we Provide Some Basic Components to Build Web App
204 lines (191 loc) • 6.28 kB
JavaScript
import { offsetMap } from "./GMTZones.js";
let dateFormat = {
dayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
timeNames: ['a', 'p', 'am', 'pm', 'A', 'P', 'AM', 'PM']
};
export function pad(n) {
let width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
let z = arguments.length > 2 ? arguments[2] : undefined;
z = z || '0';
n = `${n}`;
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
export function getTimeOffset() {
const GMT = new Date();
const matchingOffset = Object.keys(offsetMap).find(offset => GMT.getTimezoneOffset() === Number(offset));
return matchingOffset ? offsetMap[matchingOffset].toString().split(' ') : ['', ''];
}
export function formatDate(dateMill, mask) {
let date = new Date(dateMill);
let O = getTimeOffset()[0];
let Z = getTimeOffset()[1];
let d = date.getDate();
let D = date.getDay();
let m = date.getMonth();
let y = date.getFullYear();
let H = date.getHours();
let M = date.getMinutes();
let s = date.getSeconds();
let L = date.getMilliseconds();
let flags = {
d: d,
O: O,
dd: pad(d, 2),
ddd: dateFormat.dayNames[D],
dddd: dateFormat.dayNames[D + 7],
D: d,
DD: pad(d, 2),
DDD: dateFormat.dayNames[D],
DDDD: dateFormat.dayNames[D + 7],
M: m + 1,
MM: pad(m + 1, 2),
MMM: dateFormat.monthNames[m],
MMMM: dateFormat.monthNames[m + 12],
yy: String(y).slice(2),
YY: String(y).slice(2),
yyyy: y,
YYYY: y,
h: H % 12 || 12,
hh: pad(H % 12 || 12, 2),
H: H,
HH: pad(H, 2),
m: M,
mm: pad(M, 2),
s: s,
ss: pad(s, 2),
l: pad(L, 3),
L: pad(Math.round(L / 10)),
t: H < 12 ? dateFormat.timeNames[0] : dateFormat.timeNames[1],
A: H < 12 ? dateFormat.timeNames[6] : dateFormat.timeNames[7],
a: H < 12 ? dateFormat.timeNames[6] : dateFormat.timeNames[7],
T: H < 12 ? dateFormat.timeNames[4] : dateFormat.timeNames[5],
Z: Z,
VV: Z,
SSS: pad(L, 3)
};
let token = /D{1,4}|d{1,4}|M{1,4}|YYYY|yyyy|YY|O|Z|VV|SSS|yy?|([HhmsAa])\1?|[LloSZWN]|\[[^\]]*\]|'[^']*'/g;
let dat = mask.replace(token, match => {
if (match in flags) {
return flags[match];
}
return match.slice(1, match.length - 1);
});
return dat;
}
export function replaceI18NValuesWithRegex(i18nStr, values) {
if (typeof values !== 'undefined') {
if (Array.isArray(values)) {
for (let i = 0; i < values.length; i++) {
i18nStr = i18nStr.replace(new RegExp(`\\{${i}\\}`, 'g'), values[i]);
}
} else {
i18nStr = i18nStr.replace(new RegExp('\\{0\\}', 'g'), values);
}
}
return i18nStr;
}
export function unescapeUnicode(str) {
return str.replace(/\\u([a-fA-F0-9]{4})/g, (g, m1) => String.fromCharCode(parseInt(m1, 16)));
}
export function getValues() {
let params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
let diff = arguments.length > 1 ? arguments[1] : undefined;
return params.map(param => diff[param]);
}
export function getI18NValue(i18n) {
if (typeof i18n === 'undefined') {
return key => key;
}
return (key, values) => {
let i18nStr = i18n[key];
if (i18nStr === undefined) {
return key;
}
i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
return unescapeUnicode(i18nStr);
};
} // function getValues(params = [], diff) {
// return params.map(param => {
// return diff[param];
// });
// }
export function getI18NInfo(toDateObj, props, diffObj) {
let key = null,
values,
text = null;
if (typeof props === 'function') {
let value = props(diffObj1);
key = value.key;
values = getValues(value.params, diffObj);
} else if (typeof props === 'object') {
key = props.key;
values = getValues(props.params, diffObj);
} else if (typeof props === 'string') {
text = toDateObj.format(props);
}
return {
key,
values,
text
};
}
export function isToday(fromDate, toDate) {
let TODAY = toDate.clone().startOf('day');
return fromDate.isSame(TODAY, 'd');
}
export function isYesterday(fromDate, toDate) {
let YESTERDAY = toDate.clone().subtract(1, 'days').startOf('day');
return fromDate.isSame(YESTERDAY, 'd');
}
export function isTomorrow(fromDate, toDate) {
return isYesterday(toDate, fromDate);
}
export function isWithinAWeek(fromDate, toDate) {
let A_WEEK_OLD = toDate.clone().subtract(7, 'days').startOf('day');
return fromDate.isAfter(A_WEEK_OLD);
}
export function isTwoWeeksOrMore(fromDate, toDate) {
return !isWithinAWeek(fromDate, toDate);
}
let oneYearInMillis = 31536000000;
let oneDayInMillis = 86400000;
let oneHourInMillis = 3600000;
let oneMinuteInMillis = 60000;
function convertAsNonExponential(number) {
if (number.toString().toLowerCase().indexOf('e') !== -1) {
return number.toFixed(20);
}
return number;
}
export function getDiffObj(diff) {
diff = Math.abs(diff);
let diffYears = diff / oneYearInMillis;
diffYears = convertAsNonExponential(diffYears);
let diffDays = diff % oneYearInMillis / oneDayInMillis;
diffDays = convertAsNonExponential(diffDays);
let diffHours = diff % oneDayInMillis / oneHourInMillis;
diffHours = convertAsNonExponential(diffHours);
let diffMinutes = diff % oneDayInMillis % oneHourInMillis / oneMinuteInMillis;
let diffSeconds = diff % oneDayInMillis % oneHourInMillis % oneMinuteInMillis / 1000;
diffDays = parseInt(diffDays);
diffHours = parseInt(diffHours);
diffMinutes = parseInt(diffMinutes);
diffSeconds = parseInt(diffSeconds);
diffYears = parseInt(diffYears);
return {
y: diffYears,
yd: diffDays,
h: diffHours,
m: diffMinutes,
s: diffSeconds
};
}
export function getMonthEnd(month, year) {
let monthend = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let isLeapYear = year % 400 === 0 || year % 4 === 0 && year % 100 !== 0;
if (month === 1 && isLeapYear) {
return 29;
}
return monthend[month];
}