Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove parse due unsafety and replace it with parseRows #75

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
csv2geojson.js
/node_modules
51 changes: 36 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function autoDelimiter(x) {
var results = [];

delimiters.forEach(function (delimiter) {
var res = dsv.dsvFormat(delimiter).parse(x);
var res = dsv.dsvFormat(delimiter).parseRows(x);
if (res.length >= 1) {
var count = keyCount(res[0]);
for (var i = 0; i < res.length; i++) {
Expand All @@ -45,7 +45,6 @@ function autoDelimiter(x) {
});
}
});

if (results.length) {
return results.sort(function (a, b) {
return b.arity - a.arity;
Expand All @@ -56,20 +55,45 @@ function autoDelimiter(x) {
}

/**
* Silly stopgap for dsv to d3-dsv upgrade
*
* @param {Array} x dsv output
* @returns {Array} array without columns member
* @function formatDsvOutput
* @param {Array} dsvArray dsv output
* @returns {Array} array without columns member and each element has the proper column name
*/

function formatDsvOutput(dsvArray) {
return deleteFirstRow(mapToObject(dsvArray));
}

/**
* @function mapToObject
* @param {Array} dsvArray dsv output
* @returns {Array} array with each element has the proper column name
*/

function mapToObject(dsvArray) {
return dsvArray.map((row) => {
return row.reduce((resultObject, cv, index) => {
const column = dsvArray[0][index];
const object = {[`${column}`]: cv};
return Object.assign({}, resultObject, object);
}, {});
});
}

/**
* @function deleteFirstRow
* @param {Array} dsvArray dsv output
* @returns {Array} array without columns member(x[0] from parseRows)
*/
function deleteColumns(x) {
delete x.columns;
return x;

function deleteFirstRow(dsvArray) {
return dsvArray.slice(1, dsvArray.length);
}

function auto(x) {
var delimiter = autoDelimiter(x);
if (!delimiter) return null;
return deleteColumns(dsv.dsvFormat(delimiter).parse(x));
return formatDsvOutput((dsv.dsvFormat(delimiter).parseRows(x)));
}

function csv2geojson(x, options, callback) {
Expand Down Expand Up @@ -102,11 +126,9 @@ function csv2geojson(x, options, callback) {
return;
}
}

var numericFields = options.numericFields ? options.numericFields.split(',') : null;

var parsed = (typeof x == 'string') ?
dsv.dsvFormat(options.delimiter).parse(x, function (d) {
formatDsvOutput((dsv.dsvFormat(options.delimiter).parseRows(x, function (d) {
if (numericFields) {
for (var key in d) {
if (numericFields.includes(key)) {
Expand All @@ -115,8 +137,7 @@ function csv2geojson(x, options, callback) {
}
}
return d;
}) : x;

}))) : x;
if (!parsed.length) {
callback(null, featurecollection);
return;
Expand Down
Loading