@quillsql/react 2.8.7 → 2.8.9

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 (59) hide show
  1. package/dist/BarList.js +1 -1
  2. package/dist/Chart.d.ts +0 -5
  3. package/dist/Chart.d.ts.map +1 -1
  4. package/dist/Chart.js +19 -168
  5. package/dist/ChartBuilder.d.ts +1 -0
  6. package/dist/ChartBuilder.d.ts.map +1 -1
  7. package/dist/ChartBuilder.js +18 -6
  8. package/dist/ChartEditor.d.ts +2 -1
  9. package/dist/ChartEditor.d.ts.map +1 -1
  10. package/dist/ChartEditor.js +2 -2
  11. package/dist/Dashboard.d.ts.map +1 -1
  12. package/dist/Dashboard.js +7 -6
  13. package/dist/Dashboard.js.map +1 -1
  14. package/dist/DateRangePicker/DateRangePicker.d.ts.map +1 -1
  15. package/dist/DateRangePicker/DateRangePicker.js +1 -1
  16. package/dist/DateRangePicker/dateRangePickerUtils.js.map +1 -1
  17. package/dist/SQLEditor.d.ts +5 -1
  18. package/dist/SQLEditor.d.ts.map +1 -1
  19. package/dist/SQLEditor.js +34 -32
  20. package/dist/Table.d.ts.map +1 -1
  21. package/dist/Table.js +7 -4
  22. package/dist/Table.js.map +1 -1
  23. package/dist/TableChart.js +1 -1
  24. package/dist/assets/ArrowDownHeadIcon.js +2 -2
  25. package/dist/components/Banner/index.d.ts +3 -0
  26. package/dist/components/Banner/index.d.ts.map +1 -0
  27. package/dist/components/Banner/index.js +24 -0
  28. package/dist/components/ReportBuilder/ColumnSelector.d.ts +19 -0
  29. package/dist/components/ReportBuilder/ColumnSelector.d.ts.map +1 -0
  30. package/dist/components/ReportBuilder/ColumnSelector.js +87 -0
  31. package/dist/components/SqlTextEditor.d.ts.map +1 -1
  32. package/dist/components/SqlTextEditor.js +27 -3
  33. package/dist/components/SqlTextEditor.js.map +1 -0
  34. package/dist/components/UiComponents.d.ts.map +1 -1
  35. package/dist/components/UiComponents.js +7 -6
  36. package/dist/hooks/useDashboard.js.map +1 -0
  37. package/dist/index.js.map +1 -1
  38. package/dist/internals/ReportBuilder/PivotModal.d.ts +2 -2
  39. package/dist/internals/ReportBuilder/PivotModal.d.ts.map +1 -1
  40. package/dist/internals/ReportBuilder/PivotModal.js +40 -35
  41. package/dist/test-data/tables.d.ts +2 -0
  42. package/dist/test-data/tables.d.ts.map +1 -0
  43. package/dist/test-data/tables.js +621 -0
  44. package/dist/utils/axisFormatter.d.ts +20 -0
  45. package/dist/utils/axisFormatter.d.ts.map +1 -0
  46. package/dist/utils/axisFormatter.js +185 -0
  47. package/dist/utils/schema.d.ts +22 -0
  48. package/dist/utils/schema.d.ts.map +1 -0
  49. package/dist/utils/schema.js +134 -0
  50. package/dist/utils/textFormatting.d.ts +4 -0
  51. package/dist/utils/textFormatting.d.ts.map +1 -0
  52. package/dist/utils/textFormatting.js +38 -0
  53. package/dist/utils/valueFormatter.d.ts +17 -0
  54. package/dist/utils/valueFormatter.d.ts.map +1 -0
  55. package/dist/utils/valueFormatter.js +166 -0
  56. package/dist/utils/valueFormatterCSV.d.ts +17 -0
  57. package/dist/utils/valueFormatterCSV.d.ts.map +1 -0
  58. package/dist/utils/valueFormatterCSV.js +90 -0
  59. package/package.json +1 -1
@@ -0,0 +1,185 @@
1
+ import { endOfWeek, format, getWeek, isValid, startOfWeek } from 'date-fns';
2
+ import { utcToZonedTime } from 'date-fns-tz';
3
+ /**
4
+ * Pretty-prints the given values to their nice-looking string form.
5
+ *
6
+ * The axis formatter will print a shorter version of the valueFormatter, which
7
+ * fits nicely along the X or Y axes.
8
+ */
9
+ export const axisFormatter = ({ value, field, fields, }) => {
10
+ if (field === undefined || field === null)
11
+ return '';
12
+ if (value === undefined || value === null)
13
+ return '';
14
+ const fieldInfo = fields.find((f) => f.field === field);
15
+ if (!fieldInfo)
16
+ return '';
17
+ const HANDLERS = {
18
+ percent: formatPercent,
19
+ dollar_amount: formatDollarAmount,
20
+ dollar_cents: formatDollarCents,
21
+ whole_number: formatWholeNumber,
22
+ one_decimal_place: formatOneDecimalPlace,
23
+ two_decimal_places: formatTwoDecimalPlaces,
24
+ string: formatString,
25
+ yyyy: format_YYYY,
26
+ MMM_yyyy: format_MMM_yyyy,
27
+ MMM_dd_yyyy: format_MMM_dd_yyyy,
28
+ hh_ap_pm: format_hh_ap_pm,
29
+ 'MMM_dd-MMM_dd': format_MMM_dd_MMM_dd,
30
+ 'MMM_dd_hh:mm_ap_pm': format_MMM_dd_hh_mm_ap_pm,
31
+ 'wo, yyyy': format_wo_yyyy,
32
+ };
33
+ // try to handle this value using the associated formatType handler
34
+ const formatType = fieldInfo.format;
35
+ if (Object.keys(HANDLERS).includes(formatType)) {
36
+ return HANDLERS[formatType](value);
37
+ }
38
+ return value.toString(); // by default make this value a string
39
+ };
40
+ /**
41
+ * HELPER FUNCTION DEFINITIONS
42
+ **/
43
+ /**
44
+ * Formats the value as a string using the built-in converstion.
45
+ */
46
+ const formatString = (value) => value.toString();
47
+ /**
48
+ * Formats the value as a (rounded) currency amount in dollars.
49
+ *
50
+ * eg. @example format(1234.567) => "$1.2K"
51
+ */
52
+ const formatDollarAmount = (value) => {
53
+ const formatter = new Intl.NumberFormat('en-US', {
54
+ style: 'currency',
55
+ currency: 'USD',
56
+ minimumSignificantDigits: 2,
57
+ maximumSignificantDigits: 2,
58
+ notation: 'compact',
59
+ compactDisplay: 'short',
60
+ });
61
+ return formatter.format(Number(value ?? 0));
62
+ };
63
+ /**
64
+ * Formats the value as a (rounded) currency amount in dollars and cents.
65
+ *
66
+ * @example format(1234.567) => "$1.2K"
67
+ */
68
+ const formatDollarCents = (value) => {
69
+ const formatter = new Intl.NumberFormat('en-US', {
70
+ style: 'currency',
71
+ currency: 'USD',
72
+ minimumSignificantDigits: 2,
73
+ maximumSignificantDigits: 2,
74
+ notation: 'compact',
75
+ compactDisplay: 'short',
76
+ });
77
+ return formatter.format(Number(value ?? 0));
78
+ };
79
+ /**
80
+ * Formats the value as a (rounded) integer.
81
+ *
82
+ * @example format(1234.567) => "1.2K"
83
+ */
84
+ const formatWholeNumber = (value) => {
85
+ const formatter = new Intl.NumberFormat('en-US', {
86
+ minimumSignificantDigits: 2,
87
+ maximumSignificantDigits: 2,
88
+ notation: 'compact',
89
+ compactDisplay: 'short',
90
+ });
91
+ return formatter.format(Number(value));
92
+ };
93
+ /**
94
+ * Formats the value as a number rounded to exactly one decimal place.
95
+ *
96
+ * @example format(1234.567) => "1.2K"
97
+ * @example format(123.456) => "1,234.6"
98
+ */
99
+ const formatOneDecimalPlace = (value) => {
100
+ const formatter = new Intl.NumberFormat('en-US', {
101
+ minimumFractionDigits: 1,
102
+ maximumFractionDigits: 1,
103
+ notation: 'compact',
104
+ compactDisplay: 'short',
105
+ });
106
+ return formatter.format(Number(value));
107
+ };
108
+ /**
109
+ * Formats the value as a number rounded to exactly two decimal places.
110
+ *
111
+ * @example format(1234.567) => "1.23K"
112
+ * @example format(123.456) => "123.46"
113
+ */
114
+ const formatTwoDecimalPlaces = (value) => {
115
+ const formatter = new Intl.NumberFormat('en-US', {
116
+ minimumFractionDigits: 2,
117
+ maximumFractionDigits: 2,
118
+ notation: 'compact',
119
+ compactDisplay: 'short',
120
+ });
121
+ return formatter.format(Number(value));
122
+ };
123
+ /**
124
+ * Formats the value as a percentage.
125
+ *
126
+ * @example format(0.1234) => "12%"
127
+ * @example format(0.99) => "99%"
128
+ *
129
+ * NOTE: This function expects that the value is already a percentage value, and
130
+ * does not try to guess whether the percentage is represented as a whole number
131
+ * or a decimal.
132
+ *
133
+ * @example format(1234.567) => "120K%"
134
+ * @example format(99) => "9.9K%"
135
+ */
136
+ const formatPercent = (value) => {
137
+ const formatter = new Intl.NumberFormat('en-US', {
138
+ style: 'percent',
139
+ minimumSignificantDigits: 2,
140
+ maximumSignificantDigits: 2,
141
+ notation: 'compact',
142
+ compactDisplay: 'short',
143
+ });
144
+ return formatter.format(Number(value));
145
+ };
146
+ // Converts the value to a UTC date with the given format string. If the
147
+ // resulting date is invalid, it returns "Invalid date".
148
+ const _getUTCDateHelper = (value, fmt) => {
149
+ const utcDate = utcToZonedTime(new Date(value), 'UTC');
150
+ if (!isValid(utcDate))
151
+ return 'Invalid date';
152
+ return format(utcDate, fmt);
153
+ };
154
+ const format_YYYY = (value) => _getUTCDateHelper(value, 'yyyy');
155
+ const format_MMM_yyyy = (value) => _getUTCDateHelper(value, 'MMM yyyy');
156
+ const format_hh_ap_pm = (value) => _getUTCDateHelper(value, 'hh:mm aa');
157
+ const format_MMM_dd_yyyy = (value) => _getUTCDateHelper(value, 'dd MMM yyyy');
158
+ const format_MMM_dd_MMM_dd = (value) => {
159
+ const utcDate = utcToZonedTime(new Date(value), 'UTC');
160
+ if (!isValid(utcDate))
161
+ return 'Invalid date';
162
+ const monday = startOfWeek(utcDate, { weekStartsOn: 1 });
163
+ const sunday = endOfWeek(utcDate, { weekStartsOn: 1 });
164
+ // Check if start and end are in the same month
165
+ if (format(monday, 'MMM') === format(sunday, 'MMM')) {
166
+ return `${format(monday, 'MMM dd')} - ${format(sunday, 'dd')}`;
167
+ }
168
+ else {
169
+ return `${format(monday, 'MMM dd')} - ${format(sunday, 'MMM dd')}`;
170
+ }
171
+ };
172
+ const format_MMM_dd_hh_mm_ap_pm = (value) => {
173
+ const utcDate = utcToZonedTime(new Date(value), 'UTC');
174
+ if (!isValid(utcDate))
175
+ return 'Invalid date';
176
+ const formatStr = utcDate.getMinutes() === 0 ? 'MMM do h a' : 'MMM do h:mm a';
177
+ const res = format(utcDate, formatStr);
178
+ return res.slice(0, -2) + res.slice(-2).toLowerCase();
179
+ };
180
+ const format_wo_yyyy = (value) => {
181
+ const utcDate = utcToZonedTime(new Date(value), 'UTC');
182
+ if (!isValid(utcDate))
183
+ return 'Invalid date';
184
+ return `${getWeek(utcDate)},${utcDate.getFullYear()}`;
185
+ };
@@ -0,0 +1,22 @@
1
+ interface TableInfo {
2
+ displayName: string;
3
+ columns: {
4
+ name: string;
5
+ label: string;
6
+ fieldType: string;
7
+ }[];
8
+ }
9
+ export interface ColumnSelectorInfo {
10
+ name: string;
11
+ selected: boolean;
12
+ presentTables: string[];
13
+ selectable: boolean;
14
+ tableName: string;
15
+ displayName: string;
16
+ textFiltered: boolean;
17
+ fieldType: string;
18
+ }
19
+ export declare function formatColumnData(tableInfo: TableInfo[], selectedTable: any, customerFieldName: string): ColumnSelectorInfo[];
20
+ export declare function columnGrouping(tableInfo: TableInfo[]): any[];
21
+ export {};
22
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/utils/schema.ts"],"names":[],"mappings":"AAGA,UAAU,SAAS;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAC/D;AAED,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;IAClB,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,UAAU,EAAE,OAAO,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,OAAO,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,SAAS,EAAE,EACtB,aAAa,EAAE,GAAG,EAClB,iBAAiB,EAAE,MAAM,GACxB,kBAAkB,EAAE,CA0CtB;AAGD,wBAAgB,cAAc,CAAC,SAAS,EAAE,SAAS,EAAE,GAAG,GAAG,EAAE,CAmG5D"}
@@ -0,0 +1,134 @@
1
+ import { deSnakeOrCamelCase, singularize } from './textFormatting';
2
+ export function formatColumnData(tableInfo, selectedTable, customerFieldName) {
3
+ const initialState = [];
4
+ const formattedData = tableInfo.reduce((allColumnDetails, table) => {
5
+ let setSelected = false;
6
+ if (selectedTable.displayName === table.displayName) {
7
+ setSelected = true;
8
+ }
9
+ table.columns.forEach((column) => {
10
+ if (column.name === customerFieldName) {
11
+ return;
12
+ }
13
+ let formattedDisplayName = '';
14
+ console.log(column.name);
15
+ if (column.name.endsWith('_id')) {
16
+ formattedDisplayName = singularize(deSnakeOrCamelCase(column.name));
17
+ }
18
+ else {
19
+ formattedDisplayName =
20
+ singularize(deSnakeOrCamelCase(table.displayName)) +
21
+ ' ' +
22
+ deSnakeOrCamelCase(column.name);
23
+ }
24
+ const presentColumn = allColumnDetails.find((columnDetail) => columnDetail.displayName === formattedDisplayName);
25
+ if (presentColumn) {
26
+ presentColumn.presentTables.push(table.displayName);
27
+ }
28
+ else {
29
+ allColumnDetails.push({
30
+ name: column.name,
31
+ selected: setSelected,
32
+ presentTables: [table.displayName],
33
+ selectable: setSelected,
34
+ tableName: table.displayName,
35
+ displayName: formattedDisplayName,
36
+ textFiltered: true,
37
+ fieldType: column.fieldType,
38
+ });
39
+ }
40
+ });
41
+ return allColumnDetails;
42
+ }, initialState);
43
+ return formattedData;
44
+ }
45
+ // THIS FUNCTION IS A CURRENTLY NOT IN USE BUT IS A STRONG EXAMPLE OF AUTOMATED JOINS
46
+ export function columnGrouping(tableInfo) {
47
+ const tableMap = {};
48
+ tableInfo.forEach((table) => {
49
+ tableMap[table.displayName] = [];
50
+ });
51
+ tableInfo.forEach((table) => {
52
+ tableMap[table.displayName]?.push(table.displayName);
53
+ table.columns.forEach((column) => {
54
+ if (column.name.endsWith('_id')) {
55
+ const otherTable = column.name.replace('_id', '') + 's';
56
+ const possibleTable = column.name.replace('_id', '');
57
+ if (tableMap[table.displayName]) {
58
+ tableMap[table.displayName]?.push(otherTable);
59
+ tableMap[table.displayName]?.push(possibleTable);
60
+ tableMap[otherTable]?.push(table.displayName);
61
+ tableMap[possibleTable]?.push(table.displayName);
62
+ }
63
+ }
64
+ });
65
+ });
66
+ function dfs(tableName, visited, group) {
67
+ group.push(tableName);
68
+ visited[tableName] = true;
69
+ tableMap[tableName]?.forEach((table) => {
70
+ if (!visited[table]) {
71
+ dfs(table, visited, group);
72
+ }
73
+ });
74
+ }
75
+ // perform a depth first search to create a grouping of tables that can be selected all together
76
+ const visited = {};
77
+ const groups = [];
78
+ const tableNames = Object.keys(tableMap);
79
+ let i = 0;
80
+ tableNames.forEach((table) => {
81
+ if (!visited[table]) {
82
+ groups.push([]);
83
+ dfs(table, visited, groups[i]);
84
+ i++;
85
+ }
86
+ });
87
+ const formattedGroups = groups.map((tableGroup) => {
88
+ // loop through all the columns in each table and add them to columnsFormatted
89
+ // if the coulmn name isn't a foreign key then format the name to be more readable. example User.id -> User Id
90
+ // if the column is a foreign key then format the name to be like the following. example user_id -> User Id
91
+ const formattedColumns = tableGroup.map((table) => {
92
+ const curTableInfo = tableInfo.find((tableInfo) => tableInfo.displayName === table);
93
+ if (curTableInfo) {
94
+ const columns = curTableInfo.columns.map((column) => {
95
+ if (column.name.endsWith('_id')) {
96
+ return {
97
+ displayName: singularize(deSnakeOrCamelCase(column.name)),
98
+ tableName: curTableInfo.displayName,
99
+ name: column.name,
100
+ };
101
+ }
102
+ return {
103
+ displayName: singularize(deSnakeOrCamelCase(table)) +
104
+ ' ' +
105
+ deSnakeOrCamelCase(column.name),
106
+ tableName: curTableInfo.displayName,
107
+ name: column.name,
108
+ };
109
+ });
110
+ return columns;
111
+ }
112
+ return [];
113
+ });
114
+ // if there are multiple objects with the same name then flatten the array and remove duplicates by name
115
+ const formattedColumnsFlat = formattedColumns.flat().filter((column, i) => {
116
+ return (formattedColumns
117
+ .flat()
118
+ .findIndex((c) => c.displayName === column.displayName) === i);
119
+ });
120
+ const formattedColumnsFlatNames = formattedColumnsFlat.map((column) => {
121
+ return {
122
+ displayName: column.displayName,
123
+ selected: false,
124
+ group: tableGroup.join('_'),
125
+ selectable: true,
126
+ tableName: column.tableName,
127
+ name: column.name,
128
+ textFiltered: true,
129
+ };
130
+ });
131
+ return formattedColumnsFlatNames;
132
+ });
133
+ return formattedGroups.flat();
134
+ }
@@ -0,0 +1,4 @@
1
+ export declare function capitalize(str: string): string;
2
+ export declare function singularize(str: string): string;
3
+ export declare function deSnakeOrCamelCase(value: string): string;
4
+ //# sourceMappingURL=textFormatting.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"textFormatting.d.ts","sourceRoot":"","sources":["../../src/utils/textFormatting.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE9C;AAGD,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAO/C;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAsBxD"}
@@ -0,0 +1,38 @@
1
+ export function capitalize(str) {
2
+ return str.charAt(0).toUpperCase() + str.slice(1);
3
+ }
4
+ // create a function that removes pluralization
5
+ export function singularize(str) {
6
+ if (str.endsWith('ies')) {
7
+ return str.slice(0, str.length - 3) + 'y';
8
+ }
9
+ else if (str.endsWith('s')) {
10
+ return str.slice(0, str.length - 1);
11
+ }
12
+ return str;
13
+ }
14
+ export function deSnakeOrCamelCase(value) {
15
+ if (value.toLowerCase() === 'id') {
16
+ return 'ID';
17
+ }
18
+ let splitVals = [];
19
+ if (/_/.test(value)) {
20
+ splitVals = value.split('_');
21
+ }
22
+ else if (/[a-z][A-Z]/.test(value)) {
23
+ splitVals = value.split(/(?=[A-Z])/);
24
+ }
25
+ if (splitVals.length > 0) {
26
+ splitVals.forEach((val, index) => {
27
+ console.log(val);
28
+ if (val.toLowerCase() === 'id') {
29
+ splitVals[index] = 'ID';
30
+ }
31
+ else {
32
+ splitVals[index] = capitalize(val);
33
+ }
34
+ });
35
+ return splitVals.join(' ');
36
+ }
37
+ return capitalize(value);
38
+ }
@@ -0,0 +1,17 @@
1
+ type Field = {
2
+ _id: string;
3
+ label: string;
4
+ field: string;
5
+ format: 'percent';
6
+ };
7
+ type Props = {
8
+ value: string | number | boolean | undefined | null;
9
+ field: string | undefined | null;
10
+ fields: Field[];
11
+ };
12
+ /**
13
+ * Pretty-prints the given values to their nice-looking string form.
14
+ */
15
+ export declare const valueFormatter: ({ value, field, fields, }: Props) => string | number | boolean;
16
+ export {};
17
+ //# sourceMappingURL=valueFormatter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valueFormatter.d.ts","sourceRoot":"","sources":["../../src/utils/valueFormatter.ts"],"names":[],"mappings":"AAGA,KAAK,KAAK,GAAG;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,SAAS,CAAC;CACnB,CAAC;AAEF,KAAK,KAAK,GAAG;IACX,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;IACpD,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;IACjC,MAAM,EAAE,KAAK,EAAE,CAAC;CACjB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,8BAIxB,KAAK,KAAG,MAAM,GAAG,MAAM,GAAG,OA+B5B,CAAC"}
@@ -0,0 +1,166 @@
1
+ import { endOfWeek, format, getWeek, isValid, startOfWeek } from 'date-fns';
2
+ import { utcToZonedTime } from 'date-fns-tz';
3
+ /**
4
+ * Pretty-prints the given values to their nice-looking string form.
5
+ */
6
+ export const valueFormatter = ({ value, field, fields, }) => {
7
+ if (field === undefined || field === null)
8
+ return '';
9
+ if (value === undefined || value === null)
10
+ return '';
11
+ const fieldInfo = fields.find((f) => f.field === field);
12
+ if (!fieldInfo)
13
+ return '';
14
+ const HANDLERS = {
15
+ percent: formatPercent,
16
+ dollar_amount: formatDollarAmount,
17
+ dollar_cents: formatDollarCents,
18
+ whole_number: formatWholeNumber,
19
+ one_decimal_place: formatOneDecimalPlace,
20
+ two_decimal_places: formatTwoDecimalPlaces,
21
+ string: formatString,
22
+ yyyy: format_YYYY,
23
+ MMM_yyyy: format_MMM_yyyy,
24
+ MMM_dd_yyyy: format_MMM_dd_yyyy,
25
+ hh_ap_pm: format_hh_ap_pm,
26
+ 'MMM_dd-MMM_dd': format_MMM_dd_MMM_dd,
27
+ 'MMM_dd_hh:mm_ap_pm': format_MMM_dd_hh_mm_ap_pm,
28
+ 'wo, yyyy': format_wo_yyyy,
29
+ };
30
+ // try to handle this value using the associated formatType handler
31
+ const formatType = fieldInfo.format;
32
+ if (Object.keys(HANDLERS).includes(formatType)) {
33
+ return HANDLERS[formatType](value);
34
+ }
35
+ return value.toString(); // by default make this value a string
36
+ };
37
+ /**
38
+ * HELPER FUNCTION DEFINITIONS
39
+ **/
40
+ /**
41
+ * Formats the value as a string using the built-in converstion.
42
+ */
43
+ const formatString = (value) => value.toString();
44
+ /**
45
+ * Formats the value as a (rounded) currency amount in dollars.
46
+ *
47
+ * eg. @example format(1234.567) => "$1,235"
48
+ */
49
+ const formatDollarAmount = (value) => {
50
+ const formatter = new Intl.NumberFormat('en-US', {
51
+ style: 'currency',
52
+ currency: 'USD',
53
+ maximumFractionDigits: 0,
54
+ });
55
+ return formatter.format(Number(value ?? 0));
56
+ };
57
+ /**
58
+ * Formats the value as a (rounded) currency amount in dollars and cents.
59
+ *
60
+ * @example format(1234.567) => "$1,234.57"
61
+ */
62
+ const formatDollarCents = (value) => {
63
+ const formatter = new Intl.NumberFormat('en-US', {
64
+ style: 'currency',
65
+ currency: 'USD',
66
+ minimumFractionDigits: 2,
67
+ maximumFractionDigits: 2,
68
+ });
69
+ return formatter.format(Number(value ?? 0));
70
+ };
71
+ /**
72
+ * Formats the value as a (rounded) integer.
73
+ *
74
+ * @example format(1234.567) => "1,235"
75
+ */
76
+ const formatWholeNumber = (value) => {
77
+ const formatter = new Intl.NumberFormat('en-US', {
78
+ maximumFractionDigits: 0,
79
+ });
80
+ return formatter.format(Number(value));
81
+ };
82
+ /**
83
+ * Formats the value as a number rounded to exactly one decimal place.
84
+ *
85
+ * @example format(1234.567) => "1,234.6"
86
+ */
87
+ const formatOneDecimalPlace = (value) => {
88
+ const formatter = new Intl.NumberFormat('en-US', {
89
+ minimumFractionDigits: 1,
90
+ maximumFractionDigits: 1,
91
+ });
92
+ return formatter.format(Number(value));
93
+ };
94
+ /**
95
+ * Formats the value as a number rounded to exactly two decimal places.
96
+ *
97
+ * @example format(1234.567) => "1,234.57"
98
+ */
99
+ const formatTwoDecimalPlaces = (value) => {
100
+ const formatter = new Intl.NumberFormat('en-US', {
101
+ minimumFractionDigits: 2,
102
+ maximumFractionDigits: 2,
103
+ });
104
+ return formatter.format(Number(value));
105
+ };
106
+ /**
107
+ * Formats the value as a percentage.
108
+ *
109
+ * @example format(0.1234) => "12.34%"
110
+ * @example format(0.99) => "99.00%"
111
+ *
112
+ * NOTE: This function expects that the value is already a percentage value, and
113
+ * does not try to guess whether the percentage is represented as a whole number
114
+ * or a decimal.
115
+ *
116
+ * @example format(1234.567) => "123,456.70%"
117
+ * @example format(99) => "9,900.00%"
118
+ */
119
+ const formatPercent = (value) => {
120
+ const formatter = new Intl.NumberFormat('en-US', {
121
+ style: 'percent',
122
+ minimumFractionDigits: 2,
123
+ maximumFractionDigits: 2,
124
+ });
125
+ return formatter.format(Number(value));
126
+ };
127
+ // Converts the value to a UTC date with the given format string. If the
128
+ // resulting date is invalid, it returns "Invalid date".
129
+ const _getUTCDateHelper = (value, fmt) => {
130
+ const utcDate = utcToZonedTime(new Date(value), 'UTC');
131
+ if (!isValid(utcDate))
132
+ return 'Invalid date';
133
+ return format(utcDate, fmt);
134
+ };
135
+ const format_YYYY = (value) => _getUTCDateHelper(value, 'yyyy');
136
+ const format_MMM_yyyy = (value) => _getUTCDateHelper(value, 'MMM yyyy');
137
+ const format_hh_ap_pm = (value) => _getUTCDateHelper(value, 'hh:mm aa');
138
+ const format_MMM_dd_yyyy = (value) => _getUTCDateHelper(value, 'dd MMM yyyy');
139
+ const format_MMM_dd_MMM_dd = (value) => {
140
+ const utcDate = utcToZonedTime(new Date(value), 'UTC');
141
+ if (!isValid(utcDate))
142
+ return 'Invalid date';
143
+ const monday = startOfWeek(utcDate, { weekStartsOn: 1 });
144
+ const sunday = endOfWeek(utcDate, { weekStartsOn: 1 });
145
+ // Check if start and end are in the same month
146
+ if (format(monday, 'MMM') === format(sunday, 'MMM')) {
147
+ return `${format(monday, 'MMM dd')} - ${format(sunday, 'dd')}`;
148
+ }
149
+ else {
150
+ return `${format(monday, 'MMM dd')} - ${format(sunday, 'MMM dd')}`;
151
+ }
152
+ };
153
+ const format_MMM_dd_hh_mm_ap_pm = (value) => {
154
+ const utcDate = utcToZonedTime(new Date(value), 'UTC');
155
+ if (!isValid(utcDate))
156
+ return 'Invalid date';
157
+ const formatStr = utcDate.getMinutes() === 0 ? 'MMM do h a' : 'MMM do h:mm a';
158
+ const res = format(utcDate, formatStr);
159
+ return res.slice(0, -2) + res.slice(-2).toLowerCase();
160
+ };
161
+ const format_wo_yyyy = (value) => {
162
+ const utcDate = utcToZonedTime(new Date(value), 'UTC');
163
+ if (!isValid(utcDate))
164
+ return 'Invalid date';
165
+ return `${getWeek(utcDate)},${utcDate.getFullYear()}`;
166
+ };
@@ -0,0 +1,17 @@
1
+ type Field = {
2
+ _id: string;
3
+ label: string;
4
+ field: string;
5
+ format: 'percent';
6
+ };
7
+ type Props = {
8
+ value: string | number | boolean | undefined | null;
9
+ field: string | undefined | null;
10
+ fields: Field[];
11
+ };
12
+ /**
13
+ * Formats a value to CSV-format.
14
+ */
15
+ export declare const valueFormatterCSV: ({ value, field, fields, }: Props) => string | number | boolean;
16
+ export {};
17
+ //# sourceMappingURL=valueFormatterCSV.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valueFormatterCSV.d.ts","sourceRoot":"","sources":["../../src/utils/valueFormatterCSV.ts"],"names":[],"mappings":"AAGA,KAAK,KAAK,GAAG;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,SAAS,CAAC;CACnB,CAAC;AAEF,KAAK,KAAK,GAAG;IACX,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;IACpD,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;IACjC,MAAM,EAAE,KAAK,EAAE,CAAC;CACjB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,8BAI3B,KAAK,KAAG,MAAM,GAAG,MAAM,GAAG,OA+B5B,CAAC"}