Initial commit
This commit is contained in:
3
themes/keepit/node_modules/csvtojson/bin/csvtojson
generated
vendored
Executable file
3
themes/keepit/node_modules/csvtojson/bin/csvtojson
generated
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('./csvtojson.js')();
|
||||
1
themes/keepit/node_modules/csvtojson/bin/csvtojson.bat
generated
vendored
Normal file
1
themes/keepit/node_modules/csvtojson/bin/csvtojson.bat
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
@node csvtojson.js %*
|
||||
180
themes/keepit/node_modules/csvtojson/bin/csvtojson.js
generated
vendored
Normal file
180
themes/keepit/node_modules/csvtojson/bin/csvtojson.js
generated
vendored
Normal file
@@ -0,0 +1,180 @@
|
||||
function csvtojson() {
|
||||
var Converter = require("../v2").Converter;
|
||||
var fs = require("fs");
|
||||
var options = require("./options.json");
|
||||
var cmds = options.commands;
|
||||
var opts = options.options;
|
||||
var exps = options.examples;
|
||||
var pkg = require("../package.json");
|
||||
var os = require("os");
|
||||
/**
|
||||
*{
|
||||
"cmd": "parse", command to run
|
||||
"options": {}, options to passe to the command
|
||||
"inputStream": process.stdin // input stream for the command. default is stdin. can be a file read stream.
|
||||
};
|
||||
*
|
||||
*/
|
||||
var parsedCmd;
|
||||
|
||||
function _showHelp(errno) {
|
||||
var key;
|
||||
errno = typeof errno === "number" ? errno : 0;
|
||||
console.log("csvtojson: Convert csv to JSON format");
|
||||
console.log("version:", pkg.version);
|
||||
console.log("Usage: csvtojson [<command>] [<options>] filepath\n");
|
||||
console.log("Commands: ");
|
||||
for (key in cmds) {
|
||||
if (cmds.hasOwnProperty(key)) {
|
||||
console.log("\t%s: %s", key, cmds[key]);
|
||||
}
|
||||
}
|
||||
console.log("Options: ");
|
||||
for (key in opts) {
|
||||
if (opts.hasOwnProperty(key)) {
|
||||
console.log("\t%s: %s", key, opts[key].desc);
|
||||
}
|
||||
}
|
||||
console.log("Examples: ");
|
||||
for (var i = 0; i < exps.length; i++) {
|
||||
console.log("\t%s", exps[i]);
|
||||
}
|
||||
process.exit(errno);
|
||||
}
|
||||
function stringToRegExp(str) {
|
||||
var lastSlash = str.lastIndexOf("/");
|
||||
var source = str.substring(1, lastSlash);
|
||||
var flag = str.substring(lastSlash + 1);
|
||||
return new RegExp(source,flag);
|
||||
}
|
||||
function parse() {
|
||||
var is = parsedCmd.inputStream;
|
||||
if (parsedCmd.options.maxRowLength === undefined) {
|
||||
parsedCmd.options.maxRowLength = 10240;
|
||||
}
|
||||
if (is === process.stdin && is.isTTY) {
|
||||
console.log("Please specify csv file path or pipe the csv data through.\n");
|
||||
_showHelp(1);
|
||||
}
|
||||
if (parsedCmd.options.delimiter === "\\t") {
|
||||
parsedCmd.options.delimiter = "\t";
|
||||
}
|
||||
if (parsedCmd.options.ignoreColumns) {
|
||||
parsedCmd.options.ignoreColumns=stringToRegExp(parsedCmd.options.ignoreColumns);
|
||||
|
||||
}
|
||||
if (parsedCmd.options.includeColumns) {
|
||||
parsedCmd.options.includeColumns=stringToRegExp(parsedCmd.options.includeColumns);
|
||||
|
||||
}
|
||||
var conv = new Converter(parsedCmd.options);
|
||||
var isFirst = true;
|
||||
conv.on("error", function (err, pos) {
|
||||
if (!parsedCmd.options.quiet) {
|
||||
console.error("csvtojson got an error: ", err);
|
||||
if (pos) {
|
||||
console.error("The error happens at following line: ");
|
||||
console.log(pos);
|
||||
}
|
||||
}
|
||||
process.exit(1);
|
||||
})
|
||||
.on("data",function (dataStr) {
|
||||
process.stdout.write((isFirst ? "" : "," + os.EOL) + dataStr.toString().substr(0,dataStr.length-1));
|
||||
isFirst = false;
|
||||
})
|
||||
.on("done", function () {
|
||||
console.log(os.EOL + "]");
|
||||
})
|
||||
console.log("[");
|
||||
is.pipe(conv);
|
||||
// is.pipe(conv);
|
||||
}
|
||||
|
||||
function run(cmd, options) {
|
||||
if (cmd === "parse") {
|
||||
parse();
|
||||
} else if (cmd === "version") {
|
||||
console.log(pkg.version);
|
||||
} else {
|
||||
console.log("unknown command %s.", cmd);
|
||||
_showHelp(1);
|
||||
}
|
||||
}
|
||||
|
||||
function commandParser() {
|
||||
var parsedCmd = {
|
||||
"cmd": "parse",
|
||||
"options": {},
|
||||
"inputStream": process.stdin
|
||||
};
|
||||
|
||||
function parseObject(val, optional) {
|
||||
try {
|
||||
return JSON.parse(val);
|
||||
} catch (e) {
|
||||
if (optional) {
|
||||
return val;
|
||||
} else {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseBool(str, optName) {
|
||||
str = str.toLowerCase();
|
||||
if (str === "true" || str === "y") {
|
||||
return true;
|
||||
} else if (str === "false" || str === "n") {
|
||||
return false;
|
||||
}
|
||||
console.log("Unknown boolean value %s for parameter %s.", str, optName);
|
||||
_showHelp(1);
|
||||
}
|
||||
process.argv.slice(2).forEach(function (item) {
|
||||
if (item.indexOf("--") > -1) {
|
||||
var itemArr = item.split("=");
|
||||
var optName = itemArr[0];
|
||||
var key, val, type;
|
||||
if (!opts[optName]) {
|
||||
console.log("Option %s not supported.", optName);
|
||||
_showHelp(1);
|
||||
}
|
||||
key = optName.replace('--', '');
|
||||
val = itemArr[1] || '';
|
||||
type = opts[optName].type;
|
||||
if (type === "string") {
|
||||
parsedCmd.options[key] = val.toString();
|
||||
} else if (type === "boolean") {
|
||||
parsedCmd.options[key] = parseBool(val, optName);
|
||||
} else if (type === "number") {
|
||||
parsedCmd.options[key] = parseFloat(val);
|
||||
} else if (type === "object") {
|
||||
parsedCmd.options[key] = parseObject(val, false);
|
||||
} else if (type === "~object") {
|
||||
parsedCmd.options[key] = parseObject(val, true);
|
||||
} else {
|
||||
throw ({
|
||||
name: "UnimplementedException",
|
||||
message: "Option type parsing not implemented. See bin/options.json"
|
||||
});
|
||||
}
|
||||
} else if (cmds[item]) {
|
||||
parsedCmd.cmd = item;
|
||||
} else if (fs.existsSync(item)) {
|
||||
parsedCmd.inputStream = fs.createReadStream(item);
|
||||
} else {
|
||||
console.log("unknown parameter %s.", item);
|
||||
}
|
||||
});
|
||||
return parsedCmd;
|
||||
}
|
||||
process.stdin.setEncoding('utf8');
|
||||
parsedCmd = commandParser();
|
||||
run(parsedCmd.cmd, parsedCmd.options);
|
||||
}
|
||||
module.exports = csvtojson;
|
||||
if (!module.parent) {
|
||||
csvtojson();
|
||||
}
|
||||
32
themes/keepit/node_modules/csvtojson/bin/genCsv.js
generated
vendored
Executable file
32
themes/keepit/node_modules/csvtojson/bin/genCsv.js
generated
vendored
Executable file
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env node
|
||||
var minimist = require("minimist");
|
||||
var argv = process.argv;
|
||||
argv.shift();
|
||||
argv.shift();
|
||||
var args = minimist(argv);
|
||||
var headers = ["name", "header1", "file2", "description", "header2", "field2", "header3"];
|
||||
|
||||
if (args.headers) {
|
||||
headers = JSON.parse(args.headers);
|
||||
}
|
||||
var rowNum = args.row ? args.row : 10000;
|
||||
var chars = args.chars ? args.chars : "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||
var maxLength = parseInt(args.max ? args.max : "15");
|
||||
console.log(headers.join(","));
|
||||
for (var i = 0; i < rowNum; i++) {
|
||||
var row = [];
|
||||
for (var j = 0; j < headers.length; j++) {
|
||||
row.push(genWord());
|
||||
}
|
||||
console.log(row.join(","));
|
||||
}
|
||||
|
||||
function genWord() {
|
||||
var len = Math.round(Math.random() * maxLength);
|
||||
var rtn = "";
|
||||
for (var i = 0; i < len; i++) {
|
||||
var pos = Math.round(Math.random() * chars.length);
|
||||
rtn += chars[pos];
|
||||
}
|
||||
return rtn;
|
||||
}
|
||||
95
themes/keepit/node_modules/csvtojson/bin/options.json
generated
vendored
Normal file
95
themes/keepit/node_modules/csvtojson/bin/options.json
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
{
|
||||
"commands": {
|
||||
"parse": "(Default)Parse a csv file to json",
|
||||
"version": "Show version of current csvtojson"
|
||||
},
|
||||
"options": {
|
||||
"--output":{
|
||||
"desc": "The format to be converted to. \"json\" (default) -- convert csv to json. \"csv\" -- convert csv to csv row array. \"line\" -- convert csv to csv line string",
|
||||
"type": "string"
|
||||
},
|
||||
"--delimiter": {
|
||||
"desc": "delimiter to separate columns. Possible to give an array or just use 'auto'. default comma (,). e.g. --delimiter=# --delimiter='[\",\",\";\"]' --delimiter=auto",
|
||||
"type": "~object"
|
||||
},
|
||||
"--quote": {
|
||||
"desc": "quote surrounding a column content containing delimiters. To turn off quote, please use 'off' --quote=off. default double quote (\"). e.g. chage to hash: --quote=# ",
|
||||
"type": "string"
|
||||
},
|
||||
"--trim": {
|
||||
"desc": "Indicate if parser trim off spaces surrounding column content. e.g. \" content \" will be trimmed to \"content\". Default: true",
|
||||
"type": "boolean"
|
||||
},
|
||||
"--checkType": {
|
||||
"desc": "This parameter turns on and off whether check field type. default is false.",
|
||||
"type": "boolean"
|
||||
|
||||
},
|
||||
"--ignoreEmpty": {
|
||||
"desc": "This parameter turns on and off whether ignore empty column values while parsing. default is false",
|
||||
"type": "boolean"
|
||||
},
|
||||
"--noheader": {
|
||||
"desc": "Indicating csv data has no header row and first row is data row. Default is false",
|
||||
"type": "boolean"
|
||||
},
|
||||
"--headers": {
|
||||
"desc": "An array to specify the headers of CSV data. If --noheader is false, this value will override CSV header. Default: null. Example: --headers='[\"my field\",\"name\"]'",
|
||||
"type": "object"
|
||||
},
|
||||
"--flatKeys": {
|
||||
"desc": "Don't interpret dots (.) and square brackets in header fields as nested object or array identifiers at all (treat them like regular characters for JSON field identifiers). Default: false.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"--maxRowLength": {
|
||||
"desc": "the max character a csv row could have. 0 means infinite. If max number exceeded, parser will emit \"error\" of \"row_exceed\". if a possibly corrupted csv data provided, give it a number like 65535 so the parser wont consume memory. default: 10240",
|
||||
"type": "number"
|
||||
},
|
||||
"--checkColumn": {
|
||||
"desc": "whether check column number of a row is the same as headers. If column number mismatched headers number, an error of \"mismatched_column\" will be emitted.. default: false",
|
||||
"type": "boolean"
|
||||
},
|
||||
"--eol": {
|
||||
"desc": "Explicitly specify the end of line character to use.",
|
||||
"type": "string"
|
||||
},
|
||||
"--quiet": {
|
||||
"desc": "If any error happens, quit the process quietly rather than log out the error. Default is false.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"--escape":{
|
||||
"desc":"escape character used in quoted column. Default is double quote (\") according to RFC4108. Change to back slash (\\) or other chars for your own case.",
|
||||
"type":"string"
|
||||
},
|
||||
"--ignoreColumns": {
|
||||
"desc": "RegExp matched columns to ignore from input. e.g. --ignoreColumns=/(name|age)/ ",
|
||||
"type": "string"
|
||||
},
|
||||
"--includeColumns": {
|
||||
"desc": "RegExp matched columns to include from input. e.g. --includeColumns=/(name|age)/ ",
|
||||
"type": "string"
|
||||
},
|
||||
"--colParser": {
|
||||
"desc": "Specific parser for columns. e.g. --colParser='{\"col1\":\"number\",\"col2\":\"string\"}'",
|
||||
"type": "~object"
|
||||
},
|
||||
"--alwaysSplitAtEOL":{
|
||||
"desc": "Always interpret each line (as defined by eol) as a row. This will prevent eol characters from being used within a row (even inside a quoted field). This ensures that misplaced quotes only break on row, and not all ensuing rows.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"--nullObject":{
|
||||
"desc":"How to parse if a csv cell contains 'null'. Default false will keep 'null' as string. Change to true if a null object is needed.",
|
||||
"type":"boolean"
|
||||
},
|
||||
"--downstreamFormat":{
|
||||
"desc":"Option to set what JSON array format is needed by downstream. 'line' is also called ndjson format. This format will write lines of JSON (without square brackets and commas) to downstream. 'array' will write complete JSON array string to downstream (suitable for file writable stream etc). Default 'line'",
|
||||
"type":"string"
|
||||
}
|
||||
},
|
||||
"examples": [
|
||||
"csvtojson < csvfile",
|
||||
"csvtojson <path to csvfile>",
|
||||
"cat <csvfile> | csvtojson",
|
||||
"csvtojson <csvfilepath> --checkType=false --trim=false --delimiter=#"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user