Skip to content

Commit

Permalink
stripBOM
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Feb 14, 2025
1 parent 349de02 commit 480afcb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions dist/uDSV.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const BOOL_RE = /^(?:t(?:rue)?|f(?:alse)?|y(?:es)?|n(?:o)?|0|1)$/i;

const COL_DELIMS = [tab, pipe, semi, comma];

function stripBOM(str) {
return str.charCodeAt(0) === 0xFEFF ? str.slice(1) : str;
}

function boolTrue(v) {
let [c0, c1 = ''] = v;

Expand Down Expand Up @@ -177,6 +181,8 @@ function inferSchema(csvStr, opts, maxRows) {

maxRows ??= 10;

csvStr = stripBOM(csvStr);

// will fail if header contains line breaks in quoted value
// will fail if single line without line breaks
const rowRE = new RegExp(`(.*)(${rowDelim ?? '\r\n|\r|\n'})`);
Expand Down Expand Up @@ -376,6 +382,8 @@ function initParser(schema) {
// _maxCols is cols estimated by simple delimiter detection and split()
// returns [unparsed tail, shouldHalt]
function parse(csvStr, schema, skip = 0, each = () => true, withEOF = true, _maxCols) {
csvStr = stripBOM(csvStr);

let {
row: rowDelim,
col: colDelim,
Expand Down
8 changes: 8 additions & 0 deletions dist/uDSV.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ var uDSV = (function (exports) {

const COL_DELIMS = [tab, pipe, semi, comma];

function stripBOM(str) {
return str.charCodeAt(0) === 0xFEFF ? str.slice(1) : str;
}

function boolTrue(v) {
let [c0, c1 = ''] = v;

Expand Down Expand Up @@ -178,6 +182,8 @@ var uDSV = (function (exports) {

maxRows ??= 10;

csvStr = stripBOM(csvStr);

// will fail if header contains line breaks in quoted value
// will fail if single line without line breaks
const rowRE = new RegExp(`(.*)(${rowDelim ?? '\r\n|\r|\n'})`);
Expand Down Expand Up @@ -377,6 +383,8 @@ var uDSV = (function (exports) {
// _maxCols is cols estimated by simple delimiter detection and split()
// returns [unparsed tail, shouldHalt]
function parse(csvStr, schema, skip = 0, each = () => true, withEOF = true, _maxCols) {
csvStr = stripBOM(csvStr);

let {
row: rowDelim,
col: colDelim,
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.

8 changes: 8 additions & 0 deletions dist/uDSV.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const BOOL_RE = /^(?:t(?:rue)?|f(?:alse)?|y(?:es)?|n(?:o)?|0|1)$/i;

const COL_DELIMS = [tab, pipe, semi, comma];

function stripBOM(str) {
return str.charCodeAt(0) === 0xFEFF ? str.slice(1) : str;
}

function boolTrue(v) {
let [c0, c1 = ''] = v;

Expand Down Expand Up @@ -175,6 +179,8 @@ function inferSchema(csvStr, opts, maxRows) {

maxRows ??= 10;

csvStr = stripBOM(csvStr);

// will fail if header contains line breaks in quoted value
// will fail if single line without line breaks
const rowRE = new RegExp(`(.*)(${rowDelim ?? '\r\n|\r|\n'})`);
Expand Down Expand Up @@ -374,6 +380,8 @@ function initParser(schema) {
// _maxCols is cols estimated by simple delimiter detection and split()
// returns [unparsed tail, shouldHalt]
function parse(csvStr, schema, skip = 0, each = () => true, withEOF = true, _maxCols) {
csvStr = stripBOM(csvStr);

let {
row: rowDelim,
col: colDelim,
Expand Down
8 changes: 8 additions & 0 deletions src/uDSV.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const BOOL_RE = /^(?:t(?:rue)?|f(?:alse)?|y(?:es)?|n(?:o)?|0|1)$/i;

const COL_DELIMS = [tab, pipe, semi, comma];

function stripBOM(str) {
return str.charCodeAt(0) === 0xFEFF ? str.slice(1) : str;
}

function boolTrue(v) {
let [c0, c1 = ''] = v;

Expand Down Expand Up @@ -168,6 +172,8 @@ export function inferSchema(csvStr, opts, maxRows) {

maxRows ??= 10;

csvStr = stripBOM(csvStr);

// will fail if header contains line breaks in quoted value
// will fail if single line without line breaks
const rowRE = new RegExp(`(.*)(${rowDelim ?? '\r\n|\r|\n'})`);
Expand Down Expand Up @@ -368,6 +374,8 @@ export function initParser(schema) {
// _maxCols is cols estimated by simple delimiter detection and split()
// returns [unparsed tail, shouldHalt]
function parse(csvStr, schema, skip = 0, each = () => true, withEOF = true, _maxCols) {
csvStr = stripBOM(csvStr);

let {
row: rowDelim,
col: colDelim,
Expand Down
14 changes: 14 additions & 0 deletions test/parse.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,20 @@ test('repl injection guard', (t) => {
}
});

test('strip BOM', (t) => {
const csvStr = '\uFEFFa,b,c\n1,2,3';

let schema = inferSchema(csvStr);
schema.skip = 0;
let parser = initParser(schema);
let arrs = parser.stringArrs(csvStr);

assert.deepEqual(arrs, [
['a','b','c'],
['1','2','3'],
]);
});

test('transform stream', (t) => {
class ParseCSVTransform extends Transform {
#parser = null;
Expand Down

0 comments on commit 480afcb

Please sign in to comment.