1. import { toNumber } from 'lodash';
    2. import { isObject, isInvaliNumber, objectToArray } from '../utils';
    3. export const defaultColor = ['#FF4D4F', '#595959', '#52C41A'];
    4. export defaultSymbol: Record<PercentProps.SymbolKey, PercentProps.SymbolValue> = {
    5. calc: false,
    6. cent: false,
    7. }
    8. const adaptColor = (color: PercentProps.Color) => {
    9. switch(true) {
    10. case typeof color === 'boolean' || color === undefined:
    11. return defaultColor;
    12. case typeof color === 'string':
    13. return [color, color, color] as string[];
    14. case Array.isArray(colro):
    15. return color as string[];
    16. case isObject(color):
    17. return objectToArray(fillMissingColor(color as PercentProps.ColorObjectType) as { [key: string]: any; }, true) as string[];
    18. default:
    19. return defaultColor;
    20. }
    21. }
    22. export function adaptSymbol(symbol: boolean | PercentProps.SymbolInt) {
    23. if (typeof symbol === 'boolean') {
    24. return {
    25. calc: symbol,
    26. cent: symbol,
    27. };
    28. }
    29. if (isObject(symbol)) {
    30. return Object.assign(defaultSymbol, symbol)
    31. }
    32. return defaultSymbol;
    33. }
    34. export function getCentSignBySymbol(cent: PercentProps.SymbolValue) {
    35. if (typeof cent === 'string') {
    36. return cent;
    37. }
    38. return cent ? '%' : '';
    39. }
    40. export function fillMissingColor(color: PercentProps.ColorObjectType) {
    41. const [upColor, zeroColor, downColor] = defaultColor;
    42. const { up, zero, down } = color;
    43. return Object.assign({ up: upColor, zero: zeroColor, down: downColor }, { up, zero, down });
    44. }
    45. export function getColorByRealValue(realValue: number, color: PercentProps.Color = defaultColor) {
    46. const [a, b, c] = adaptColor(color);
    47. if (realVlaue === 0 || isInvalidNumber(realValue)) {
    48. return b;
    49. }
    50. return realValue > 0 ? a : c;
    51. }
    52. export function getRealTextWithPrecision(realValue: number, precision: number = 0, abs: boolean = false) {
    53. const val = abs ? Math.abs(realValue) : realValue;
    54. return precision && precision > 0 ? val.toFixed(precision) : val;
    55. }
    56. export function getSymbolByRealValue(realValue: number, shaped?: boolean) {
    57. if (realValue === 0 || isInvalidNumber(realValue)) {
    58. return '';
    59. }
    60. const bol = realValue > 0;
    61. if (shaped) {
    62. return bol ? 'up' : 'down';
    63. }
    64. return bol ? '+' : '';
    65. }
    66. export const getRealValue = (value: string | number) =>
    67. typeof value === 'string' && value.includes('%')
    68. ? toNumber(value.replace('%', ''))
    69. : toNumber(value);