dce-reactkit 4.1.2 → 4.1.3

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.
@@ -2,7 +2,10 @@
2
2
  * Generate a CSV file
3
3
  * @author Gabe Abrams
4
4
  * @param data list of row data in the form of json objects
5
- * @param columns list of columns to include in the csv
5
+ * @param columns list of columns to include in the csv where each column is an object with:
6
+ * - title: the column title
7
+ * - param: the key in the data object to use for this column
8
+ * - textIfUndefined: optional text to use if the value is undefined (defaults to empty string)
6
9
  * @returns multiline csv string
7
10
  */
8
11
  declare const genCSV: (data: {
@@ -10,5 +13,6 @@ declare const genCSV: (data: {
10
13
  }[], columns: {
11
14
  title: string;
12
15
  param: string;
16
+ textIfUndefined?: string;
13
17
  }[]) => string;
14
18
  export default genCSV;
package/dist/index.d.ts CHANGED
@@ -1269,7 +1269,10 @@ declare const getMonthName: (month: number) => {
1269
1269
  * Generate a CSV file
1270
1270
  * @author Gabe Abrams
1271
1271
  * @param data list of row data in the form of json objects
1272
- * @param columns list of columns to include in the csv
1272
+ * @param columns list of columns to include in the csv where each column is an object with:
1273
+ * - title: the column title
1274
+ * - param: the key in the data object to use for this column
1275
+ * - textIfUndefined: optional text to use if the value is undefined (defaults to empty string)
1273
1276
  * @returns multiline csv string
1274
1277
  */
1275
1278
  declare const genCSV: (data: {
@@ -1277,6 +1280,7 @@ declare const genCSV: (data: {
1277
1280
  }[], columns: {
1278
1281
  title: string;
1279
1282
  param: string;
1283
+ textIfUndefined?: string;
1280
1284
  }[]) => string;
1281
1285
 
1282
1286
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dce-reactkit",
3
- "version": "4.1.2",
3
+ "version": "4.1.3",
4
4
  "description": "Shared components for Harvard DCE apps",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -23,7 +23,7 @@ describe('genCSV', () => {
23
23
  { name: 'Bob', age: 30, email: 'bob@example.com' },
24
24
  { name: 'Charlie', age: 35, email: 'charlie@example.com' },
25
25
  ];
26
- const expected = `Name,Age,Email\nAlice,25,alice@example.com\nBob,30,bob@example.com\nCharlie,35,charlie@example.com`;
26
+ const expected = 'Name,Age,Email\nAlice,25,alice@example.com\nBob,30,bob@example.com\nCharlie,35,charlie@example.com';
27
27
  const result = genCSV(data, columns);
28
28
  expect(result).toEqual(expected);
29
29
  });
@@ -39,7 +39,7 @@ describe('genCSV', () => {
39
39
  { name: 'Bob', age: null, email: 'bob@example.com' },
40
40
  { name: 'Charlie', age: 35, email: undefined },
41
41
  ];
42
- const expected = `Name,Age,Email\nAlice,,\nBob,,bob@example.com\nCharlie,35,`;
42
+ const expected = 'Name,Age,Email\nAlice,,\nBob,,bob@example.com\nCharlie,35,';
43
43
  const result = genCSV(data, columns);
44
44
  expect(result).toEqual(expected);
45
45
  });
@@ -54,8 +54,22 @@ describe('genCSV', () => {
54
54
  { name: 'Bob', data: [1, 2, 3] },
55
55
  { name: 'Charlie', data: null },
56
56
  ];
57
- const expected = `Name,Data\nAlice,"{""foo"":""bar""}"\nBob,"[1,2,3]"\nCharlie,`;
57
+ const expected = 'Name,Data\nAlice,"{""foo"":""bar""}"\nBob,"[1,2,3]"\nCharlie,';
58
58
  const result = genCSV(data, columns);
59
59
  expect(result).toEqual(expected);
60
60
  });
61
- });
61
+
62
+ it('should use textIfUndefined for undefined values', () => {
63
+ const columns = [
64
+ { title: 'Name', param: 'name' },
65
+ { title: 'Age', param: 'age', textIfUndefined: 'N/A' },
66
+ ];
67
+ const data = [
68
+ { name: 'Alice', age: undefined },
69
+ { name: 'Bob', age: 30 },
70
+ ];
71
+ const expected = 'Name,Age\nAlice,N/A\nBob,30';
72
+ const result = genCSV(data, columns);
73
+ expect(result).toEqual(expected);
74
+ });
75
+ });
@@ -21,7 +21,10 @@ const escapeCellText = (text: string): string => {
21
21
  * Generate a CSV file
22
22
  * @author Gabe Abrams
23
23
  * @param data list of row data in the form of json objects
24
- * @param columns list of columns to include in the csv
24
+ * @param columns list of columns to include in the csv where each column is an object with:
25
+ * - title: the column title
26
+ * - param: the key in the data object to use for this column
27
+ * - textIfUndefined: optional text to use if the value is undefined (defaults to empty string)
25
28
  * @returns multiline csv string
26
29
  */
27
30
  const genCSV = (
@@ -31,6 +34,7 @@ const genCSV = (
31
34
  columns: {
32
35
  title: string,
33
36
  param: string,
37
+ textIfUndefined?: string,
34
38
  }[],
35
39
  ): string => {
36
40
  let csv = '';
@@ -57,11 +61,13 @@ const genCSV = (
57
61
  || typeof cell === 'number'
58
62
  ) {
59
63
  contents = String(cell);
60
- } else if (
61
- typeof cell === 'undefined'
62
- || cell === null
63
- ) {
64
+ } else if (cell === null) {
64
65
  contents = '';
66
+ } else if (typeof cell === 'undefined') {
67
+ contents = (
68
+ column.textIfUndefined
69
+ ?? ''
70
+ );
65
71
  } else if (typeof cell === 'object') {
66
72
  contents = JSON.stringify(cell);
67
73
  } else {