UNPKG

log4js2

Version:

[![Build Status](https://travis-ci.org/anigenero/log4js2.svg?branch=master)](https://travis-ci.org/anigenero/log4js2) [![codecov](https://codecov.io/gh/anigenero/log4js2/branch/master/graph/badge.svg)](https://codecov.io/gh/anigenero/log4js2)

105 lines 4.2 kB
Object.defineProperty(exports, "__esModule", { value: true }); // tslint:disable-next-line const TIMEZONE = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g; const TIMEZONE_CLIP = /[^-+\dA-Z]/g; /** * Pads numbers in the date format * * @param {string|number} value * @param {number} length * @param {string} character * * @returns {string} the padded string */ exports.padLeft = (value, length, character = ' ') => { let strValue = String(value); while (strValue.length < length) { strValue = character + strValue; } return strValue; }; var DateTimeFormat; (function (DateTimeFormat) { DateTimeFormat["DEFAULT"] = "yyyy-MM-dd HH:mm:ss,S"; DateTimeFormat["ABSOLUTE"] = "HH:mm:ss,S"; DateTimeFormat["COMPACT"] = "yyyyMMddHHmmssS"; DateTimeFormat["DATE"] = "dd MMM yyyy HH:mm:ss,S"; DateTimeFormat["ISO8601"] = "yyyy-MM-ddTHH:mm:ss,S"; DateTimeFormat["ISO8601_BASIC"] = "yyyyMMddTHHmmss,S"; })(DateTimeFormat = exports.DateTimeFormat || (exports.DateTimeFormat = {})); const _i18n = { d: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], m: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] }; const _maskStore = {}; const _flags = { d: (date) => date.getDate(), dd: (date) => exports.padLeft(date.getDate(), 2, '0'), ddd: (date) => _i18n.d[date.getDay()], dddd: (date) => _i18n.d[date.getDay() + 7], M: (date) => date.getMonth() + 1, MM: (date) => exports.padLeft(date.getMonth() + 1, 2, '0'), MMM: (date) => _i18n.m[date.getMonth()], MMMM: (date) => _i18n.m[date.getMonth() + 12], yy: (date, isUTC) => String(isUTC ? date.getFullYear() : date.getUTCFullYear()).slice(2), yyyy: (date, isUTC) => isUTC ? date.getFullYear() : date.getUTCFullYear(), h: (date) => date.getHours() % 12 || 12, hh: (date) => exports.padLeft(date.getHours() % 12 || 12, 2, '0'), H: (date) => date.getHours(), HH: (date) => exports.padLeft(date.getHours(), 2, '0'), m: (date) => date.getMinutes(), mm: (date) => exports.padLeft(date.getMinutes(), 2, '0'), s: (date) => date.getSeconds(), ss: (date) => exports.padLeft(date.getSeconds(), 2, '0'), S: (date) => exports.padLeft(date.getMilliseconds(), 1), a: (date) => date.getHours() < 12 ? 'a' : 'p', aa: (date) => date.getHours() < 12 ? 'am' : 'pm', A: (date) => date.getHours() < 12 ? 'A' : 'P', AA: (date) => date.getHours() < 12 ? 'AM' : 'PM', Z: (date, isUTC) => isUTC ? 'UTC' : (String(date).match(TIMEZONE)).pop().replace(TIMEZONE_CLIP, ''), o: (date, isUTC) => { const offset = isUTC ? 0 : date.getTimezoneOffset(); return (offset > 0 ? '-' : '+') + exports.padLeft(Math.floor(Math.abs(offset) / 60) * 100 + Math.abs(offset) % 60, 4, '0'); } }; function _createMask(mask) { if (_maskStore[mask] instanceof Array) { return _maskStore[mask]; } const formatMap = []; const regex = /(.)\1+|(.)/; let maskStr = mask; let match; let value; // tslint:disable-next-line:no-conditional-assignment while (match = maskStr.match(regex)) { value = match[0]; formatMap.push(_flags[value] || value); maskStr = maskStr.substring(value.length); } _maskStore[mask] = formatMap; return formatMap; } /** * Converts a date to a formatted string * @param {Date | string | number} date * @param {string} mask * @returns {string} */ exports.formatDate = (date, mask) => { if (!(date instanceof Date)) { date = new Date(date); } mask = String(mask || DateTimeFormat.DEFAULT); const isUTC = (mask.slice(0, 4) === 'UTC:'); if (isUTC) { mask = mask.slice(4); } return _createMask(mask).map((value) => { return value instanceof Function ? value(date, isUTC) : value; }).join(''); }; //# sourceMappingURL=date.formatter.js.map