@thoughtspot/ts-chart-sdk 0.0.2-alpha.24 → 0.0.2-alpha.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/ts-chart-sdk.d.ts +63 -3
  2. package/lib/index.d.ts +2 -0
  3. package/lib/index.d.ts.map +1 -1
  4. package/lib/index.js +2 -0
  5. package/lib/index.js.map +1 -1
  6. package/lib/main/custom-chart-context.d.ts.map +1 -1
  7. package/lib/main/custom-chart-context.js +3 -1
  8. package/lib/main/custom-chart-context.js.map +1 -1
  9. package/lib/types/answer-column.types.d.ts +3 -3
  10. package/lib/types/answer-column.types.d.ts.map +1 -1
  11. package/lib/types/answer-column.types.js +3 -3
  12. package/lib/types/answer-column.types.js.map +1 -1
  13. package/lib/types/visual-prop.types.d.ts +40 -0
  14. package/lib/types/visual-prop.types.d.ts.map +1 -1
  15. package/lib/types/visual-prop.types.js +32 -1
  16. package/lib/types/visual-prop.types.js.map +1 -1
  17. package/lib/utils/globalize-Initializer/globalize-utils.d.ts +16 -0
  18. package/lib/utils/globalize-Initializer/globalize-utils.d.ts.map +1 -0
  19. package/lib/utils/globalize-Initializer/globalize-utils.js +101 -0
  20. package/lib/utils/globalize-Initializer/globalize-utils.js.map +1 -0
  21. package/lib/utils/globalize-Initializer/globalize-utils.spec.d.ts +2 -0
  22. package/lib/utils/globalize-Initializer/globalize-utils.spec.d.ts.map +1 -0
  23. package/lib/utils/globalize-Initializer/globalize-utils.spec.js +149 -0
  24. package/lib/utils/globalize-Initializer/globalize-utils.spec.js.map +1 -0
  25. package/lib/utils/number-formatting/number-formatting-utils.d.ts +20 -0
  26. package/lib/utils/number-formatting/number-formatting-utils.d.ts.map +1 -0
  27. package/lib/utils/number-formatting/number-formatting-utils.js +159 -0
  28. package/lib/utils/number-formatting/number-formatting-utils.js.map +1 -0
  29. package/lib/utils/number-formatting/number-formatting-utils.spec.d.ts +2 -0
  30. package/lib/utils/number-formatting/number-formatting-utils.spec.d.ts.map +1 -0
  31. package/lib/utils/number-formatting/number-formatting-utils.spec.js +221 -0
  32. package/lib/utils/number-formatting/number-formatting-utils.spec.js.map +1 -0
  33. package/lib/utils/number-formatting/number-formatting.d.ts +5 -0
  34. package/lib/utils/number-formatting/number-formatting.d.ts.map +1 -0
  35. package/lib/utils/number-formatting/number-formatting.js +128 -0
  36. package/lib/utils/number-formatting/number-formatting.js.map +1 -0
  37. package/lib/utils/number-formatting/number-formatting.spec.d.ts +2 -0
  38. package/lib/utils/number-formatting/number-formatting.spec.d.ts.map +1 -0
  39. package/lib/utils/number-formatting/number-formatting.spec.js +159 -0
  40. package/lib/utils/number-formatting/number-formatting.spec.js.map +1 -0
  41. package/package.json +4 -1
  42. package/src/index.ts +2 -0
  43. package/src/main/custom-chart-context.ts +4 -1
  44. package/src/types/answer-column.types.ts +3 -3
  45. package/src/types/visual-prop.types.ts +86 -0
  46. package/src/utils/globalize-Initializer/globalize-utils.spec.ts +192 -0
  47. package/src/utils/globalize-Initializer/globalize-utils.ts +216 -0
  48. package/src/utils/number-formatting/number-formatting-utils.spec.ts +296 -0
  49. package/src/utils/number-formatting/number-formatting-utils.ts +260 -0
  50. package/src/utils/number-formatting/number-formatting.spec.ts +243 -0
  51. package/src/utils/number-formatting/number-formatting.ts +264 -0
@@ -0,0 +1,264 @@
1
+ /**
2
+ * @file: Number Formatting Utils
3
+ *
4
+ * @author Yashvardhan Nehra <yashvardhan.nehra@thoughtspot.com>
5
+ *
6
+ * Copyright: ThoughtSpot Inc. 2024
7
+ */
8
+ import _ from 'lodash';
9
+ import { create } from '../../main/logger';
10
+ import {
11
+ ColumnFormat,
12
+ CurrencyFormat,
13
+ CurrencyFormatType,
14
+ } from '../../types/answer-column.types';
15
+ import {
16
+ CategoryType,
17
+ FormatConfig,
18
+ Unit,
19
+ } from '../../types/number-formatting.types';
20
+ import {
21
+ formatNumberSafely,
22
+ getDefaultCurrencyCode,
23
+ globalizeCurrencyFormatter,
24
+ globalizeNumberFormatter,
25
+ sanitizeFormat,
26
+ validateNumberFormat,
27
+ } from '../globalize-Initializer/globalize-utils';
28
+ import {
29
+ defaultFormatConfig,
30
+ formatNegativeValue,
31
+ formatSpecialDataValue,
32
+ getLocaleName,
33
+ mapFormatterConfig,
34
+ UNITS_TO_DIVIDING_FACTOR,
35
+ UNITS_TO_SUFFIX,
36
+ } from './number-formatting-utils';
37
+
38
+ const logger = create('number-formatting');
39
+
40
+ const CURRENCY_CODE_EXTRACTOR_REGEX = /[\d.,]+/g;
41
+
42
+ /**
43
+ * Formats a number with a custom pattern and combines it with a currency symbol.
44
+ *
45
+ * @param value - The numeric value to format.
46
+ * @param currencyCode - The ISO currency code (e.g., 'USD').
47
+ * @param formatPattern - The custom format pattern (e.g., '#,##0.00').
48
+ * @returns The formatted value with the currency symbol.
49
+ */
50
+ export const formatCurrencyWithCustomPattern = (
51
+ value: number,
52
+ currencyCode: string,
53
+ formatPattern: string,
54
+ ): string => {
55
+ // Sanitize the custom format pattern
56
+ const sanitizedPattern = sanitizeFormat(formatPattern);
57
+
58
+ // Create a custom number formatter
59
+ const customFormatter = globalizeNumberFormatter({
60
+ ...({ raw: sanitizedPattern } as any),
61
+ });
62
+
63
+ const formattedValue = customFormatter(value);
64
+
65
+ const currencyFormatter = globalizeCurrencyFormatter(currencyCode, {
66
+ style: 'symbol',
67
+ });
68
+
69
+ const currencySymbol = currencyFormatter(0).replace(
70
+ CURRENCY_CODE_EXTRACTOR_REGEX,
71
+ '',
72
+ ); // Extract the currency symbol
73
+
74
+ // Combine the custom formatted value with the currency symbol
75
+ return `${currencySymbol}${formattedValue}`;
76
+ };
77
+
78
+ /**
79
+ * Formats a value based on the provided format configuration and column settings.
80
+ *
81
+ * @param value - The value to format (can be a string or number).
82
+ * @param formatConfigProp - The format configuration for the value.
83
+ * @param columnFormatConfig - The column-specific formatting settings.
84
+ * @returns The formatted value as a string.
85
+ */
86
+ export const getFormattedValue = (
87
+ value: string | number,
88
+ formatConfigProp: FormatConfig,
89
+ columnFormatConfig: ColumnFormat,
90
+ ): string => {
91
+ let formatConfig = _.cloneDeep(formatConfigProp);
92
+
93
+ // Use default configuration if none is provided
94
+ if (_.isNil(formatConfig) || _.isEmpty(formatConfig)) {
95
+ formatConfig = defaultFormatConfig(columnFormatConfig);
96
+ }
97
+
98
+ // Handle special data values (e.g., NaN, Infinity)
99
+ const specialVal = formatSpecialDataValue(value);
100
+ if (specialVal) {
101
+ return specialVal;
102
+ }
103
+
104
+ // Convert value to a float
105
+ const floatValue = parseFloat(value.toString());
106
+ const absFloatValue = Math.abs(floatValue);
107
+
108
+ switch (formatConfig.category) {
109
+ case CategoryType.Number: {
110
+ const configDetails = formatConfig.numberFormatConfig || {};
111
+ const formatterConfigMap = mapFormatterConfig(
112
+ absFloatValue,
113
+ configDetails,
114
+ );
115
+ const compactValue =
116
+ absFloatValue /
117
+ UNITS_TO_DIVIDING_FACTOR[
118
+ formatterConfigMap.unitDetails as Unit
119
+ ];
120
+ const suffix = UNITS_TO_SUFFIX[formatterConfigMap.unitDetails];
121
+ const formattedValue = formatNumberSafely(
122
+ {
123
+ style: 'decimal',
124
+ maximumFractionDigits: formatterConfigMap.decimalDetails,
125
+ minimumFractionDigits:
126
+ formatterConfigMap.shouldRemoveTrailingZeros
127
+ ? 0
128
+ : formatterConfigMap.decimalDetails,
129
+ useGrouping: configDetails.toSeparateThousands || false,
130
+ },
131
+ compactValue,
132
+ );
133
+ const absFormattedValue = `${formattedValue}${suffix}`;
134
+
135
+ if (absFloatValue !== floatValue) {
136
+ return formatNegativeValue(
137
+ absFormattedValue,
138
+ configDetails.negativeValueFormat,
139
+ );
140
+ }
141
+ return absFormattedValue;
142
+ }
143
+ case CategoryType.Percentage: {
144
+ const configDetails = formatConfig.percentageFormatConfig;
145
+ const decimalDetails = configDetails?.decimals || 0;
146
+ return formatNumberSafely(
147
+ {
148
+ style: 'percent',
149
+ maximumFractionDigits: decimalDetails,
150
+ minimumFractionDigits: configDetails?.removeTrailingZeroes
151
+ ? 0
152
+ : decimalDetails,
153
+ },
154
+ floatValue,
155
+ );
156
+ }
157
+ case CategoryType.Currency: {
158
+ const configDetails = formatConfig.currencyFormatConfig || {};
159
+ const formatterConfigMap = mapFormatterConfig(
160
+ absFloatValue,
161
+ configDetails,
162
+ );
163
+ const compactValue =
164
+ floatValue /
165
+ UNITS_TO_DIVIDING_FACTOR[formatterConfigMap.unitDetails];
166
+
167
+ let locale = configDetails.locale || getDefaultCurrencyCode();
168
+ if (locale === CurrencyFormatType.USER_LOCALE) {
169
+ locale = getLocaleName({
170
+ type: locale,
171
+ } as CurrencyFormat);
172
+ }
173
+ try {
174
+ const formatter = globalizeCurrencyFormatter(locale, {
175
+ style: 'symbol',
176
+ maximumFractionDigits: formatterConfigMap.decimalDetails,
177
+ minimumFractionDigits:
178
+ formatterConfigMap.shouldRemoveTrailingZeros
179
+ ? 0
180
+ : formatterConfigMap.decimalDetails,
181
+ useGrouping: configDetails.toSeparateThousands || false,
182
+ });
183
+
184
+ const formattedValue = formatter(compactValue);
185
+ const currencyCode = formattedValue.replace(
186
+ CURRENCY_CODE_EXTRACTOR_REGEX,
187
+ '',
188
+ );
189
+ /**
190
+ * When we format the value in Millions, Billions etc,
191
+ * we have to handle the scenario of the unit suffix, currencycode and the formatted
192
+ * value We test the presence of numeric character with the help of regex and then
193
+ * we append the apt suffix and currencyCode to the formatted value
194
+ */
195
+ const suffix = !_.isNil(floatValue)
196
+ ? UNITS_TO_SUFFIX[formatterConfigMap.unitDetails]
197
+ : '';
198
+ if (/[0-9]$/.test(formattedValue.charAt(0))) {
199
+ return `${formattedValue.replace(
200
+ currencyCode,
201
+ '',
202
+ )}${suffix}${currencyCode}`;
203
+ }
204
+ return `${formattedValue}${suffix}`;
205
+ } catch (e) {
206
+ logger.error(
207
+ 'Corrupted format config passed, formatting using default config',
208
+ formatConfig,
209
+ e,
210
+ );
211
+ }
212
+ break;
213
+ }
214
+ case CategoryType.Custom: {
215
+ const formatPattern = formatConfig.customFormatConfig?.format;
216
+ const currencyCode = columnFormatConfig.currencyFormat?.isoCode;
217
+ if (
218
+ !_.isNil(formatPattern) &&
219
+ validateNumberFormat(sanitizeFormat(formatPattern))
220
+ ) {
221
+ const sanitizedPattern = sanitizeFormat(formatPattern);
222
+ if (!_.isNil(currencyCode)) {
223
+ /**
224
+ * Globalize does not consider format pattern while formatting the currency,
225
+ * only the currency specific rules are considered, so need to combine. That's
226
+ * what we do in this formatCurrency method
227
+ */
228
+ const formattedValueWithLocaleAndPattern =
229
+ formatCurrencyWithCustomPattern(
230
+ floatValue,
231
+ currencyCode,
232
+ formatPattern,
233
+ );
234
+ return `${formattedValueWithLocaleAndPattern}`;
235
+ }
236
+ return formatNumberSafely(
237
+ {
238
+ style: 'decimal',
239
+ raw: sanitizedPattern,
240
+ },
241
+ floatValue as any,
242
+ );
243
+ }
244
+ logger.error('Invalid custom format config passed:', formatConfig);
245
+ break;
246
+ }
247
+ default: {
248
+ return formatNumberSafely(
249
+ {
250
+ style: 'decimal',
251
+ },
252
+ floatValue as any,
253
+ );
254
+ }
255
+ }
256
+
257
+ // Default fallback: Decimal formatting
258
+ return formatNumberSafely(
259
+ {
260
+ style: 'decimal',
261
+ },
262
+ floatValue as any,
263
+ );
264
+ };