All files / transform numberToThousand.ts

80% Statements 8/10
71.43% Branches 5/7
100% Functions 3/3
87.5% Lines 7/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221x             1x 4x 4x       4x 4x       4x      
import * as R from 'ramda';
 
/**
 * 数值转化为 千分位 展示
 * @param num 
 * @param separator 分隔符,默认 ','
 */
export const numberToThousand = (num: number | string, separator = ','): number | string => {
  Iif (R.isEmpty(num) || R.isNil(num)) return num;
  Iif (isNaN(Number(num))) {
    return NaN;
  }
 
  return num.toString().replace(/\d+/, (n: string) =>
    n.replace(
      /(\d)(?=(\d{3})+$)/g,
      (
        $1: string,
      ) => `${$1}${separator}`,
    ),
  );
}