numberToChinese
const fraction = ['角', '分']const defaultDigit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']// 遍历unit[1] 进位 unit[0]const unit = [['元', '万', '亿'],['', '拾', '佰', '仟'],]// 阿拉伯数值转换 大写 数值interface IUppercaseConfig {/**** @description default is '整'* */tail?: string/**** @description default is ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']* */digit?: string[]}export function numberToChinese(n: number | string, config?: IUppercaseConfig): string {const { digit = defaultDigit, tail = '整' } = config ?? {}try {// 超出最大解析限制if (String(n).length > 13) {return ''}const head = n < 0 ? '负' : ''n = Math.abs(n as number)let s = ''// 小数位转换for (let i = 0; i < fraction.length; i++) {s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '')}s = s || tailn = Math.floor(n)for (let i = 0; i < unit[0].length && n > 0; i++) {let p = ''for (let j = 0; j < unit[1].length && n > 0; j++) {p = digit[n % 10] + unit[1][j] + pn = Math.floor(n / 10)}s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s}return (head +s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整'))} catch (e) {return ''}}
对象深度比较
对象属性顺序可以不一致,只要属性类型及其值相同,则判定为该属性相同
function deepCompare(x, y) {var i, l, leftChain, rightChain;function compare2Objects(x, y) {var p;// remember that NaN === NaN returns false// and isNaN(undefined) returns trueif (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {return true;}// Compare primitives and functions.// Check if both arguments link to the same object.// Especially useful on the step where we compare prototypesif (x === y) {return true;}// Works in case when functions are created in constructor.// Comparing dates is a common scenario. Another built-ins?// We can even handle functions passed across iframesif ((typeof x === 'function' && typeof y === 'function') ||(x instanceof Date && y instanceof Date) ||(x instanceof RegExp && y instanceof RegExp) ||(x instanceof String && y instanceof String) ||(x instanceof Number && y instanceof Number)) {return x.toString() === y.toString();}// At last checking prototypes as good as we canif (!(x instanceof Object && y instanceof Object)) {return false;}if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {return false;}if (x.constructor !== y.constructor) {return false;}if (x.prototype !== y.prototype) {return false;}// Check for infinitive linking loopsif (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {return false;}// Quick checking of one object being a subset of another.// todo: cache the structure of arguments[0] for performancefor (p in y) {if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {return false;} else if (typeof y[p] !== typeof x[p]) {return false;}}for (p in x) {if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {return false;} else if (typeof y[p] !== typeof x[p]) {return false;}switch (typeof(x[p])) {case 'object':case 'function':leftChain.push(x);rightChain.push(y);if (!compare2Objects(x[p], y[p])) {return false;}leftChain.pop();rightChain.pop();break;default:if (x[p] !== y[p]) {return false;}break;}}return true;}if (arguments.length < 1) {return true; //Die silently? Don't know how to handle such case, please help...// throw "Need two or more arguments to compare";}for (i = 1, l = arguments.length; i < l; i++) {leftChain = []; //Todo: this can be cachedrightChain = [];if (!compare2Objects(arguments[0], arguments[i])) {return false;}}return true;}
扁平数据结构转tree
function arrayToTree(items) {const result = []; // 存放结果集const itemMap = {}; //for (const item of items) {const id = item.id;const pid = item.pid;if (!itemMap[id]) {itemMap[id] = {children: [],}}itemMap[id] = {...item,children: itemMap[id]['children']}const treeItem = itemMap[id];if (pid === 0) {result.push(treeItem);} else {if (!itemMap[pid]) {itemMap[pid] = {children: [],}}itemMap[pid].children.push(treeItem)}}return result;}
快排
const arr = [19, 9, 8, 7, 6, 5, 4, 3, 2, 1, -2];function quickSort(arr, low, high) {if (low < high) {const index = getIndex(arr, low, high);quickSort(arr, low, index - 1);quickSort(arr, index + 1, high);}}function getIndex(arr, low, high) {const temp = arr[low];while (low < high) {while (low < high && arr[high] >= temp) high--;arr[low] = arr[high];while (low < high && arr[low] <= temp) low++;arr[high] = arr[low];}arr[low] = temp;return low;}quickSort(arr, 0, arr.length - 1);console.log(arr);
