index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {
  2. formatNumber,
  3. parseLocale,
  4. isDate,
  5. isPercent,
  6. isText,
  7. getLocale,
  8. addLocale,
  9. round,
  10. dec2frac,
  11. options,
  12. codeToLocale,
  13. color,
  14. parsePattern,
  15. parseCatch,
  16. dateToSerial,
  17. parseNumber,
  18. parseDate,
  19. parseTime,
  20. parseBool,
  21. parseValue,
  22. dateFromSerial,
  23. OptionsData,
  24. PatternType,
  25. } from './internal';
  26. export interface FormatterType {
  27. (value: number, opts?: OptionsData): void;
  28. color(value, ops?): string;
  29. isDate(): boolean;
  30. isText(): boolean;
  31. isPercent(): boolean;
  32. }
  33. const _cache: {
  34. [key: string]: PatternType
  35. } = {};
  36. function getFormatter(parseData: PatternType, initOpts: OptionsData = {}): FormatterType {
  37. const { pattern, partitions, locale } = parseData;
  38. const getRuntimeOptions = (opts) => {
  39. const runOpts = Object.assign({}, options(), initOpts, opts);
  40. if (locale) {
  41. runOpts.locale = locale;
  42. }
  43. return runOpts;
  44. };
  45. const formatter = (value, opts) => {
  46. const o = getRuntimeOptions(opts);
  47. return formatNumber(dateToSerial(value, o), partitions, o);
  48. };
  49. formatter.color = (value, opts = {}) => {
  50. const o = getRuntimeOptions(opts);
  51. return color(dateToSerial(value, o), partitions);
  52. };
  53. formatter.isPercent = () => isPercent(partitions);
  54. formatter.isDate = () => isDate(partitions);
  55. formatter.isText = () => isText(partitions);
  56. formatter.pattern = pattern;
  57. if (parseData.error) {
  58. formatter.error = parseData.error;
  59. }
  60. formatter.options = getRuntimeOptions;
  61. formatter.locale = locale || (initOpts && initOpts.locale) || '';
  62. return Object.freeze(formatter);
  63. }
  64. function numfmt(pattern: string, opts: OptionsData = {}): FormatterType {
  65. if (!pattern) {
  66. pattern = 'General';
  67. }
  68. let parseData = null;
  69. if (_cache[pattern]) {
  70. parseData = _cache[pattern];
  71. }
  72. else {
  73. const constructOpts = Object.assign({}, options(), opts);
  74. parseData = constructOpts.throws
  75. ? parsePattern(pattern)
  76. : parseCatch(pattern);
  77. if (!parseData.error) {
  78. _cache[pattern] = parseData;
  79. }
  80. }
  81. return getFormatter(parseData, opts);
  82. }
  83. numfmt.isDate = (d: string) => numfmt(d, { throws: false }).isDate();
  84. numfmt.isPercent = (d: string) => numfmt(d, { throws: false }).isPercent();
  85. numfmt.isText = (d: string) => numfmt(d, { throws: false }).isText();
  86. numfmt.dateToSerial = dateToSerial;
  87. numfmt.dateFromSerial = dateFromSerial;
  88. numfmt.options = options;
  89. numfmt.dec2frac = dec2frac;
  90. numfmt.round = round;
  91. numfmt.codeToLocale = codeToLocale;
  92. numfmt.getLocale = getLocale;
  93. numfmt.parseLocale = parseLocale;
  94. numfmt.addLocale = (options, l4e) => {
  95. const c = parseLocale(l4e);
  96. // when locale is changed, expire all cached patterns
  97. delete _cache[c.lang];
  98. delete _cache[c.language];
  99. return addLocale(options, c);
  100. };
  101. // SSF interface compatibility
  102. function format(pattern, value, l4e, noThrows = false) {
  103. const opts = (l4e && typeof l4e === 'object') ? l4e : { locale: l4e, throws: !noThrows };
  104. return numfmt(pattern, opts)(dateToSerial(value, opts), opts);
  105. }
  106. numfmt.format = format;
  107. numfmt.is_date = numfmt.isDate;
  108. numfmt.parseNumber = parseNumber;
  109. numfmt.parseDate = parseDate;
  110. numfmt.parseTime = parseTime;
  111. numfmt.parseBool = parseBool;
  112. numfmt.parseValue = parseValue;
  113. export {
  114. numfmt,
  115. };