csv-to-custom-json
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -70,3 +70,3 @@ #!/usr/bin/env node | ||
let res; | ||
(async () => { | ||
const cliAction = async () => { | ||
if (fs.existsSync(options.entry)) { | ||
@@ -130,4 +130,6 @@ let schema; | ||
} | ||
})().catch((e) => { | ||
}; | ||
cliAction().catch((e) => { | ||
console.log(e); | ||
}) |
# Welcome to the csv-to-custom-json wiki | ||
- [How to install](https://github.com/Its-Just-Nans/csv-to-custom-json/wiki/How-to-install) | ||
- [How to use](https://github.com/Its-Just-Nans/csv-to-custom-json/wiki/How-to-use) | ||
- [How to Options](https://github.com/Its-Just-Nans/csv-to-custom-json/wiki/How-to-options) | ||
- [How to CLI](https://github.com/Its-Just-Nans/csv-to-custom-json/wiki/How-to-CLI) | ||
- [How to know more](https://github.com/Its-Just-Nans/csv-to-custom-json/wiki/How-to-know-more) |
# How to use this node_modules ? | ||
First have [Node](https://nodejs.org/) installed. If it's isn't the case, you can download it on the [official website](https://nodejs.org/en/download/) | ||
First have [NodeJS](https://nodejs.org/) installed. If it's isn't the case, you can download it on the [official website](https://nodejs.org/en/download/) | ||
@@ -8,3 +8,3 @@ Then install my node_module with | ||
```sh | ||
npm install csv | ||
npm install csv-to-custom-json | ||
``` | ||
@@ -14,3 +14,4 @@ | ||
> | ||
> - `npm` stands for `Node Packet Manager`, it can be used to download `node_modules` | ||
> - `npm` stands for `Node Packet Manager`, it can be used to download `node_modules` | ||
> - `csv-to-custom-json` is the name of the `node_modules` | ||
@@ -20,5 +21,5 @@ Then if you want to use it, you can for example copy and paste this code : | ||
```js | ||
const converter = require("csv") | ||
const converter = require("csv-to-custom-json") | ||
const result = await converter("myfile.csv", ) | ||
const result = await converter("myfile.csv"); | ||
@@ -30,1 +31,3 @@ ``` | ||
> - the name `myfile.csv` can be different, just change it with the correct name of your file | ||
Now you are ready to check Options ! |
@@ -24,3 +24,3 @@ # How to use csv-to-custom-json options | ||
```sh | ||
npm -s run test -- -l test/callBack_force.test.js callBack_force.test.js | ||
npm -s run test -- -n test/callBack_force.test.js callBack_force.test.js | ||
``` | ||
@@ -31,4 +31,4 @@ | ||
> - `-s` is to silent npm | ||
> - `--` is used to pass the `-l` to the `test.js` file | ||
> - `-l` is used to allow only useful log | ||
> - `--` is used to pass the `-n` parameters to the `test.js` file | ||
> - `-n` is used to allow only useful log | ||
> - `test/callBack_force.test.js` and `callBack_force.test.js` are here to demonstrate that you can omit the `test/` in the string | ||
@@ -173,1 +173,35 @@ | ||
</details> | ||
### Private separator | ||
> - name: `privateSeparator` | ||
> - default: `...` | ||
> - value: `string` | ||
This options allow you to change the internal separator of the script. It can be useful if values contain `.` in their names | ||
<details> | ||
<summary>Test</summary> | ||
```sh | ||
npm -s run test test/private_separator.test.js test/private_separator_2.test.js | ||
``` | ||
</details> | ||
### Avoid void line | ||
> - name: `avoidVoidLine` | ||
> - default: `false` | ||
> - value: `boolean` | ||
This options allow you to not parse void line | ||
<details> | ||
<summary>Test</summary> | ||
```sh | ||
npm -s run test test/avoidVoidLine.test.js test/avoidVoidLine2.test.js test/avoidVoidLine3.test.js | ||
``` | ||
</details> |
128
index.js
@@ -0,3 +1,5 @@ | ||
// front-not-used start-block | ||
const fs = require("fs"); | ||
const readline = require("readline"); | ||
// front-not-used end-block | ||
@@ -12,12 +14,20 @@ /* eslint require-await: "off"*/ | ||
// Because if it's "false" -> optionsUser.nameOptions || true -> will be true | ||
const checkOptions = (value, defaultValue) => { | ||
if (typeof value !== "undefined" && value !== null) { | ||
return value; | ||
} else { | ||
return defaultValue; | ||
} | ||
}; | ||
const options = { | ||
arrayParse: typeof optionsUser.arrayParse !== "undefined" && optionsUser.arrayParse !== null ? optionsUser.arrayParse : true, | ||
callBackForce: typeof optionsUser.callBackForce !== "undefined" && optionsUser.callBackForce !== null ? optionsUser.callBackForce : false, | ||
debug: typeof optionsUser.debug !== "undefined" && optionsUser.debug !== null ? optionsUser.debug : false, | ||
error: typeof optionsUser.error !== "undefined" && optionsUser.error !== null ? optionsUser.error : false, | ||
lineCallBack: typeof optionsUser.lineCallBack !== "undefined" && optionsUser.lineCallBack !== null ? optionsUser.lineCallBack : null, | ||
parse: typeof optionsUser.parse !== "undefined" && optionsUser.parse !== null ? optionsUser.parse : true, | ||
separator: typeof optionsUser.separator !== "undefined" && optionsUser.separator !== null ? optionsUser.separator : ",", | ||
privateSeparator: typeof optionsUser.privateSeparator !== "undefined" && optionsUser.privateSeparator !== null ? optionsUser.privateSeparator : "...", | ||
overrideFirstLine: typeof optionsUser.overrideFirstLine !== "undefined" && optionsUser.overrideFirstLine !== null ? optionsUser.overrideFirstLine : false | ||
arrayParse: checkOptions(optionsUser.arrayParse, true), | ||
callBackForce: checkOptions(optionsUser.callBackForce, false), | ||
debug: checkOptions(optionsUser.debug, false), | ||
error: checkOptions(optionsUser.error, false), | ||
lineCallBack: checkOptions(optionsUser.lineCallBack, null), | ||
parse: checkOptions(optionsUser.parse, true), | ||
separator: checkOptions(optionsUser.separator, ","), | ||
privateSeparator: checkOptions(optionsUser.privateSeparator, "..."), | ||
overrideFirstLine: checkOptions(optionsUser.overrideFirstLine, false), | ||
avoidVoidLine: checkOptions(optionsUser.avoidVoidLine, false), | ||
}; | ||
@@ -35,13 +45,24 @@ if (options.debug) { | ||
} | ||
if (!fs.existsSync(pathToFile)) { | ||
if (options.error === "no") { | ||
return []; | ||
// front-not-used start-block | ||
if (typeof pathToFile === "string") { | ||
if (!fs.existsSync(pathToFile)) { | ||
if (options.error === "no") { | ||
return []; | ||
} | ||
throw new Error("Can't access to the file"); | ||
} | ||
throw new Error("Can't access to the file"); | ||
} | ||
// front-not-used end-block | ||
return new Promise((resolve) => { | ||
const lineReader = readline.createInterface({ | ||
crlfDelay: Infinity, | ||
input: fs.createReadStream(pathToFile) | ||
}); | ||
let lineReader; | ||
if (typeof pathToFile == "string") { | ||
// front-not-used start-block | ||
lineReader = readline.createInterface({ | ||
crlfDelay: Infinity, | ||
input: fs.createReadStream(pathToFile) | ||
}); | ||
// front-not-used end-block | ||
} else if (Array.isArray(pathToFile)) { | ||
lineReader = pathToFile; | ||
} | ||
let rows = []; | ||
@@ -121,3 +142,3 @@ let lineCounter = 0; | ||
} else { | ||
currentValue = allValues[index]; | ||
currentValue = allValues[index] || ""; | ||
} | ||
@@ -183,34 +204,10 @@ // Optionnal parse the value | ||
}; | ||
lineReader.on("line", (line) => { | ||
lineReader.pause(); | ||
if (lineCounter === 0) { | ||
if (typeof options.overrideFirstLine !== "undefined" && options.overrideFirstLine !== null && Array.isArray(options.overrideFirstLine)) { | ||
firstLine = options.overrideFirstLine; // check if same length ? | ||
} else { | ||
firstLine = line.split(options.separator); | ||
} | ||
if (typeof schema !== "undefined" && schema !== null) { | ||
rows = createFieldsBinding(schema); | ||
if (options.debug) { | ||
console.log("BINDINGS:", JSON.stringify(rows)); | ||
const clearBuffer = async () => { | ||
for (const oneLine of lineBuffer) { | ||
let parsedLine = {}; | ||
if (options.avoidVoidLine === true) { | ||
if (oneLine === "" || oneLine === "\n" || oneLine === "\r\n") { | ||
continue; | ||
} | ||
} else { | ||
// There is no schema | ||
rows = firstLine.map((element) => ({ | ||
name: element, | ||
path: element | ||
})); | ||
} | ||
lineCounter++; | ||
} else { | ||
lineBuffer.push(line); | ||
} | ||
}); | ||
lineReader.on("close", () => { | ||
resolve(finalJson); | ||
}); | ||
lineReader.on("pause", async () => { | ||
for (const oneLine of lineBuffer) { | ||
let parsedLine = {}; | ||
parsedLine = await parseLine(oneLine); | ||
@@ -232,4 +229,35 @@ if (typeof options.lineCallBack !== "undefined" && typeof options.lineCallBack === "function") { | ||
lineBuffer = []; // Clear the buffer | ||
lineReader.resume(); | ||
}); | ||
}; | ||
const parsefirstLine = async (line) => { | ||
if (typeof options.overrideFirstLine !== "undefined" && options.overrideFirstLine !== null && Array.isArray(options.overrideFirstLine)) { | ||
firstLine = options.overrideFirstLine; // check if same length ? | ||
} else { | ||
firstLine = line.split(options.separator); | ||
} | ||
if (typeof schema !== "undefined" && schema !== null) { | ||
rows = createFieldsBinding(schema); | ||
if (options.debug) { | ||
console.log("BINDINGS:", JSON.stringify(rows)); | ||
} | ||
} else { | ||
// There is no schema | ||
rows = firstLine.map((element) => ({ | ||
name: element, | ||
path: element | ||
})); | ||
} | ||
}; | ||
const reader = async () => { | ||
for await (const line of lineReader) { | ||
if (lineCounter === 0) { | ||
lineCounter++; | ||
await parsefirstLine(line); | ||
} else { | ||
lineBuffer.push(line); | ||
await clearBuffer(); | ||
} | ||
} | ||
resolve(finalJson); | ||
} | ||
reader(); | ||
}); | ||
@@ -236,0 +264,0 @@ }; |
{ | ||
"name": "csv-to-custom-json", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Easily transform your CSV to a custom JSON with cool options", | ||
@@ -34,6 +34,7 @@ "author": "n4n5", | ||
"scripts": { | ||
"test": "node test/test.js" | ||
"test": "node test/test.js", | ||
"build": "node createFile.js" | ||
}, | ||
"browser": "./browser/browser.js", | ||
"main": "index.js" | ||
} | ||
} |
@@ -1,20 +0,17 @@ | ||
# csv-to-custom-json | ||
# csv-to-custom-json · [![npm version](https://img.shields.io/npm/v/csv-to-custom-json.svg)](https://www.npmjs.org/package/csv-to-custom-json) [![dependencies](https://status.david-dm.org/gh/its-just-nans/csv-to-custom-json.svg)](https://david-dm.org/its-just-nans/csv-to-custom-json) [![Build Status](https://travis-ci.com/Its-Just-Nans/csv-to-custom-json.svg?branch=main)](https://travis-ci.com/Its-Just-Nans/csv-to-custom-json) | ||
[![npm version](https://img.shields.io/npm/v/csv-to-custom-json.svg)](https://www.npmjs.org/package/csv-to-custom-json) | ||
Transform your `.csv` file to a custom JSON structure :) ! In browser and NodeJS ! | ||
Transform your `.csv` file to a custom JSON structure :) ! | ||
<details> | ||
<summary>Click to expand</summary> | ||
<!-- TOC --> | ||
- [csv-to-custom-json](#csv-to-custom-json) | ||
- [Simple documentation](#simple-documentation) | ||
- [Simple case](#simple-case) | ||
- [Structure JSON](#structure-json) | ||
- [Options](#options) | ||
- [Documentation](#documentation) | ||
- [Examples](#examples) | ||
- [About](#about) | ||
- [License](#license) | ||
- [Simple documentation](#simple-documentation) | ||
- [Simple case](#simple-case) | ||
- [Structure JSON](#structure-json) | ||
- [Options](#options) | ||
- [Documentation](#documentation) | ||
- [Examples](#examples) | ||
- [Issues](#issues) | ||
- [About](#about) | ||
- [License](#license) | ||
@@ -30,3 +27,3 @@ </details> | ||
```javascript | ||
const parseFile = require("./index"); | ||
const parseFile = require("csv-to-custom-json"); | ||
const parsed = await parseFile(linkFile); | ||
@@ -98,8 +95,4 @@ ``` | ||
For options, when I say `boolean`, in reality, it can be any `true` value of javascript. Same for `false`. | ||
All options are listed in the documentation (you can run [examples](#examples) to help you) ! | ||
Run [examples](#examples) to help you ! | ||
All options are listed in the documentation | ||
## Documentation | ||
@@ -129,2 +122,8 @@ | ||
## Issues | ||
Oh no 😟 ! | ||
Go here [csv-to-custom-json/issues](https://github.com/Its-Just-Nans/csv-to-custom-json/issues) | ||
## About | ||
@@ -134,4 +133,6 @@ | ||
You can discuss here : [csv-to-custom-json/discussions](https://github.com/Its-Just-Nans/csv-to-custom-json/discussions) | ||
## License | ||
Licensed under the MIT License - [LICENSE](LICENSE) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
135
4
41548
13
619