UNPKG

time-kinesis

Version:

A simple and powerful node.js Library to control time

146 lines (145 loc) 5.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const datetime_1 = require("./datetime"); const timezones_1 = require("./timezones"); const units_1 = require("./units"); const monthNames = { 1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December' }; const timezoneReplacers = { ZZZZ: (datetime, timezone) => timezone.description, ZZZ: (datetime, timezone) => timezone.name, ZZ: (datetime, timezone) => timezone.utc, Z: (datetime, timezone) => { let tz = new datetime_1.DateTime(0).tz(timezones_1.timezones.UTC).add(Math.abs(timezone.offset), units_1.units.hours).format('HH:mm'); if (timezone.offset >= 0) return `+${tz}`; return `-${tz}`; }, z: (datetime, timezone) => timezone.offset }; const monthReplacers = { // @ts-ignore MMMM: (datetime, timezone) => monthNames[(datetime.getDate().getMonth() + 1)], // @ts-ignore MMM: (datetime, timezone) => monthNames[(datetime.getDate().getMonth() + 1)].slice(0, 3), // @ts-ignore MM: (datetime, timezone) => (datetime.getDate().getMonth() + 1).toString().padStart(2, '0'), M: (datetime, timezone) => datetime.getDate().getMonth() + 1, Mo: (datetime, timezone) => { let month = datetime.getDate().getMonth() + 1; if (month == 1) return `${month}st`; if (month == 2) return `${month}nd`; if (month == 3) return `${month}rd`; return `${month}th`; } }; const yearReplacers = { YYYY: (datetime, timezone) => datetime.getDate().getFullYear(), YY: (datetime, timezone) => datetime.getDate().getFullYear().toString().slice(2, 4) }; const dayOfMonthReplacers = { // @ts-ignore DD: (datetime, timezone) => datetime.getDate().getDate().toString().padStart(2, '0'), D: (datetime, timezone) => datetime.getDate().getDate() }; const hourReplacers = { // @ts-ignore HH: (datetime, timezone) => datetime.getDate().getHours().toString().padStart(2, '0'), H: (datetime, timezone) => datetime.getDate().getHours(), h: (datetime, timezone) => { let hours = datetime.getDate().getHours(); if (hours <= 12) return hours; return hours - 12; }, hh: (datetime, timezone) => { let hours = datetime.getDate().getHours(); // @ts-ignore if (hours <= 12) return hours.toString().padStart(2, '0'); // @ts-ignore return (hours - 12).toString().padStart(2, '0'); }, }; const minuteReplacers = { // @ts-ignore mm: (datetime, timezone) => datetime.getDate().getMinutes().toString().padStart(2, '0'), m: (datetime, timezone) => datetime.getDate().getMinutes(), }; const millisecondReplacers = { // @ts-ignore sss: (datetime, timezone) => datetime.getDate().getMilliseconds().toString().padStart(3, '0'), }; const secondReplacers = { // @ts-ignore ss: (datetime, timezone) => datetime.getDate().getSeconds().toString().padStart(2, '0'), s: (datetime, timezone) => datetime.getDate().getSeconds() }; const replacers = Object.assign({}, timezoneReplacers, yearReplacers, monthReplacers, dayOfMonthReplacers, hourReplacers, minuteReplacers, millisecondReplacers, secondReplacers); // @ts-ignore function desc(arr) { // @ts-ignore return arr.sort((a, b) => { return b.index - a.index; }); } const replaceDeadzones = (deadzones, target) => { let result = target.slice(); for (let deadzone of deadzones) { result = `${result.slice(0, deadzone.index)}${result.slice(deadzone.index + deadzone.length)}`; } return result; }; const isDeadZone = (deadzones, index) => { for (let deadzone of deadzones) { if (index >= deadzone.index && index <= (deadzone.index + deadzone.length)) return deadzone; } return; }; function format(datetime, pattern) { let timezone = timezones_1.getTimezone(datetime.timezone); let deadzones = []; let replaces = []; for (let rep of Object.keys(replacers)) { if (!replaceDeadzones(deadzones, pattern).includes(rep)) continue; let index = pattern.lastIndexOf(rep); while (index > -1) { let deadzone = isDeadZone(deadzones, index); if (deadzone) { index = pattern.slice(0, deadzone.index).lastIndexOf(rep); continue; } let length = rep.length; deadzones.push({ index, length }); desc(deadzones); // @ts-ignore replaces.push({ replacer: replacers[rep], index, length }); index = pattern.slice(0, index).lastIndexOf(rep); } } let result = pattern.slice(); desc(replaces); for (let replace of replaces) { result = `${result.slice(0, replace.index)}${replace.replacer(datetime, timezone)}${result.slice(replace.index + replace.length)}`; } return result; } exports.format = format; ;