util.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import moment from 'moment'
  2. import { IFieldFormatConfig } from './types'
  3. import { NumericUnit, FieldFormatTypes } from './constants'
  4. export function getDefaultFieldFormatConfig (): IFieldFormatConfig {
  5. return {
  6. formatType: FieldFormatTypes.Default
  7. }
  8. }
  9. export function getFormattedValue (value: number | string, format: IFieldFormatConfig) {
  10. if (!format || format.formatType === FieldFormatTypes.Default) { return value }
  11. if (value === null || value === undefined) { return value }
  12. const { formatType } = format
  13. if (typeof value === 'string' && formatType !== FieldFormatTypes.Date && (!value || isNaN(+value))) { return value }
  14. const config = format[formatType]
  15. let formattedValue
  16. switch (formatType) {
  17. case FieldFormatTypes.Numeric:
  18. case FieldFormatTypes.Currency:
  19. const {
  20. decimalPlaces,
  21. unit,
  22. useThousandSeparator } = config as IFieldFormatConfig['numeric'] | IFieldFormatConfig['currency']
  23. formattedValue = formatByUnit(value, unit)
  24. formattedValue = formatByDecimalPlaces(formattedValue, decimalPlaces)
  25. formattedValue = formatByThousandSeperator(formattedValue, useThousandSeparator)
  26. if (unit !== NumericUnit.None) {
  27. formattedValue = `${formattedValue}${unit}`
  28. }
  29. if (formatType === FieldFormatTypes.Currency) {
  30. const { prefix, suffix } = config as IFieldFormatConfig['currency']
  31. formattedValue = [prefix, formattedValue, suffix].join('')
  32. }
  33. break
  34. case FieldFormatTypes.Percentage:
  35. formattedValue = (+value) * 100
  36. formattedValue = isNaN(formattedValue) ? value
  37. : `${formatByDecimalPlaces(formattedValue, (config as IFieldFormatConfig['percentage']).decimalPlaces)}%`
  38. break
  39. case FieldFormatTypes.ScientificNotation:
  40. formattedValue = (+value).toExponential((config as IFieldFormatConfig['scientificNotation']).decimalPlaces)
  41. formattedValue = isNaN(formattedValue) ? value : formattedValue
  42. break
  43. case FieldFormatTypes.Date:
  44. const { format } = config as IFieldFormatConfig['date']
  45. formattedValue = moment(value).format(format)
  46. break
  47. case FieldFormatTypes.Custom:
  48. // @TODO
  49. break
  50. default:
  51. formattedValue = value
  52. break
  53. }
  54. return formattedValue
  55. }
  56. function formatByDecimalPlaces (value, decimalPlaces: number) {
  57. if (isNaN(value)) { return value }
  58. if (decimalPlaces < 0 || decimalPlaces > 100) { return value }
  59. return (+value).toFixed(decimalPlaces)
  60. }
  61. function formatByThousandSeperator (value, useThousandSeparator: boolean) {
  62. if (isNaN(+value) || !useThousandSeparator) { return value }
  63. const parts = value.toString().split('.')
  64. parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  65. const formatted = parts.join('.')
  66. return formatted
  67. }
  68. function formatByUnit (value, unit: NumericUnit) {
  69. const numericValue = +value
  70. if (isNaN(numericValue)) { return value }
  71. let exponent = 0
  72. switch (unit) {
  73. case NumericUnit.TenThousand:
  74. exponent = 4
  75. break
  76. case NumericUnit.OneHundredMillion:
  77. exponent = 8
  78. break
  79. case NumericUnit.Thousand:
  80. exponent = 3
  81. break
  82. case NumericUnit.Million:
  83. exponent = 6
  84. break
  85. case NumericUnit.Giga:
  86. exponent = 9
  87. break
  88. }
  89. return numericValue / Math.pow(10, exponent)
  90. }