UNPKG

commonui-lib-test

Version:

"#common ui lib test"

106 lines (105 loc) 4.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); //import numeral from "numeral"; const numeral = require("numeral"); const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const lodash_es_1 = require("lodash-es"); function formatCurrency(s, n = 0, formatOption) { let dotIndex = s.indexOf("."); let t0 = dotIndex < 0 ? s : s.substring(0, dotIndex + n + 1); let t1 = formatOption || "0,0"; let t2 = ""; for (let i = 0; i < n; i++) t2 += "0"; if (n > 0) t1 = t1 + "." + t2; return numeral(t0).format(t1); } exports.formatCurrency = formatCurrency; function formatShortNumeral(s, roundingFunction = null) { return numeral(s).format("0.[00]a", roundingFunction); } exports.formatShortNumeral = formatShortNumeral; function formatPercentages(s) { return numeral(s).format("0%"); } exports.formatPercentages = formatPercentages; function formatWithOption(s, formatOption = "", roundingFunction = null) { return numeral(s).format(formatOption, roundingFunction); } exports.formatWithOption = formatWithOption; function convertString2Numeral(s) { let n = numeral(s); return n.value(); } exports.convertString2Numeral = convertString2Numeral; function createCountdownTimer(time, interval) { if (isNaN(time)) return; let takeCount = lodash_es_1.add(lodash_es_1.floor(lodash_es_1.divide(time, interval)), 1); let waitTime = time % interval; let mapTime = lodash_es_1.subtract(time, waitTime); let mapHandle = (i) => lodash_es_1.subtract(mapTime, i * interval); let clockTimer = rxjs_1.timer(waitTime, interval).pipe(operators_1.map(i => mapHandle(i)), operators_1.take(takeCount)); let timerObservable = rxjs_1.concat(rxjs_1.of(time), clockTimer); return timerObservable; } exports.createCountdownTimer = createCountdownTimer; function formatActorNameString(value, wordCount, omissionString) { return (omissionString || "***").concat(value.length > wordCount ? value.substring(value.length - wordCount) : value); } exports.formatActorNameString = formatActorNameString; function formatShortString(value, maxlength, omissionString) { let options = { length: maxlength || 6, omission: "" }; return value.length > options.length ? lodash_es_1.truncate(value, options).concat(omissionString || "...") : value; } exports.formatShortString = formatShortString; function formatStringSize(target, str, maxFontSize) { const checkWidth = () => { return target.width >= target.displayObject.textWidth; }; const checkHeight = () => { return target.height >= target.displayObject.textHeight; }; const sizeFromData = () => { if (!target.data || !target.data.hasOwnProperty("maxFontSize")) target.data = { "maxFontSize": target.fontSize }; return target.data["maxFontSize"]; }; target.text = str; let nowSize = target.fontSize; let maxSize = maxFontSize || sizeFromData(); if (checkWidth() && checkHeight()) { for (let i = nowSize; i <= maxSize; i++) { target.fontSize = i; if (checkWidth() && checkHeight()) continue; target.fontSize = i - 1; return; } } else { for (let i = nowSize; i > 1; i--) { target.fontSize = i; if (checkWidth() && checkHeight()) return; } } } exports.formatStringSize = formatStringSize; //讓輸入的字串可以包含{} function getStringFormatPlaceHolderRegEx(placeHolderIndex) { return new RegExp("({)?\\{" + placeHolderIndex + "\\}(?!})", "gm"); } //當format格式有多餘的position時,就不會將多餘的position輸出 //ex: // var fullName = "Hello. My name is {0} {1} {2}.".format("firstName", "lastName"); // 輸出的 fullName 為 "firstName lastName", 而不會是 "firstName lastName {2}" function cleanStringFormatResult(txt) { if (txt == null) return ""; return txt.replace(getStringFormatPlaceHolderRegEx("\\d+"), ""); } let commonUtils = { formatCurrency, formatShortNumeral, formatPercentages, formatWithOption, convertString2Numeral, createCountdownTimer, formatActorNameString, formatShortString, formatStringSize }; exports.default = commonUtils;