export function customFormatter()

in src/common/common.ts [592:660]


export function customFormatter(config: customFormatterConfig) {
  const {
    unit,
    decimal = 1,
    grouping = true,
    needUnitTransform,
    unitTransformTo,
    valueType,
    hideZeroUnit = false,
    customCarryUnits = [],
    customCarryThreshold,
    addonTextAfter = ''
  } = config;

  if (!unit && (decimal === undefined || decimal === null) && !grouping && !needUnitTransform) {
    return null;
  }
  return function (v: any) {
    // 柱状图极端情况特殊处理
    if (typeof v === 'string' && v.startsWith('widgets-pad-')) {
      return '';
    }

    let result = v;
    let newUnit = unit || '';

    if (isInvalidNumber(v)) {
      return `${v}${newUnit}`;
    }

    if (needUnitTransform && (unit || valueType)) {
      if (valueType === 'percent_1') {
        result = result * 100;
      }
      const { value, unit: transformUnit } = unitConversion(
        result,
        unit,
        decimal,
        unitTransformTo,
        valueType,
        customCarryUnits,
        customCarryThreshold,
        addonTextAfter
      );

      result = value;
      newUnit = transformUnit;

      // count计数时不显示单位
      if (valueType === 'count' && newUnit === 'counts') {
        newUnit = '';
      }
    }

    // 小数位
    result = numberDecimal(result, decimal);

    // 千分位
    if (grouping || valueType === 'money' || valueType === 'count') {
      result = beautifyNumber(result, typeof grouping === 'boolean' ? ',' : grouping);
    }

    if (hideZeroUnit && Number(result) === 0) {
      newUnit = '';
    }

    return valueType === 'money' ? `${newUnit}${result}` : `${result}${newUnit}`;
  };
}