Comparing version 0.0.3 to 0.0.4
@@ -15,3 +15,4 @@ #!/usr/bin/env node | ||
const yaml_1 = __importDefault(require("yaml")); | ||
const validFileTypes = ["txt", "json", "yaml"]; | ||
const sync_1 = __importDefault(require("csv-parse/lib/sync")); | ||
const validFileTypes = ["txt", "json", "yaml", "csv"]; | ||
const getFileType = (filename) => { | ||
@@ -25,2 +26,4 @@ switch (true) { | ||
return "yaml"; | ||
case commander_1.program.csv: | ||
return "csv"; | ||
} | ||
@@ -55,2 +58,7 @@ if (filename) { | ||
return yaml_1.default.parse(content); | ||
case "csv": | ||
return sync_1.default(content, { | ||
columns: true, | ||
skipEmptyLines: true | ||
}); | ||
case "txt": | ||
@@ -77,3 +85,8 @@ return content.split(os_1.EOL); | ||
readContent(filename, fileType, (data) => { | ||
printer_1.print(query_1.run(query, data, lodash_1.default), fileType, commander_1.program.save); | ||
if (commander_1.program.save) { | ||
printer_1.writeToFile(query_1.run(query, data, lodash_1.default), commander_1.program.save, fileType); | ||
} | ||
else { | ||
printer_1.print(query_1.run(query, data, lodash_1.default), fileType, commander_1.program.print); | ||
} | ||
}); | ||
@@ -87,2 +100,3 @@ } | ||
.option('--yaml', 'tell the program it\'s yaml content') | ||
.option('--print <format>', 'printer format (table)') | ||
.option('--save <filename>', 'save output to a file') | ||
@@ -89,0 +103,0 @@ .arguments(`<query> [filename]`) |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.print = void 0; | ||
exports.writeToFile = exports.print = void 0; | ||
const stream_1 = require("stream"); | ||
@@ -13,12 +13,28 @@ const fs_1 = require("fs"); | ||
const yaml_1 = __importDefault(require("yaml")); | ||
function print(data, fileType, filename) { | ||
if (filename) { | ||
writeToFile(data, filename, fileType); | ||
const util_1 = __importDefault(require("util")); | ||
function print(data, fileType, printer) { | ||
if (printer === "table") { | ||
console.table(data); | ||
return; | ||
} | ||
if (Array.isArray(data)) { | ||
writeToSTD(data); | ||
if (printer === "json") { | ||
process.stdout.write(util_1.default.inspect(data, false, null, true)); | ||
process.stdout.write(os_1.EOL); | ||
return; | ||
} | ||
console.log(data); | ||
if (printer === "yaml") { | ||
process.stdout.write(yaml_1.default.stringify(data)); | ||
process.stdout.write(os_1.EOL); | ||
return; | ||
} | ||
if (fileType === "csv") { | ||
console.table(data); | ||
return; | ||
} | ||
if (fileType === "txt") { | ||
writeToStdout(data); | ||
return; | ||
} | ||
process.stdout.write(util_1.default.inspect(data, false, null, true)); | ||
process.stdout.write(os_1.EOL); | ||
} | ||
@@ -39,3 +55,3 @@ exports.print = print; | ||
} | ||
let text = ''; | ||
let text = ""; | ||
if (typeof data === "string" || typeof data === "number") { | ||
@@ -52,3 +68,4 @@ text = String(data); | ||
} | ||
function writeToSTD(items) { | ||
exports.writeToFile = writeToFile; | ||
function writeToStdout(items) { | ||
const stream = new stream_1.Readable({ | ||
@@ -60,3 +77,3 @@ read(bits) { | ||
this.push(items.shift() + os_1.EOL); | ||
} | ||
}, | ||
}); | ||
@@ -63,0 +80,0 @@ stream.on("error", (err) => { |
@@ -103,2 +103,7 @@ "use strict"; | ||
} | ||
}, | ||
pick: { | ||
value(...args) { | ||
return this.map((item) => _.pick(item, args)); | ||
} | ||
} | ||
@@ -105,0 +110,0 @@ }); |
{ | ||
"name": "jiq", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Use existing javascript knowledge to query or mutate data", | ||
@@ -21,3 +21,3 @@ "keywords": [ | ||
"repository": { | ||
"url": "https://md-adil.github.io/jiq/", | ||
"url": "https://github.com/md-adil/jiq", | ||
"type": "git" | ||
@@ -37,2 +37,3 @@ }, | ||
"commander": "^6.2.0", | ||
"csv-parse": "^4.14.0", | ||
"lodash": "^4.17.20", | ||
@@ -39,0 +40,0 @@ "yaml": "^1.10.0" |
@@ -127,2 +127,19 @@ # Javascript Inline Query (jiq) | ||
Printing in table format | ||
jiq '.keywords' package.json --print table | ||
output | ||
┌─────────┬───────────────────────────┐ | ||
│ (index) │ Values │ | ||
├─────────┼───────────────────────────┤ | ||
│ 0 │ 'javascript inline query' │ | ||
│ 1 │ 'json query' │ | ||
│ 2 │ 'json inline query' │ | ||
│ 3 │ 'json' │ | ||
│ 4 │ 'yaml' │ | ||
│ 5 │ 'query' │ | ||
└─────────┴───────────────────────────┘ | ||
### Using `$` | ||
@@ -129,0 +146,0 @@ |
@@ -8,7 +8,8 @@ #!/usr/bin/env node | ||
import { parseCommand, run } from "./query"; | ||
import { print } from "./printer"; | ||
import { print, writeToFile } from "./printer"; | ||
import { EOL } from "os"; | ||
import YAML from "yaml"; | ||
import csv from "csv-parse/lib/sync"; | ||
const validFileTypes = [ "txt", "json", "yaml" ] as const; | ||
const validFileTypes = [ "txt", "json", "yaml", "csv" ] as const; | ||
export type FileType = typeof validFileTypes[number]; | ||
@@ -23,2 +24,4 @@ const getFileType = (filename?: string): FileType => { | ||
return "yaml"; | ||
case program.csv: | ||
return "csv"; | ||
} | ||
@@ -55,2 +58,7 @@ if (filename) { | ||
return YAML.parse(content); | ||
case "csv": | ||
return csv(content, { | ||
columns: true, | ||
skipEmptyLines: true | ||
}); | ||
case "txt": | ||
@@ -79,7 +87,11 @@ return content.split(EOL); | ||
readContent(filename, fileType, (data) => { | ||
print(run(query, data, _), fileType, program.save); | ||
if (program.save) { | ||
writeToFile(run(query, data, _), program.save, fileType); | ||
} else { | ||
print(run(query, data, _), fileType, program.print); | ||
} | ||
}); | ||
}; | ||
program.version("0.0.3"); | ||
program.version("0.0.4"); | ||
program | ||
@@ -89,2 +101,3 @@ .option('--json', 'tell the program it\'s json content') | ||
.option('--yaml', 'tell the program it\'s yaml content') | ||
.option('--print <format>', 'printer format (table)') | ||
.option('--save <filename>', 'save output to a file') | ||
@@ -91,0 +104,0 @@ .arguments(`<query> [filename]`) |
@@ -7,16 +7,32 @@ import { Readable } from "stream"; | ||
import YAML from "yaml"; | ||
import util from "util"; | ||
export function print(data: any, fileType: FileType, filename?: string) { | ||
if (filename) { | ||
writeToFile(data, filename, fileType); | ||
export function print(data: any, fileType: FileType, printer?: string) { | ||
if (printer === "table") { | ||
console.table(data); | ||
return; | ||
} | ||
if (Array.isArray(data)) { | ||
writeToSTD(data); | ||
if (printer === "json") { | ||
process.stdout.write(util.inspect(data, false, null, true)); | ||
process.stdout.write(EOL); | ||
return; | ||
} | ||
console.log(data); | ||
if (printer === "yaml") { | ||
process.stdout.write(YAML.stringify(data)); | ||
process.stdout.write(EOL); | ||
return; | ||
} | ||
if (fileType === "csv") { | ||
console.table(data); | ||
return; | ||
} | ||
if (fileType === "txt") { | ||
writeToStdout(data); | ||
return; | ||
} | ||
process.stdout.write(util.inspect(data, false, null, true)); | ||
process.stdout.write(EOL); | ||
} | ||
function writeToFile(data: any, filename: string, fileType: FileType) { | ||
export function writeToFile(data: any, filename: string, fileType: FileType) { | ||
const ext = extname(filename); | ||
@@ -31,13 +47,11 @@ if (ext) { | ||
if (fileType === "yaml") { | ||
writeFileSync(filename, YAML.stringify(data)); | ||
return; | ||
writeFileSync(filename, YAML.stringify(data)); | ||
return; | ||
} | ||
let text = ''; | ||
let text = ""; | ||
if (typeof data === "string" || typeof data === "number") { | ||
text = String(data); | ||
} | ||
else if (Array.isArray(data) && data[0] && typeof data[0] === "string") { | ||
} else if (Array.isArray(data) && data[0] && typeof data[0] === "string") { | ||
text = data.join(EOL); | ||
} | ||
else { | ||
} else { | ||
text = JSON.stringify(data); | ||
@@ -48,10 +62,10 @@ } | ||
function writeToSTD(items: (string|number)[]) { | ||
function writeToStdout(items: (string | number)[]) { | ||
const stream = new Readable({ | ||
read(bits) { | ||
if (!items.length) { | ||
return this.push(null) | ||
return this.push(null); | ||
} | ||
this.push(items.shift() + EOL); | ||
} | ||
}, | ||
}); | ||
@@ -58,0 +72,0 @@ stream.on("error", (err) => { |
@@ -105,2 +105,7 @@ import { LoDashStatic } from "lodash"; | ||
} | ||
}, | ||
pick: { | ||
value(...args: string[]) { | ||
return this.map((item: any) => _.pick(item, args)); | ||
} | ||
} | ||
@@ -107,0 +112,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27737
636
206
4
+ Addedcsv-parse@^4.14.0
+ Addedcsv-parse@4.16.3(transitive)