update formatters

This commit is contained in:
wxzhang 2022-08-09 22:01:05 +08:00
parent 7b75e57195
commit 6d9efc5302
3 changed files with 58 additions and 54 deletions

View File

@ -36,19 +36,7 @@ function dict(value, ...args) {
if (args.length > 0 && (args.length % 2 !== 0)) return args[args.length - 1] if (args.length > 0 && (args.length % 2 !== 0)) return args[args.length - 1]
return value return value
} }
/**
* 格式化货币
* formatCurrency("123456789") == "123,456,789"
* formatCurrency("123456789",4) == "1,2345,6789"
* @param {*} value
* @param {*} bit 逗号分割位置
*/
function formatCurrency(value, bit = 3) {
if (!isNumber(value)) return value
}
/** /**
* 格式化日期 * 格式化日期
@ -59,7 +47,7 @@ function toDate(value) {
try { try {
return value instanceof Date ? value : new Date(value) return value instanceof Date ? value : new Date(value)
} catch { } catch {
return value == undefined ? "" : value return value
} }
} }
@ -71,7 +59,7 @@ function toNumber(value,defualt=0) {
return defualt return defualt
} }
} catch { } catch {
return value == undefined ? "" : value return value
} }
} }
@ -80,14 +68,23 @@ const CHINESE_UNITS = ['', '十', '百', '千', '万', '十', '百', '千',
const CHINESE_BIG_DIGITS = ["零", '壹', '貳', '參', '肆', '伍', '陸', '柒', '捌', '玖'] const CHINESE_BIG_DIGITS = ["零", '壹', '貳', '參', '肆', '伍', '陸', '柒', '捌', '玖']
const CHINESE_BIG_UNITS = ['', '拾', '佰', '仟', '萬', '拾', '佰', '仟', '億', '拾', '佰', '仟', '兆', '拾', '佰', '仟', '京', '拾', '佰', '仟', '垓'] const CHINESE_BIG_UNITS = ['', '拾', '佰', '仟', '萬', '拾', '佰', '仟', '億', '拾', '佰', '仟', '兆', '拾', '佰', '仟', '京', '拾', '佰', '仟', '垓']
function toChineseNumber(value,isBig=false) { /**
if(typeof(value)!=="number") return value; *
* 将数字转换为中文数字
*
* 注意会忽略掉小数点后面的数字
*
* @param {*} value 数字
* @param {*} isBig 是否大写数字
* @returns
*/
function toChineseNumber(value,isBig) {
if(!isNumber(value)) return value;
let [wholeValue,decimalValue] = String(value).split(".") // 处理小数点 let [wholeValue,decimalValue] = String(value).split(".") // 处理小数点
const DIGITS = isBig ? CHINESE_BIG_DIGITS : CHINESE_DIGITS const DIGITS = isBig ? CHINESE_BIG_DIGITS : CHINESE_DIGITS
const UNITS = isBig ? CHINESE_BIG_UNITS : CHINESE_UNITS const UNITS = isBig ? CHINESE_BIG_UNITS : CHINESE_UNITS
// 整数部份
let result = '' let result = ''
if(wholeValue.length==1) return DIGITS[parseInt(wholeValue)]
for(let i=wholeValue.length-1; i>=0; i--){ for(let i=wholeValue.length-1; i>=0; i--){
let bit = parseInt(wholeValue[i]) let bit = parseInt(wholeValue[i])
let digit = DIGITS[bit] let digit = DIGITS[bit]
@ -95,7 +92,6 @@ function toChineseNumber(value,isBig=false) {
if(bit==0){ if(bit==0){
let preBit =i< wholeValue.length ? parseInt(wholeValue[i+1]) : null// 上一位 let preBit =i< wholeValue.length ? parseInt(wholeValue[i+1]) : null// 上一位
let isKeyBits = ((wholeValue.length-i-1) % 4)==0 let isKeyBits = ((wholeValue.length-i-1) % 4)==0
//if(preBit && preBit!=0) result = "零" + result
if(preBit && preBit!=0 && !isKeyBits) result = "零" + result if(preBit && preBit!=0 && !isKeyBits) result = "零" + result
if(isKeyBits) result = UNITS[wholeValue.length-i-1] + result if(isKeyBits) result = UNITS[wholeValue.length-i-1] + result
}else{ }else{
@ -116,12 +112,8 @@ function toChineseNumber(value,isBig=false) {
.replace("万千","万") .replace("万千","万")
if(result.startsWith("一十")) result=result.substring(1) if(result.startsWith("一十")) result=result.substring(1)
} }
// 中文数字忽略小数部分 return result // 中文数字忽略小数部分
return result }
}
function toChineseBigNumber(value) {
return toChineseNumber(value,true)
}
/** /**
@ -147,19 +139,29 @@ function toCurrency(value,{division=3,unit="",precision=2}={}){
/** /**
* 转换为中文大写货币 * 转换为中文大写货币
*
*
*
* @param {*} value * @param {*} value
* @param {*} division 分割符号位数,3代表每3个数字添加一个, * @param {*} division 分割符号位数,3代表每3个数字添加一个,
* @param {*} unit 货币单位 * @param {*} unit 货币单位
* @param {*} precision 小数点精确到几位 * @param {*} precision 小数点精确到几位
*/ */
function toChineseCurrency(value,division=3,unit="¥"){ function toChineseCurrency(value,padding){
let result = [] let [wholeValue,decimalValue] = String(value).split(".")
let v = String(value) let result = `${toChineseNumber(wholeValue,true)}`
for(let i=0;i<v.length;i++){ if(padding){
if(((v.length - i) % division)==0 && i<v.length-1) result.push(",") if(decimalValue){
result.push(v[i]) decimalValue=decimalValue.padEnd(2,"0")
}else{
decimalValue="00"
}
}
if(decimalValue){
if(decimalValue[0]) result=result+`${toChineseNumber(decimalValue[0],true)}`
if(decimalValue[1]) result=result+`${toChineseNumber(decimalValue[1],true)}`
} }
return unit+result.join("") return result
} }
@ -189,11 +191,15 @@ module.exports = {
millisecond : value => toDate(value).getMilliseconds(), millisecond : value => toDate(value).getMilliseconds(),
timestamp : value => toDate(value).getTime(), timestamp : value => toDate(value).getTime(),
// 数字 // 数字
number : (value) => toNumber(value), number : (value) => toNumber(value),
error : (value, tips = 'ERROR') => value instanceof Error ? tips : value,
empty : (value) => value, // 货币
currency: (value,unit="$",division=3,precision=2) => toCurrency(value,{division,unit,precision}),
capital: value=>value, capital: value=>value,
dict, dict,
error : (value, tips = 'ERROR') => value instanceof Error ? tips : value,
empty : (value) => value
}, },
zh: { zh: {
$types: { $types: {
@ -207,15 +213,12 @@ module.exports = {
shorMonthName: value => ["一","二","三","四","五","六","七","八","九","十","十一","十二"][value.getMonth()] shorMonthName: value => ["一","二","三","四","五","六","七","八","九","十","十一","十二"][value.getMonth()]
// 时间 // 时间
time : value => `${value.getHours()}${value.getMinutes()}${value.getSeconds()}` time : value => `${value.getHours()}${value.getMinutes()}${value.getSeconds()}`
// 数字 // 数字
number : (value) => toNumber(value), number : (value,isBig=false) => toChineseNumber(value,isBig), // 转换为中文数字
// 货币 // 货币
currency: (value) => `${value}`, currency: (value,unit="¥",division=4,precision=2) => toCurrency(value,{division,unit,precision}),
}, },
en: { en: { }
currency: (value) => {
return `$${value}`
}
}
} }

View File

@ -226,20 +226,20 @@ function getDataTypeDefaultFormatter(scope,activeLanguage,dataType){
const targets = [scope.formatters,scope.global.formatters] const targets = [scope.formatters,scope.global.formatters]
for(const target of targets){ for(const target of targets){
if(!target) continue if(!target) continue
// 1. 优先在当前语言的$types中查找 // 1. 在全局$types中查找
if(("*" in target) && isPlainObject(target["*"].$types)){
let formatters = target["*"].$types
if(dataType in formatters && typeof(formatters[dataType])==="function"){
return scope.$cache.typedFormatters[dataType] = formatters[dataType]
}
}
// 2. 当前语言的$types中查找
if((activeLanguage in target) && isPlainObject(target[activeLanguage].$types)){ if((activeLanguage in target) && isPlainObject(target[activeLanguage].$types)){
let formatters = target[activeLanguage].$types let formatters = target[activeLanguage].$types
if(dataType in formatters && typeof(formatters[dataType])==="function"){ if(dataType in formatters && typeof(formatters[dataType])==="function"){
return scope.$cache.typedFormatters[dataType] = formatters[dataType] return scope.$cache.typedFormatters[dataType] = formatters[dataType]
} }
} }
// 2. 在全局$types中查找
if(("*" in target) && isPlainObject(target["*"].$types)){
let formatters = target["*"].$types
if(dataType in formatters && typeof(formatters[dataType])==="function"){
return scope.$cache.typedFormatters[dataType] = formatters[dataType]
}
}
} }
} }
@ -251,6 +251,7 @@ function getDataTypeDefaultFormatter(scope,activeLanguage,dataType){
* *
* - 在全局作用域中查找 * - 在全局作用域中查找
* *
* 全局作用域的格式化器优先
* *
* @param {*} scope * @param {*} scope
* @param {*} activeLanguage * @param {*} activeLanguage
@ -266,7 +267,7 @@ function getFormatter(scope,activeLanguage,name){
resetScopeCache(scope,activeLanguage) resetScopeCache(scope,activeLanguage)
} }
// 先在当前作用域中查找,再在全局查找 // 先在当前作用域中查找,再在全局查找
const targets = [scope.formatters,scope.global.formatters] const targets = [scope.global.formatters,scope.formatters]
for(const target of targets){ for(const target of targets){
// 1. 优先在当前语言查找 // 1. 优先在当前语言查找
if(activeLanguage in target){ if(activeLanguage in target){

View File

@ -22,7 +22,7 @@
* @returns * @returns
*/ */
function isNumber(value){ function isNumber(value){
if(!value) return false if(value==undefined) return false
if(typeof(value)=='number') return true if(typeof(value)=='number') return true
if(typeof(value)!='string') return false if(typeof(value)!='string') return false
try{ try{