ts-csv-builder
Advanced tools
Comparing version 1.1.0 to 1.1.1
# ts-csv-builder | ||
## 1.1.1 | ||
### Patch Changes | ||
- 0186eee: Better handling of builder concatenation and setting column options. | ||
## 1.1.0 | ||
@@ -4,0 +10,0 @@ |
@@ -54,3 +54,4 @@ type CSVTemplate = Record<string, any>; | ||
mapColumns<K extends keyof T>(toColumn: K, callbackFunc: (value: T, index: number, values: Array<T>) => T[K]): this; | ||
private checkConcatConstraints; | ||
private getMissingColumns; | ||
private padMissingColumns; | ||
concat(other: CSVBuilder<T>): this; | ||
@@ -57,0 +58,0 @@ setColumnOptions<K extends keyof T>(column: K, options: ColumnOptions<T, K>): this; |
@@ -87,23 +87,26 @@ var __defProp = Object.defineProperty; | ||
} | ||
checkConcatConstraints(other) { | ||
const sumOfDataLengths = this.dataLength + other.dataLength; | ||
const errors = [ | ||
.../* @__PURE__ */ new Set([...Object.keys(this.data), ...Object.keys(other.data)]) | ||
].map((column) => [ | ||
column, | ||
(this.data[column]?.length ?? 0) + (other.data[column]?.length ?? 0) === sumOfDataLengths | ||
]).map( | ||
([column, matchingLenghts]) => matchingLenghts ? null : `After concatenation, the length of column "${column}" does not match the resulting builder's row length.` | ||
).filter(Boolean); | ||
if (errors.length) { | ||
throw new Error(`Invalid column lengths: | ||
- ${errors.join("\n - ")}`); | ||
} | ||
getMissingColumns(other) { | ||
return [ | ||
...Object.keys(other.data).filter( | ||
(otherKey) => !Object.keys(this.data).includes(otherKey) | ||
) | ||
]; | ||
} | ||
padMissingColumns(other) { | ||
this.data = this.getMissingColumns(other).reduce( | ||
(acc, missingColumn) => ({ | ||
...acc, | ||
[missingColumn]: Array(this.dataLength).fill(null) | ||
}), | ||
this.data | ||
); | ||
return this; | ||
} | ||
concat(other) { | ||
this.checkConcatConstraints(other); | ||
this.padMissingColumns(other); | ||
other.padMissingColumns(this); | ||
this.data = Object.entries(other.data).reduce((data, [key, values]) => { | ||
return { | ||
...data, | ||
[key]: key in data ? [...data[key], ...values] : values | ||
[key]: [...data[key], ...values] | ||
}; | ||
@@ -133,12 +136,21 @@ }, this.data); | ||
setColumnOptions(column, options) { | ||
this.columnOptions[column] = options; | ||
this.columnOptions[column] = Object.entries(options).reduce( | ||
(aggrOptions, [key, value]) => ({ | ||
...aggrOptions, | ||
[key]: value | ||
}), | ||
this.columnOptions[column] | ||
); | ||
return this; | ||
} | ||
setBuilderOptions(options) { | ||
this.options = Object.entries(options).reduce((options2, [key, value]) => { | ||
return { | ||
...options2, | ||
[key]: value | ||
}; | ||
}, this.options); | ||
this.options = Object.entries(options).reduce( | ||
(aggrOptions, [key, value]) => { | ||
return { | ||
...aggrOptions, | ||
[key]: value | ||
}; | ||
}, | ||
this.options | ||
); | ||
return this; | ||
@@ -182,6 +194,6 @@ } | ||
(index) => columns.map((column) => { | ||
const f = this.columnOptions[column]?.transform; | ||
const transform = this.columnOptions[column]?.transform; | ||
const columnValue = this.data[column]?.[index]; | ||
return this.formatCell( | ||
f ? f( | ||
transform ? transform( | ||
columnValue, | ||
@@ -188,0 +200,0 @@ index, |
{ | ||
"name": "ts-csv-builder", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Utility package for building CSV files.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -197,2 +197,31 @@ # CSVBuilder | ||
### Concatenating builders with different columns | ||
In case you are attempting to concatenate two builders with different columns set so far, each builder will create empty columns for any columns it is missing, that the other builder has. This means that the following scenario: | ||
```typescript | ||
type Template = { | ||
id: number, | ||
animal: 'dog' | 'cat' | null, | ||
colour: 'blue' | 'green' | null, | ||
} | ||
const builder1 = new CSVBuilder<Template>() | ||
.createColumn('id', [1, 2]) | ||
.createColumn('animal', ['dog', 'cat']) | ||
const builder2 = new CSVBuilder<Template>() | ||
.createColumn('id', [3, 4]) | ||
.createColumn('colour', ['blue', 'green']) | ||
builder1.concat(builder2) | ||
``` | ||
will result in the following CSV: | ||
| id | animal | colour | | ||
|----|--------| -------| | ||
| 1 | dog | null | | ||
| 2 | cat | null | | ||
| 3 | null | blue | | ||
| 4 | null | green | | ||
## Merging multiple builders | ||
@@ -199,0 +228,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38837
604
356