Skip to content

Commit

Permalink
implement .stringCols()
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Dec 2, 2024
1 parent 233fc71 commit c08e92b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ let typedObjs = parser.typedObjs(csvStr); // [ {a: 1, b: 2, c: 3}, {a: 4, b: 5
let typedCols = parser.typedCols(csvStr); // [ [1, 4], [2, 5], [3, 6] ]

let stringObjs = parser.stringObjs(csvStr); // [ {a: '1', b: '2', c: '3'}, {a: '4', b: '5', c: '6'} ]
let stringCols = parser.typedCols(csvStr); // [ ['1', '4'], ['2', '5'], ['3', '6'] ]
```

Nested/deep objects can be re-constructed from column naming via `.typedDeep()`:
Expand Down
6 changes: 6 additions & 0 deletions dist/uDSV.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,17 @@ function initParser(schema, chunkSize) {
return rows => _toCols(_toArrs(rows));
});

const stringCols = gen(initRows, addRows, () => {
_toCols ??= genToCols(cols);
return _toCols;
});

return {
schema,

stringArrs,
stringObjs,
stringCols,

typedArrs,
typedObjs,
Expand Down
3 changes: 3 additions & 0 deletions dist/uDSV.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ export interface Parser {
/** parses to typed columnar arrays */
typedCols: <T extends unknown[] = []>(csvStr: string, onData?: OnDataFn<T>) => T[];

/** parses to string columnar arrays */
stringCols: <T extends string[] = []>(csvStr: string, onData?: OnDataFn<T>) => T[];

/**
* starts or continues incremental parsing \
* default parse = stringArrs, default onData = accumulator
Expand Down
6 changes: 6 additions & 0 deletions dist/uDSV.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,17 @@ var uDSV = (function (exports) {
return rows => _toCols(_toArrs(rows));
});

const stringCols = gen(initRows, addRows, () => {
_toCols ??= genToCols(cols);
return _toCols;
});

return {
schema,

stringArrs,
stringObjs,
stringCols,

typedArrs,
typedObjs,
Expand Down
2 changes: 1 addition & 1 deletion dist/uDSV.iife.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dist/uDSV.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,17 @@ function initParser(schema, chunkSize) {
return rows => _toCols(_toArrs(rows));
});

const stringCols = gen(initRows, addRows, () => {
_toCols ??= genToCols(cols);
return _toCols;
});

return {
schema,

stringArrs,
stringObjs,
stringCols,

typedArrs,
typedObjs,
Expand Down
6 changes: 6 additions & 0 deletions src/uDSV.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,17 @@ export function initParser(schema, chunkSize) {
return rows => _toCols(_toArrs(rows));
});

const stringCols = gen(initRows, addRows, () => {
_toCols ??= genToCols(cols);
return _toCols;
});

return {
schema,

stringArrs,
stringObjs,
stringCols,

typedArrs,
typedObjs,
Expand Down
15 changes: 15 additions & 0 deletions test/parse.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,21 @@ test('string objs', (t) => {
]);
});

test('string cols', (t) => {
const csvStr = `a,b,c\n1,2,3\n4,,6\n7,NaN,null`;

let schema = inferSchema(csvStr);
let parser = initParser(schema);

let rows = parser.stringCols(csvStr);

assert.deepEqual(rows, [
['1', '4', '7' ],
['2', '', 'NaN' ],
['3', '6', 'null'],
]);
});

test('typed cols', (t) => {
const csvStr = `a,b,c\n1,2,3\n4,5,6`;

Expand Down

0 comments on commit c08e92b

Please sign in to comment.