@thoughtspot/ts-chart-sdk 0.0.2-alpha.6 → 0.0.2-alpha.8

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 (32) hide show
  1. package/dist/ts-chart-sdk.d.ts +20 -1
  2. package/lib/index.d.ts +1 -0
  3. package/lib/index.d.ts.map +1 -1
  4. package/lib/index.js +1 -0
  5. package/lib/index.js.map +1 -1
  6. package/lib/main/custom-chart-context.spec.js +53 -2
  7. package/lib/main/custom-chart-context.spec.js.map +1 -1
  8. package/lib/react/use-custom-chart-context.spec.js +54 -2
  9. package/lib/react/use-custom-chart-context.spec.js.map +1 -1
  10. package/lib/types/answer-column.types.d.ts +1 -0
  11. package/lib/types/answer-column.types.d.ts.map +1 -1
  12. package/lib/types/common.types.d.ts +1 -1
  13. package/lib/types/common.types.d.ts.map +1 -1
  14. package/lib/utils/conditional-formatting/conditional-formatting.spec.js +20 -1
  15. package/lib/utils/conditional-formatting/conditional-formatting.spec.js.map +1 -1
  16. package/lib/utils/date-formatting.d.ts +19 -0
  17. package/lib/utils/date-formatting.d.ts.map +1 -0
  18. package/lib/utils/date-formatting.js +56 -0
  19. package/lib/utils/date-formatting.js.map +1 -0
  20. package/lib/utils/date-formatting.spec.d.ts +2 -0
  21. package/lib/utils/date-formatting.spec.d.ts.map +1 -0
  22. package/lib/utils/date-formatting.spec.js +123 -0
  23. package/lib/utils/date-formatting.spec.js.map +1 -0
  24. package/package.json +3 -1
  25. package/src/index.ts +1 -0
  26. package/src/main/custom-chart-context.spec.ts +64 -4
  27. package/src/react/use-custom-chart-context.spec.tsx +65 -2
  28. package/src/types/answer-column.types.ts +7 -0
  29. package/src/types/common.types.ts +9 -3
  30. package/src/utils/conditional-formatting/conditional-formatting.spec.ts +52 -0
  31. package/src/utils/date-formatting.spec.ts +166 -0
  32. package/src/utils/date-formatting.ts +109 -0
@@ -0,0 +1,109 @@
1
+ /**
2
+ * @file: Date Formatting Utils
3
+ *
4
+ * @author Yashvardhan Nehra <yashvardhan.nehra@thoughtspot.com>
5
+ *
6
+ * Copyright: ThoughtSpot Inc. 2023
7
+ */
8
+
9
+ import _ from 'lodash';
10
+ import { DateTime } from 'luxon';
11
+ import {
12
+ ChartColumn,
13
+ ColumnTimeBucket,
14
+ ColumnType,
15
+ DataType,
16
+ } from '../types/answer-column.types';
17
+
18
+ export interface CustomCalendarDate {
19
+ v: {
20
+ s: number;
21
+ e: number;
22
+ };
23
+ d: string;
24
+ }
25
+
26
+ export const isDateColumn = (col: ChartColumn) =>
27
+ DataType[col.dataType] === 'DATE' || DataType[col.dataType] === 'DATE_TIME';
28
+
29
+ export const isAttribute = (col: ChartColumn) =>
30
+ col.type === ColumnType.ATTRIBUTE;
31
+
32
+ export const getCustomCalendarGuidFromColumn = (col: ChartColumn) =>
33
+ col.calenderGuid;
34
+
35
+ const isDateNumTimeBucket = (col: ChartColumn): boolean => {
36
+ return [
37
+ ColumnTimeBucket.HOUR_OF_DAY,
38
+ ColumnTimeBucket.DAY_OF_WEEK,
39
+ ColumnTimeBucket.DAY_OF_MONTH,
40
+ ColumnTimeBucket.DAY_OF_QUARTER,
41
+ ColumnTimeBucket.DAY_OF_YEAR,
42
+ ColumnTimeBucket.WEEK_OF_MONTH,
43
+ ColumnTimeBucket.WEEK_OF_QUARTER,
44
+ ColumnTimeBucket.WEEK_OF_YEAR,
45
+ ColumnTimeBucket.MONTH_OF_QUARTER,
46
+ ColumnTimeBucket.MONTH_OF_YEAR,
47
+ ColumnTimeBucket.QUARTER_OF_YEAR,
48
+ ].includes(col.timeBucket);
49
+ };
50
+ export const isDateNumColumn = (col: ChartColumn): boolean => {
51
+ return isAttribute(col) && isDateNumTimeBucket(col);
52
+ };
53
+
54
+ export const isDateFamilyColumn = (col: ChartColumn): boolean => {
55
+ return isDateColumn(col) || isDateNumColumn(col);
56
+ };
57
+
58
+ export const isTimeColumn = (col: ChartColumn) => {
59
+ return DataType[col.dataType] === 'TIME';
60
+ };
61
+
62
+ /**
63
+ * Determines if the given column has a custom calendar.
64
+ *
65
+ * @param col - The chart column to check.
66
+ * @returns True if the column is a date family column and has a custom calendar GUID, false otherwise.
67
+ */
68
+ export const hasCustomCalendar = (col: ChartColumn): boolean =>
69
+ isDateFamilyColumn(col) && !!getCustomCalendarGuidFromColumn(col);
70
+
71
+ /**
72
+ * Retrieves the start epoch from a custom calendar date.
73
+ *
74
+ * @param date - The custom calendar date object.
75
+ * @returns The start epoch time if present, null otherwise.
76
+ */
77
+ export function getStartEpoch(date: CustomCalendarDate): number | null {
78
+ if (_.has(date, 'v') && _.has(date.v, 's')) {
79
+ return date.v.s;
80
+ }
81
+ return null;
82
+ }
83
+
84
+ export function getDisplayString(date: CustomCalendarDate): string | null {
85
+ if (_.has(date, 'd')) {
86
+ return date.d;
87
+ }
88
+ return null;
89
+ }
90
+
91
+ /**
92
+ * Formats the date value based on the column's properties and custom calendar settings.
93
+ *
94
+ * @param dataValue - The date value to format.
95
+ * @param col - The chart column.
96
+ * @returns The formatted date string.
97
+ */
98
+ export function dateFormatter(dataValue: any, col: ChartColumn) {
99
+ if (hasCustomCalendar(col)) {
100
+ if (getDisplayString(dataValue)) {
101
+ return getDisplayString(dataValue);
102
+ }
103
+ const startEpoch = getStartEpoch(dataValue);
104
+ return startEpoch !== null
105
+ ? DateTime.fromMillis(startEpoch * 1000).toFormat('dd-MM-yyyy')
106
+ : null;
107
+ }
108
+ return DateTime.fromMillis(dataValue * 1000).toFormat('dd-MM-yyyy');
109
+ }