Skip to content

Commit

Permalink
refactor(csv-stringify): remove unused dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Nov 19, 2021
1 parent bfcbd5b commit 30f7c84
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 443 deletions.
5 changes: 0 additions & 5 deletions packages/csv-stringify/dist/cjs/sync.cjs
Expand Up @@ -3,7 +3,6 @@
Object.defineProperty(exports, '__esModule', { value: true });

var stream = require('stream');
var string_decoder = require('string_decoder');

const bom_utf8 = Buffer.from([239, 187, 191]);

Expand Down Expand Up @@ -546,10 +545,6 @@ class Stringifier extends stream.Transform {

const stringify = function(records, options={}){
const data = [];
if(Buffer.isBuffer(records)){
const decoder = new string_decoder.StringDecoder();
records = decoder.write(records);
}
const stringifier = new Stringifier(options);
stringifier.push = function(record){
if(record === null){
Expand Down
4 changes: 0 additions & 4 deletions packages/csv-stringify/dist/esm/sync.js
Expand Up @@ -5507,10 +5507,6 @@ class Stringifier extends Transform {

const stringify = function(records, options={}){
const data = [];
if(isBuffer(records)){
const decoder = new StringDecoder();
records = decoder.write(records);
}
const stringifier = new Stringifier(options);
stringifier.push = function(record){
if(record === null){
Expand Down
4 changes: 0 additions & 4 deletions packages/csv-stringify/dist/iife/sync.js
Expand Up @@ -5510,10 +5510,6 @@ var csv_stringify_sync = (function (exports) {

const stringify = function(records, options={}){
const data = [];
if(isBuffer(records)){
const decoder = new StringDecoder();
records = decoder.write(records);
}
const stringifier = new Stringifier(options);
stringifier.push = function(record){
if(record === null){
Expand Down
4 changes: 0 additions & 4 deletions packages/csv-stringify/dist/umd/sync.js
Expand Up @@ -5513,10 +5513,6 @@

const stringify = function(records, options={}){
const data = [];
if(isBuffer(records)){
const decoder = new StringDecoder();
records = decoder.write(records);
}
const stringifier = new Stringifier(options);
stringifier.push = function(record){
if(record === null){
Expand Down
5 changes: 0 additions & 5 deletions packages/csv-stringify/lib/sync.js
@@ -1,13 +1,8 @@

import { Stringifier } from './index.js';
import { StringDecoder } from 'string_decoder';

const stringify = function(records, options={}){
const data = [];
if(Buffer.isBuffer(records)){
const decoder = new StringDecoder();
records = decoder.write(records);
}
const stringifier = new Stringifier(options);
stringifier.push = function(record){
if(record === null){
Expand Down
95 changes: 49 additions & 46 deletions packages/csv/dist/cjs/index.cjs
Expand Up @@ -1926,7 +1926,9 @@ class Stringifier extends stream.Transform {
options.header = false;
}
// Normalize option `columns`
options.columns = this.normalize_columns(options.columns);
const [err, columns] = this.normalize_columns(options.columns);
if(err) return err;
options.columns = columns;
// Normalize option `quoted`
if(options.quoted === undefined || options.quoted === null){
options.quoted = false;
Expand Down Expand Up @@ -1978,45 +1980,62 @@ class Stringifier extends stream.Transform {
if(this.state.stop === true){
return;
}
const err = this.__transform(chunk);
if(err !== undefined){
this.state.stop = true;
}
callback(err);
}
_flush(callback){
if(this.info.records === 0){
this.bom();
const err = this.headers();
if(err) callback(err);
}
callback();
}
__transform(chunk){
// Chunk validation
if(!Array.isArray(chunk) && typeof chunk !== 'object'){
this.state.stop = true;
return callback(Error(`Invalid Record: expect an array or an object, got ${JSON.stringify(chunk)}`));
return Error(`Invalid Record: expect an array or an object, got ${JSON.stringify(chunk)}`);
}
// Detect columns from the first record
if(this.info.records === 0){
if(Array.isArray(chunk)){
if(this.options.header === true && this.options.columns === undefined){
this.state.stop = true;
return callback(Error('Undiscoverable Columns: header option requires column option or object records'));
return Error('Undiscoverable Columns: header option requires column option or object records');
}
}else if(this.options.columns === undefined){
this.options.columns = this.normalize_columns(Object.keys(chunk));
const [err, columns] = this.normalize_columns(Object.keys(chunk));
if(err) return;
this.options.columns = columns;
}
}
// Emit the header
if(this.info.records === 0){
this.bom();
this.headers();
const err = this.headers();
if(err) return err;
}
// Emit and stringify the record if an object or an array
try{
this.emit('record', chunk, this.info.records);
}catch(err){
this.state.stop = true;
return this.emit('error', err);
return err;
}
// Convert the record into a string
let chunk_string;
let err, chunk_string;
if(this.options.eof){
chunk_string = this.stringify(chunk);
[err, chunk_string] = this.stringify(chunk);
if(err) return err;
if(chunk_string === undefined){
return;
}else {
chunk_string = chunk_string + this.options.record_delimiter;
}
}else {
chunk_string = this.stringify(chunk);
[err, chunk_string] = this.stringify(chunk);
if(err) return err;
if(chunk_string === undefined){
return;
}else {
Expand All @@ -2028,18 +2047,10 @@ class Stringifier extends stream.Transform {
// Emit the csv
this.info.records++;
this.push(chunk_string);
callback();
}
_flush(callback){
if(this.info.records === 0){
this.bom();
this.headers();
}
callback();
}
stringify(chunk, chunkIsHeader=false){
if(typeof chunk !== 'object'){
return chunk;
return [undefined, chunk];
}
const {columns} = this.options;
const record = [];
Expand All @@ -2056,10 +2067,7 @@ class Stringifier extends stream.Transform {
const [err, value] = this.__cast(field, {
index: i, column: i, records: this.info.records, header: chunkIsHeader
});
if(err){
this.emit('error', err);
return;
}
if(err) return [err];
record[i] = [value, field];
}
// Record is a literal object
Expand All @@ -2070,10 +2078,7 @@ class Stringifier extends stream.Transform {
const [err, value] = this.__cast(field, {
index: i, column: columns[i].key, records: this.info.records, header: chunkIsHeader
});
if(err){
this.emit('error', err);
return;
}
if(err) return [err];
record[i] = [value, field];
}
}
Expand All @@ -2085,30 +2090,25 @@ class Stringifier extends stream.Transform {
if(typeof value === "string"){
options = this.options;
}else if(isObject(value)){
// let { value, ...options } = value
options = value;
value = options.value;
delete options.value;
if(typeof value !== "string" && value !== undefined && value !== null){
this.emit("error", Error(`Invalid Casting Value: returned value must return a string, null or undefined, got ${JSON.stringify(value)}`));
return;
if(err) return [Error(`Invalid Casting Value: returned value must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
options = {...this.options, ...options};
if((err = this.normalize(options)) !== undefined){
this.emit("error", err);
return;
return [err];
}
}else if(value === undefined || value === null){
options = this.options;
}else {
this.emit("error", Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`));
return;
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
if(value){
if(typeof value !== 'string'){
this.emit("error", Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`));
return null;
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)];
}
const containsdelimiter = delimiter.length && value.indexOf(delimiter) >= 0;
const containsQuote = (quote !== '') && value.indexOf(quote) >= 0;
Expand Down Expand Up @@ -2145,7 +2145,7 @@ class Stringifier extends stream.Transform {
csvrecord += delimiter;
}
}
return csvrecord;
return [undefined, csvrecord];
}
bom(){
if(this.options.bom !== true){
Expand All @@ -2160,12 +2160,15 @@ class Stringifier extends stream.Transform {
if(this.options.columns === undefined){
return;
}
let err;
let headers = this.options.columns.map(column => column.header);
if(this.options.eof){
headers = this.stringify(headers, true) + this.options.record_delimiter;
[err, headers] = this.stringify(headers, true);
headers += this.options.record_delimiter;
}else {
headers = this.stringify(headers);
[err, headers] = this.stringify(headers);
}
if(err) return err;
this.push(headers);
}
__cast(value, context){
Expand All @@ -2192,10 +2195,10 @@ class Stringifier extends stream.Transform {
}
normalize_columns(columns){
if(columns === undefined || columns === null){
return undefined;
return [];
}
if(typeof columns !== 'object'){
throw Error('Invalid option "columns": expect an array or an object');
return [Error('Invalid option "columns": expect an array or an object')];
}
if(!Array.isArray(columns)){
const newcolumns = [];
Expand All @@ -2216,19 +2219,19 @@ class Stringifier extends stream.Transform {
});
}else if(typeof column === 'object' && column !== undefined && !Array.isArray(column)){
if(!column.key){
throw Error('Invalid column definition: property "key" is required');
return [Error('Invalid column definition: property "key" is required')];
}
if(column.header === undefined){
column.header = column.key;
}
newcolumns.push(column);
}else {
throw Error('Invalid column definition: expect a string or an object');
return [Error('Invalid column definition: expect a string or an object')];
}
}
columns = newcolumns;
}
return columns;
return [undefined, columns];
}
}

Expand Down

0 comments on commit 30f7c84

Please sign in to comment.