| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import Pinyin from './pinyin';
- import { ReportDate } from './ReportDate'
- const pinyin = new Pinyin();
- const isEmptyString = (str: string) => {
- let rst = false;
- if (str === null || str === undefined) {
- rst = true;
- } else if (typeof str) {
- let reg = /^\s*$/;
- rst = reg.test(str);
- }
- return rst;
- }
- const trim = (str: string) => {
- return str.replace(/(^\s*)|(\s*$)/g, "");
- }
- const leftTrim = (str: string) => {
- return str.replace(/(^\s*)/g, "");
- }
- const rightTrim = (str: string) => {
- return str.replace(/(\s*$)/g, "");
- }
- const replaceAll = (targetStr: string, FindText: string, RepText: string) => {
- let regExp = new RegExp(FindText, "gm");
- return targetStr.replace(regExp, RepText);
- }
- const comdify = (numStr: string) => {
- let re = /\d{1,3}(?=(\d{3})+$)/g;
- return numStr.replace(/^(\d+)((\.\d+)?)$/, function (s, s1, s2) { return s1.replace(re, "$&,") + s2; });
- }
- const convertToCaptionNum = (num: any, isCurrency: boolean, isTraditionalCap: boolean) => {
- let rst = "";
- if (/^\d*(\.\d*)?$/.test(num)) {
- let capChars, unitChars;
- if (isTraditionalCap) {
- capChars = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
- unitChars = ["", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"];
- } else {
- capChars = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
- unitChars = ["", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万"];
- }
- let numSplitArr = ("" + num).replace(/(^0*)/g, "").split(".");
- if (numSplitArr[0] === "") numSplitArr[0] = "0";
- let len = numSplitArr[0].length;
- let intPartArr = [];
- if (len <= 13) {
- for (let idx = 0; idx < len; idx++) {
- intPartArr.push(capChars[parseInt(numSplitArr[0].charAt(idx))] + unitChars[len - idx - 1]);
- }
- rst = intPartArr.join('');
- rst = replaceAll(rst, capChars[0] + unitChars[3], capChars[0]); //零千 -> 零
- rst = replaceAll(rst, capChars[0] + unitChars[2], capChars[0]); //零百 -> 零
- rst = replaceAll(rst, capChars[0] + unitChars[1], capChars[0]); //零十 -> 零
- //
- rst = replaceAll(replaceAll(rst, "零零", "零"), "零零", "零");
- rst = replaceAll(rst, capChars[0] + unitChars[8], unitChars[8]); //零亿 -> 亿
- rst = replaceAll(rst, capChars[0] + unitChars[4], unitChars[4]); //零万 -> 万
- //
- rst = replaceAll(rst, unitChars[8] + unitChars[4], unitChars[8] + capChars[0]); //亿万 -> 亿零
- if (num === 0) {
- rst = "零";
- } else if (rst.length > 1 && rst.charAt(rst.length - 1) === '零') {
- rst = rst.slice(0, rst.length - 1);
- }
- //小数部分处理
- if (numSplitArr.length > 1) {
- len = numSplitArr[1].length;
- if (parseInt(numSplitArr[1]) === 0) {
- rst = rst + (isCurrency ? "元整" : "");
- } else {
- if (isCurrency && len > 2) len = 2;
- let fractionStr = [];
- for (let idx = 0; idx < len; idx++) {
- fractionStr.push(capChars[parseInt(numSplitArr[1].charAt(idx))] + (isCurrency ? ((idx === 0) ? "角" : "分") : ""));
- }
- rst = rst + (isCurrency ? "元" : "点") + fractionStr.join("");
- }
- } else {
- rst = rst + (isCurrency ? "元整" : "");
- }
- } else {
- rst = "Number is too big!";
- }
- } else {
- rst = "Number is wrong!";
- }
- return rst;
- }
- const convertStrToBoolean = (str: string) => {
- let rst = false;
- if (!isEmptyString(str)) {
- let upperStr = str.toUpperCase();
- if (upperStr === 'T' || upperStr === 'Y' || upperStr === 'YES' || upperStr === 'TRUE') {
- rst = true;
- }
- }
- return rst;
- }
- const getPinYinFullChars=(value: string) =>{
- return pinyin.getFullChars(value);
- }
- const getPinYinCamelChars=(value: string)=> {
- return pinyin.getCamelChars(value);
- }
- const formatNumber = (formatStr: string, val: any) => {
- let rst = val;
- if (formatStr) {
- if (!(isNaN(parseFloat(val)))) {
- let dotIdx = formatStr.indexOf(".");
- if (dotIdx >= 0) {
- let tmpStr = parseFloat(val).toFixed(formatStr.length - dotIdx - 1);
- let digStr = formatStr.substr(dotIdx + 1, formatStr.length - dotIdx);
- for (let sIdx = digStr.length - 1; sIdx >= 0; sIdx--) {
- if (digStr[sIdx] === '#') {
- if (tmpStr.length > 0 && tmpStr[tmpStr.length - 1] === '0') {
- tmpStr = tmpStr.substr(0, tmpStr.length - 1);
- } else {
- break;
- }
- } else {
- break;
- }
- }
- if (tmpStr[tmpStr.length - 1] === '.') tmpStr = tmpStr.substr(0, tmpStr.length - 1);
- rst = tmpStr;
- } else {
- rst = parseFloat(val).toFixed(0);
- }
- let commaIdx = formatStr.indexOf(",");
- if (commaIdx >= 0) {
- rst = comdify(val.toString());
- }
- }
- }
- return rst;
- }
- const setupDateFormat=()=> {
- // 原本是直接向Date赋值的,但是现在ts不允许这么做,因此定义一个继承了Date的类使用
- const newReportDate = new ReportDate();
- Object.assign(Date, newReportDate);
- }
- export {
- isEmptyString,
- trim,
- leftTrim,
- rightTrim,
- replaceAll,
- comdify,
- convertToCaptionNum,
- convertStrToBoolean,
- formatNumber,
- getPinYinFullChars,
- getPinYinCamelChars,
- setupDateFormat
- }
|