Skip to content

Commit

Permalink
Fix date values are not auto detected as headers
Browse files Browse the repository at this point in the history
  • Loading branch information
koresar committed Jul 10, 2021
1 parent 2c56cdd commit f283e07
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/lil-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let isNumber = (v) => typeof v === "number";
let isBoolean = (v) => typeof v === "boolean";
let isDate = (v) => v instanceof Date && !isNaN(v.valueOf());
let isObject = (v) => v && typeof v === "object" && !isArray(v);
const isPlainObject = (value) => value && typeof value == "object" && value.__proto__ == Object.prototype;
let isFunction = (v) => typeof v === "function";

function getDeep(obj, path) {
Expand All @@ -13,17 +14,17 @@ function getDeep(obj, path) {
function setDeep(obj, path, value) {
path.split(".").reduce((result, curr, index, paths) => {
let newVal = index + 1 === paths.length ? value : {};
return isObject(result[curr]) ? result[curr] : (result[curr] = newVal);
return isPlainObject(result[curr]) ? result[curr] : (result[curr] = newVal);
}, obj);
}

function mergeDeep(target, ...sources) {
if (!sources.length) return target;
let source = sources.shift();

if (isObject(target) && isObject(source)) {
if (isPlainObject(target) && isPlainObject(source)) {
for (let [key, value] of Object.entries(source)) {
if (isObject(value)) {
if (isPlainObject(value)) {
mergeDeep((target[key] = target[key] || {}), value);
} else {
target[key] = value;
Expand All @@ -37,7 +38,7 @@ function mergeDeep(target, ...sources) {
function keysDeep(obj, prefix) {
return Object.entries(obj).reduce((keys, [k, v]) => {
let newKey = prefix ? prefix + "." + k : k;
return keys.concat(isObject(v) ? keysDeep(v, newKey) : newKey);
return keys.concat(isPlainObject(v) ? keysDeep(v, newKey) : newKey);
}, []);
}

Expand Down Expand Up @@ -170,7 +171,7 @@ export function generate(rows, { header = true, lineTerminator = "\n", escapeCha
if (!isArray(rows[0])) throw new Error("Can't auto detect header from rows");
}

if (isObject(header)) {
if (isPlainObject(header)) {
// If there is "star" header then the column order would be taken from the data rows;
// but if no "*" was given then let's user the `header` object order.
let detectedHeadersSet = new Set(header["*"] ? detectedHeaders : Object.keys(header));
Expand Down
24 changes: 24 additions & 0 deletions test/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,28 @@ John,Smith,1999-01-15,123.00,Y
Alice,Dwarf,1991-11-24,123.00,N`
);
});

it("should parse and generate with '*' header", () => {
let fileContents = `firstName,lastName,dob,price,completed
John,Smith,1999-01-15,123.55,Y
Alice,Dwarf,1991-11-24,123.55,N`;
const people = parse(fileContents, {
header: {
"*": String,
dob: (v) => (v ? new Date(v) : null),
price: (v) => (isNaN(v) ? null : Number(v)),
completed: (v) => String(v).toUpperCase() === "Y",
},
});

let string = generate(people, {
header: {
"*": String,
dob: (v) => (v ? new Date(v).toISOString().substr(0, 10) : ""),
price: (v) => (isNaN(v) ? "" : Number(v).toFixed(2)),
completed: (v) => (v ? "Y" : "N"),
},
});
assert.strictEqual(string, fileContents);
});
});

0 comments on commit f283e07

Please sign in to comment.