#typeCastOnly;
/**
- * @param {Array<CSVNormalizerHeader>} headers An array of metadata objects to configure the data casting and
- * cleanup transformations.
+ * @param {Array<CSVNormalizerHeader|string>} headers An array of `CSVNormalizerHeader` or `string` to configure the
+ * data casting and cleanup transformations. Passing a plain `string` is the same as passing `{ name: string }`.
* @param {CSVNormalizerOptions} options Object containing flags to configure the stream logic.
*/
constructor(headers, options = {}) {
this.#passthroughEmptyRows = options.passthroughEmptyRows ?? false;
this.#typeCastOnly = options.typeCastOnly ?? false;
- for (const { name, type, displayName = name, defaultValue = '' } of headers) {
- let normalizedType = type?.toLowerCase() ?? 'string';
- if (!['string', 'number', 'bigint', 'date'].includes(normalizedType)) {
- console.warn(`Type "${normalizedType}" is not supported, defaulting to string.`);
- normalizedType = 'string';
+ for (const header of headers) {
+ if (typeof header === 'string') {
+ const name = this.#useLiteralNames ? header : toCamelCase(header);
+ this.#columns.push({
+ name,
+ type: 'string',
+ displayName: name,
+ defaultValue: '',
+ index: null
+ });
+ }
+ else {
+ const { name, type, displayName = name, defaultValue = '' } = header;
+ let normalizedType = type?.toLowerCase() ?? 'string';
+ if (!['string', 'number', 'bigint', 'date'].includes(normalizedType)) {
+ console.warn(`Type "${normalizedType}" is not supported, defaulting to string.`);
+ normalizedType = 'string';
+ }
+ this.#columns.push({
+ name: this.#useLiteralNames ? name : toCamelCase(name),
+ type: normalizedType,
+ displayName,
+ defaultValue,
+ index: null
+ });
}
- this.#columns.push({
- name: this.#useLiteralNames ? name : toCamelCase(name),
- type: normalizedType,
- displayName,
- defaultValue,
- index: null
- });
}
}
});
suite('CSVNormalizer', { concurrency: true }, () => {
+ test('accepts string shorthand to set headers', { concurrency: true }, async () => {
+ await createCSVMockStream([
+ ['columnA', 'columnB'],
+ ['a', 'b']
+ ])
+ .pipeThrough(new CSVNormalizer(['columnA', 'columnB']))
+ .pipeThrough(new CSVDenormalizer())
+ .pipeTo(csvStreamEqualWritable([
+ ['columnA', 'columnB'],
+ ['a', 'b']
+ ]));
+ });
+ test('accepts mixed string and CSVNormalizerHeader to set headers', { concurrency: true }, async () => {
+ await createCSVMockStream([
+ ['columnA', 'columnB'],
+ ['a', 'b']
+ ])
+ .pipeThrough(new CSVNormalizer([
+ 'columnA',
+ { name: 'columnB' }
+ ]))
+ .pipeThrough(new CSVDenormalizer())
+ .pipeTo(csvStreamEqualWritable([
+ ['columnA', 'columnB'],
+ ['a', 'b']
+ ]));
+ });
test('can remove extra columns in CSV', { concurrency: true }, async () => {
await createCSVMockStream([
['extra1', 'columnA', 'extra2', 'extra3', 'columnB'],