make csv suggest return full source-type json

This commit is contained in:
Paul Trowbridge 2018-06-26 01:12:16 -04:00
parent d9087da9a1
commit fe025f6bf3

View File

@ -116,27 +116,39 @@ server.use("/csv_suggest", upload.single('upload'), function (inReq, inRes) {
//{headers: "true", delimiter: ",", output: "jsonObj", flatKeys: "true"} //{headers: "true", delimiter: ",", output: "jsonObj", flatKeys: "true"}
csvtojson({ flatKeys: "true" }).fromString(csv).then( csvtojson({ flatKeys: "true" }).fromString(csv).then(
(x) => { (x) => {
var sug = {}; var sug = {
schemas: {
default: []
},
loading_function: "csv",
source:"client_file",
name: "",
constraint: []
};
for (var key in x[0]) { for (var key in x[0]) {
var col = {};
//test if number //test if number
if (!isNaN(parseFloat(x[0][key])) && isFinite(x[0][key])) { if (!isNaN(parseFloat(x[0][key])) && isFinite(x[0][key])) {
//if is a number but leading character is -0- then it's text //if is a number but leading character is -0- then it's text
if (x[0][key].charAt(0) == "0"){ if (x[0][key].charAt(0) == "0"){
sug[key] = "text"; col["type"] = "text";
} }
//if number and leadign character is not 0 then numeric //if number and leadign character is not 0 then numeric
else { else {
sug[key] = "numeric"; col["type"] = "numeric";
} }
} }
//if can cast to a date within a hundred years its probably a date //if can cast to a date within a hundred years its probably a date
else if (Date.parse(x[0][key]) > Date.parse('1950-01-01') && Date.parse(x[0][key]) < Date.parse('2050-01-01')) { else if (Date.parse(x[0][key]) > Date.parse('1950-01-01') && Date.parse(x[0][key]) < Date.parse('2050-01-01')) {
sug[key] = "date"; col["type"] = "date";
} }
//otherwise its text //otherwise its text
else { else {
sug[key] = "text"; col["type"] = "text";
} }
col["path"] = "{" + key + "}";
col["column_name"] = key;
sug.schemas.default.push(col);
} }
console.log(sug); console.log(sug);
inRes.json(sug); inRes.json(sug);