Files
SaaS-lib/lib/util/unit-util.ts
xuziqiang d0155dbe3c push
2024-05-15 17:29:42 +08:00

175 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export class unitTransfer {
//单位进制
unitConfig = {
B: ['B', 'KB', 'MB', 'GB', 'TB', 'PB'],
b: ['b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb'],
// def: ['', 'K', 'mill', 'bill', 'trill'],
def: ['', 'K', 'M', 'B', 'T', 'Q'],
}
constructor() {
}
/**
* 单位转换,统一用于监控指标的单位换算问题
* @param value 数值
* @param unit 单位
* @param params {tagUnit:想要转化到的单位级别unitDisplay是否需要输出单位}
*/
unitTransfom(value, unit?, params?: {tagUnit?:string,unitDisplay?:boolean}) {
if (!value || typeof (value) != 'number') {
return value;
}
if ([undefined, null, ''].indexOf(unit) != -1) {
unit = ''
}
let index = unit.indexOf('/');
let moreUnit = '';
let resultValue;
let tagUnit;
let unitDisplay = true;
if (params) {
tagUnit = params['tagUnit'] || null;
unitDisplay = params['unitDisplay'] || true;
}
if (index > 0) {
moreUnit = unit.substring(index);
unit = unit.split('/')[0];
}
if (unit == '%') {
resultValue = value + unit;
}
if (this.unitConfig.def.indexOf(unit) > -1 || unit == 'nub') {
unit === 'nub' ? unit = '' : '';
resultValue = this.formatterFun(value, 'def', { 'tagUnit': tagUnit, 'defaulUnit': unit })
}
if (this.unitConfig.B.indexOf(unit) > -1) {
resultValue = this.formatterFun(value, 'B', { 'defaulvalue': 1024, 'tagUnit': tagUnit, 'defaulUnit': unit }) + moreUnit;
}
if (this.unitConfig.b.indexOf(unit) > -1) {
resultValue = this.formatterFun(value, 'b', { 'defaulvalue': 1024, 'tagUnit': tagUnit, 'defaulUnit': unit }) + moreUnit;
}
if (unit == 'cnt') {
resultValue = this.formatterFun(value, 'def', { 'defaulvalue': 1000, 'defaulUnit': '' }) + '个' + moreUnit;
}
if (unit == 'ms' || unit == 's') {
resultValue = this.Flavorunit(value, unit) + moreUnit;
}
if (unit == 'bout') {
resultValue = this.formatterFun(value, 'def', { 'defaulvalue': 1000, 'defaulUnit': '' }) + '次' + moreUnit;
}
//判断需不需要输出单位
if (!unitDisplay) {
resultValue = resultValue.replace(/[^0-9,.]*/ig, '');
}
return resultValue
}
/**
* 此处为时间的单位转换
* @param value
* @param unit
*/
Flavorunit(value, unit?) {
if (value >= 1000 && unit == 'ms') {
unit = 's';
value = (value / 1000).toFixed(2);
}
if (value >= 60 && unit == 's') {
unit = 'min';
value = (value / 60).toFixed(2);
}
if (value >= 60 && unit == 'min') {
unit = 'h';
value = (value / 60).toFixed(2);
}
if (value >= 24 && unit == 'h') {
unit = 'd';
value = (value / 24).toFixed(2);
}
return Number(value) + unit;
}
/**
*
* @param value 数值
* @param unit 单位类别,如果没有默认单位,也可以作为默认单位
* @param params {
* defaulvalue 默认进制的值没有值取1000,
* defaulUnit 起步单位,没有的话使用单位类别的值作为默认单位,
* tagUnit 目标单位,需要转化到改单位
* point 保留小数几位
* }
*/
formatterFun(value, unit, params?: {}) {
let defaulvalue, defaulUnit, tagUnit, point
if (params) {
defaulvalue = params['defaulvalue'] ? params['defaulvalue'] : 1000;
defaulUnit = params['defaulUnit'];
tagUnit = params['tagUnit'] ? params['tagUnit'] : null;
point = params['point'] ? params['point'] : 2;
}
let count = 0;
//判断有没有目标单位
if (tagUnit) {
let indexOfNow = this.unitConfig[unit].indexOf(defaulUnit);
let indexOfTag = this.unitConfig[unit].indexOf(tagUnit);
const n = indexOfTag - indexOfNow;
value = value / Math.pow(defaulvalue, n);
} else {
while (value >= defaulvalue && count < 6) {
value = value / defaulvalue;
count++;
}
}
value = Number(value.toFixed(point));
let index: number = 0;
let resultUnit: string = '';
if (this.unitConfig[unit].indexOf(defaulUnit) >= 0) {
index = this.unitConfig[unit].indexOf(defaulUnit);
resultUnit = this.unitConfig[unit][count + index];
}
if (tagUnit) {
resultUnit = tagUnit;
}
return value + resultUnit;
}
/**
* 日期格式转换
* y+ 待办匹配y一次以上替换为年其他类似
*/
dateFormat(time, fmt) {
try {
const date = new Date(time);
let ret;
const opt = {
'y+': date.getFullYear().toString(), // 年
'm+': (date.getMonth() + 1).toString(), // 月
'd+': date.getDate().toString(), // 日
'H+': date.getHours().toString(), // 时
'M+': date.getMinutes().toString(), // 分
'S+': date.getSeconds().toString(), // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
};
for (let k in opt) {
ret = new RegExp('(' + k + ')').exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')))
}
}
return fmt;
} catch (err) {
console.log(err);
return '';
}
};
}
export const unitUtil = new unitTransfer();