UNPKG

@beenotung/tslib

Version:
219 lines 6.5 kB
"use strict"; /** * Created by beenotung on 3/9/17. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.format_percentage = exports.format_n_digit = exports.format_timestamp_code = exports.format_time_code = exports.format_2_digit = exports.format_relative_time = exports.format_time_duration = exports.format_long_short_time = exports.format_datetime = exports.format_byte = exports.setLang = void 0; const en_1 = require("./en"); const locale_1 = require("./locale"); const time_1 = require("./time"); const size_units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const time_units_en = [ [time_1.CENTURY, 'century'], [time_1.DECADE, 'decade'], [time_1.YEAR, 'year'], [time_1.MONTH, 'month'], [time_1.WEEK, 'week'], [time_1.DAY, 'day'], [time_1.HOUR, 'hour'], [time_1.MINUTE, 'minute'], [time_1.SECOND, 'second'], ]; const time_units_zh = [ [time_1.CENTURY, '世紀'], [time_1.YEAR, '年'], [time_1.MONTH, '月'], [time_1.WEEK, '週'], [time_1.DAY, '日'], [time_1.HOUR, '小時'], [time_1.MINUTE, '分鐘'], [time_1.SECOND, '秒'], ]; /* tslint:disable:quotemark */ const word_en = { instantly: 'instantly', 'just now': 'just now', hence: 'hence', ago: 'ago', }; const word_zh = { instantly: '頃刻', 'just now': '剛剛', hence: '後', ago: '前', }; /* tslint:enable:quotemark */ let word = word_en; let time_units = time_units_en; let locale = locale_1.getEnvLocale() || 'en'; function setLang(lang) { locale = lang; if (lang.includes('zh')) { time_units = time_units_zh; word = word_zh; } else { time_units = time_units_en; word = word_en; } } exports.setLang = setLang; setLang(locale); function concatWords(a, b) { if (en_1.isEngChar(a[a.length - 1]) || en_1.isEngChar(b[0])) { return a + ' ' + b; } return a + b; } function format_byte(n_byte, n_decimal = 2) { let acc = n_byte; for (const size_unit of size_units) { if (acc < 1024) { return acc.toFixed(n_decimal) + ' ' + size_unit; } acc /= 1024; } return ((acc * 1024).toFixed(n_decimal) + ' ' + size_units[size_units.length - 1]); } exports.format_byte = format_byte; function format_datetime(time, options = {}) { if (!time) { return options.empty || '-'; } const locales = options.locales || locale_1.getEnvLocale() || locale; return new Date(time).toLocaleString(locales, { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', hour12: true, minute: '2-digit', }); } exports.format_datetime = format_datetime; const roundUnits = time_units_en.map(([unit]) => unit).sort((a, b) => b - a); function roundTime(timeDiff) { const absDiff = Math.abs(timeDiff); for (const unit of roundUnits) { if (absDiff > unit) { if (timeDiff > 0) { return Math.floor(timeDiff / unit) * unit; } else { return Math.ceil(timeDiff / unit) * unit; } } } return timeDiff; } function format_long_short_time(time, options) { // if within 1-week, format relative time, else format absolute time const diff = time - Date.now(); if (Math.abs(diff) < ((options === null || options === void 0 ? void 0 : options.threshold) || time_1.WEEK)) { return format_relative_time(roundTime(diff)); } return format_datetime(time); /* return new Intl.DateTimeFormat('zh-HK', { month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', year: 'numeric', // hour12: true, }).format(new Date()); */ } exports.format_long_short_time = format_long_short_time; function format_time_duration(delta, digit = 1) { const diff = Math.abs(delta); const res = (n, unit) => { const p = Math.pow(10, digit); n = Math.round(n * p) / p; if (n > 1) { unit = en_1.to_plural(unit); } return n + ' ' + unit; }; for (const [size, unit] of time_units) { if (diff > size) { return res(diff / size, unit); } } return word.instantly; } exports.format_time_duration = format_time_duration; function format_relative_time(delta, digit = 1) { const s = format_time_duration(delta, digit); if (s === word.instantly) { return word['just now']; } if (delta > 0) { return concatWords(s, word.hence); } else { return concatWords(s, word.ago); } } exports.format_relative_time = format_relative_time; /** * mainly for formatting month, date, hour, minute, and second * @param x: [0..60] * */ function format_2_digit(x) { if (x < 10) { return '0' + x; } return x.toString(); } exports.format_2_digit = format_2_digit; /** * format: YYYYMMDD-HHMM * less accurate, for human reading * */ function format_time_code(time, separator = '-') { const t = new Date(time); const y = t.getFullYear(); const m = format_2_digit(t.getMonth() + 1); const d = format_2_digit(t.getDate()); const H = format_2_digit(t.getHours()); const M = format_2_digit(t.getMinutes()); return y + m + d + separator + H + M; } exports.format_time_code = format_time_code; /** * format: YYYYMMDD-HHMM-SS-sss * more accurate, for filename * */ function format_timestamp_code(time, separator = '-') { const t = new Date(time); const y = t.getFullYear(); const m = format_2_digit(t.getMonth() + 1); const d = format_2_digit(t.getDate()); const H = format_2_digit(t.getHours()); const M = format_2_digit(t.getMinutes()); const S = format_2_digit(t.getSeconds()); const s = format_n_digit(t.getMilliseconds(), 3); return y + m + d + separator + H + M + separator + S + separator + s; } exports.format_timestamp_code = format_timestamp_code; function format_n_digit(x, n, prefix = '0') { let s = Math.abs(x).toString(); if (x < 0) { n--; } if (s.length < n) { s = prefix.repeat(n - s.length) + s; } if (x < 0) { s = '-' + s; } return s; } exports.format_n_digit = format_n_digit; function format_percentage(p, n_decimal = 2) { return Math.round(p * Math.pow(10, 2 + n_decimal)) / 100 + '%'; } exports.format_percentage = format_percentage; //# sourceMappingURL=format.js.map