csv-to-custom-json
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -215,3 +215,3 @@ const fs = require("fs"); | ||
const resCallback = await options.lineCallBack(parsedLine, oneLine); | ||
if (typeof resCallBack !== "undefined" && resCallback !== null) { | ||
if (typeof resCallBack === "undefined" && resCallback === null) { | ||
if (options.callBackForce) { | ||
@@ -218,0 +218,0 @@ parsedLine = resCallback; |
{ | ||
"name": "csv-to-custom-json", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Easily transform your CSV to a custom JSON with cool options", | ||
@@ -5,0 +5,0 @@ "author": "n4n5", |
128
README.md
# 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 :) ! | ||
@@ -10,23 +12,14 @@ | ||
- [csv-to-custom-json](#csv-to-custom-json) | ||
- [Documentation](#documentation) | ||
- [Simple documentation](#simple-documentation) | ||
- [Simple case](#simple-case) | ||
- [Structure JSON](#structure-json) | ||
- [Options](#options) | ||
- [Debug](#debug) | ||
- [Separator](#separator) | ||
- [Parse](#parse) | ||
- [Line Call Back](#line-call-back) | ||
- [Call Back Force](#call-back-force) | ||
- [Array Parse](#array-parse) | ||
- [Override First Line](#override-first-line) | ||
- [Tricks](#tricks) | ||
- [Array Trick](#array-trick) | ||
- [Array schema Trick](#array-schema-trick) | ||
- [Documentation](#documentation) | ||
- [Examples](#examples) | ||
- [About](#about) | ||
- [Licence](#licence) | ||
- [License](#license) | ||
</details> | ||
## Documentation | ||
## Simple documentation | ||
@@ -105,105 +98,22 @@ ### Simple case | ||
For options, when I say `boolean`, in reality, it can be any `true` value of javascript. Same for `false` | ||
For options, when I say `boolean`, in reality, it can be any `true` value of javascript. Same for `false`. | ||
You can see [examples](#examples) to help you ! | ||
Run [examples](#examples) to help you ! | ||
#### Debug | ||
All options are listed in the documentation | ||
> - name: `debug` | ||
> - default: `false` | ||
> - value: boolean: `true` or `false` | ||
## Documentation | ||
This options show the parsed result of your schema (can be useful sometimes) | ||
A whole documentation is available on GitHub : [https://github.com/Its-Just-Nans/csv-to-custom-json/wiki](https://github.com/Its-Just-Nans/csv-to-custom-json/wiki) | ||
This options also allow log from the function (example, a mistake) | ||
And if you really really like it, you can even clone it with : | ||
#### Separator | ||
> - name: `separator` | ||
> - default: `,` | ||
> - values: string | ||
`.csv` stands for "Comma Separated Values", but if you're a rebel, this options is made for you :) | ||
#### Parse | ||
> - name: `parse` | ||
> - default: `true` | ||
> - value: boolean: `true` or `false` | ||
This function desactivate the parsing of values: `function`, `int`, `float`, `string` | ||
With this function all is string | ||
#### Line Call Back | ||
> - name: `lineCallBack` | ||
> - default: `null` | ||
> - value: function (async or not) | ||
It activate the callBack after each line, can be useful if ou want to do a insert in database (for example) | ||
#### Call Back Force | ||
> - name: `callBackForce` | ||
> - default: `false` | ||
> - value: boolean: `true` or `false` | ||
This options allow you to force taking the result of the call back even if it's `undefined` or `null` | ||
#### Array Parse | ||
> - name: `arrayParse` | ||
> - default: `true` | ||
> - value: boolean: `true` or `false` | ||
This options allow you to disable the parsing in an array. | ||
#### Override First Line | ||
> - name: `overrideFirstLine` | ||
> - default: `false` | ||
> - value: `array of string` or `false` | ||
This options allow you to override the first line. | ||
## Tricks | ||
### Array Trick | ||
If you have an simple array (not with object), the function can even parse your fields | ||
```javascript | ||
const schema = { | ||
num1: [ | ||
"num4", | ||
"text", | ||
function (allValues) { | ||
return `toto${allValues.toString()}`; | ||
}, | ||
() => "arrow" | ||
] | ||
} | ||
```sh | ||
git clone https://github.com/Its-Just-Nans/csv-to-custom-json.wiki.git | ||
``` | ||
> Legend: | ||
> Legend : | ||
> | ||
> - by default, `num4` in the firstLine will be parsed and replace by the corresponding value. | ||
> - functions are in an array and can't be identified by a name, so we can't give to it a value paramter, so the function will receive an array with all value of the current line | ||
> - Just "wow" | ||
### Array schema Trick | ||
Fun-fact : schema can even be an array ! | ||
```javascript | ||
const schema = [ | ||
"num4", | ||
"text", | ||
function (allValues) { | ||
return `toto${allValues.toString()}`; | ||
}, | ||
() => "arrow" | ||
]; | ||
``` | ||
## Examples | ||
@@ -217,3 +127,3 @@ | ||
And see `test.js` to know what code is used | ||
And see `*.test.js` files to know which code is used ! | ||
@@ -224,4 +134,4 @@ ## About | ||
## Licence | ||
## License | ||
TODO | ||
Licensed under the MIT License - [LICENSE](LICENSE) |
421
test/test.js
@@ -1,394 +0,49 @@ | ||
const parseFile = require("../index"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
/* eslint no-unused-expressions: "off"*/ | ||
/* eslint sort-keys: "off"*/ | ||
/* eslint require-await: "off"*/ | ||
const test = async () => { | ||
let doLog = true; | ||
let listOfFiles = fs.readdirSync(__dirname); | ||
listOfFiles = listOfFiles.filter((name) => name.endsWith("test.js") && name !== "test.js"); | ||
// Output params | ||
const doLog = true; | ||
let woaw = true; | ||
woaw = false; | ||
const allFunc = []; | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple.csv"; | ||
doLog ? console.log(`- Normal \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: "", | ||
num2: "", | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple.csv"; | ||
doLog ? console.log(`- Normal with debug output \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: "", | ||
num2: "", | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema, { | ||
Debug: true | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple.csv"; | ||
doLog ? console.log(`- No schema \n"${linkFile}"`) : ""; | ||
return parseFile(linkFile); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple.csv"; | ||
doLog ? console.log(`- No schema but options \n"${linkFile}"`) : ""; | ||
return parseFile(linkFile, null, { error: "no" }); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_customSeparator.csv"; | ||
doLog ? console.log(`Custom Separator "${linkFile}"`) : ""; | ||
const schema = { | ||
num1: "", | ||
num2: "", | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema, { | ||
separator: ";" | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple.csv"; | ||
doLog ? console.log(`- CallBack on item \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: "", | ||
num2(item) { | ||
return `callBack${item}`; | ||
}, | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple.csv"; | ||
doLog ? console.log(`- CallBack on item with async callBack \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: "", | ||
async num2(item) { | ||
// eslint-disable-next-line no-unused-vars | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(`callBack${item}`); | ||
}, 500); | ||
// Only 500 ms because the test will be too long :/ | ||
}); | ||
}, | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple.csv"; | ||
doLog ? console.log(`- CallBack after line \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: "", | ||
num2: "", | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema, { | ||
lineCallBack: async (parsedLine, sourceLine) => { | ||
doLog ? console.log(JSON.stringify(parsedLine), sourceLine, "lineCallBack") : null; | ||
if (typeof args !== "undefined") { | ||
if (args.includes("-n")) { | ||
args.splice(args.indexOf("-n"), 1); | ||
doLog = false; | ||
} | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple.csv"; | ||
doLog ? console.log(`- Parse Value (default) \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: "int", | ||
num2: "float", | ||
num3: "string" | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple.csv"; | ||
doLog ? console.log(`- Don't parse Value (force) \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: "int", | ||
num2: "float", | ||
num3: "string" | ||
}; | ||
return parseFile(linkFile, schema, { | ||
parse: false | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- More complexe structure \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: { | ||
num4: "" | ||
}, | ||
num2: "", | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- More complexe double same name \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: { | ||
num4: { | ||
num4: "string" | ||
} | ||
}, | ||
num2: "", | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- More complexe struct \n"${linkFile}"`) : ""; | ||
const schema = { | ||
hello: { | ||
uno: { | ||
dos: { | ||
tres: { | ||
num4: "string" | ||
} | ||
if (args.length > 0) { | ||
console.log(args); | ||
listOfFiles = []; | ||
for (const oneArg of args) { | ||
if (!oneArg.startsWith("-")) { | ||
listOfFiles.push(oneArg.replace("test/", "")); | ||
} | ||
} | ||
}, | ||
bonjour: { | ||
un: { | ||
deux: { | ||
trois: { | ||
num2: "string", | ||
num1: "int" | ||
} | ||
} | ||
} | ||
}, | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Array \n"${linkFile}"`) : ""; | ||
const schema = { | ||
hello: [ | ||
{ | ||
num4: "int", | ||
num1: "string" | ||
} | ||
], | ||
num3: "" | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Array \n"${linkFile}"`) : ""; | ||
const schema = { | ||
hello: [ | ||
{ | ||
num4: "int", | ||
num1: "string" | ||
} | ||
], | ||
hello2: [ | ||
{ | ||
num4: "int", | ||
num1: [ | ||
{ | ||
num3: "string" | ||
} | ||
] | ||
} | ||
] | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Array \n"${linkFile}"`) : ""; | ||
const schema = { | ||
hello: [ | ||
{ | ||
num4: "int", | ||
num1: "string" | ||
} | ||
], | ||
hello2: [ | ||
[ | ||
{ | ||
num3: "string" | ||
} | ||
] | ||
] | ||
}; | ||
return parseFile(linkFile, schema); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Array in object\n"${linkFile}"`) : ""; | ||
const schema = { | ||
hello: [ | ||
{ | ||
num4: "int", | ||
num1: "string" | ||
} | ||
], | ||
hello2: [ | ||
{ | ||
num4: "int", | ||
num3(value) { | ||
return `hey${value}`; | ||
}, | ||
num1: [ | ||
"num4", | ||
"text", | ||
function (allValues) { | ||
return `toto${allValues.toString()}`; | ||
}, | ||
() => "arrow" | ||
] | ||
} | ||
] | ||
}; | ||
return parseFile(linkFile, schema, { | ||
debug: true | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Array \n"${linkFile}"`) : ""; | ||
const schema = { | ||
num1: [ | ||
"num4", | ||
"text", | ||
function (allValues) { | ||
return `toto${allValues.toString()}`; | ||
}, | ||
() => "arrow" | ||
] | ||
}; | ||
return parseFile(linkFile, schema, { | ||
debug: "max" | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Array schema \n"${linkFile}"`) : ""; | ||
const schema = [ | ||
"num4", | ||
"text", | ||
function (allValues) { | ||
return `toto${allValues.toString()}`; | ||
}, | ||
() => "arrow" | ||
]; | ||
return parseFile(linkFile, schema, { | ||
debug: true | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Array schema with an object \n"${linkFile}"`) : ""; | ||
const schema = [ | ||
"num4", | ||
"text", | ||
function (allValues) { | ||
return `toto${allValues.toString()}`; | ||
}, | ||
() => "arrow", | ||
{ | ||
staticValue: "value", | ||
num1: "int" | ||
} | ||
]; | ||
return parseFile(linkFile, schema, { | ||
debug: true | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Array schema with an object and an array\n"${linkFile}"`) : ""; | ||
const schema = [ | ||
"num4", | ||
"text", | ||
[ | ||
"arrayHereLol", | ||
[ | ||
"andHereLol", | ||
{ | ||
obj: "lol", | ||
num4: "int" | ||
} | ||
for (const oneName of listOfFiles) { | ||
if (fs.existsSync(oneName) || fs.existsSync(`test/${oneName}`)) { | ||
const testFile = require(path.join(__dirname, oneName)); | ||
const res = await testFile.test(doLog); | ||
try { | ||
if (JSON.stringify(res) == JSON.stringify(testFile.result)) { | ||
console.log("OK"); | ||
} else { | ||
console.log(JSON.stringify(res, null, 4)); | ||
throw ""; | ||
} | ||
] | ||
], | ||
function (allValues) { | ||
return `toto${allValues.toString()}`; | ||
}, | ||
() => "arrow", | ||
{ | ||
staticValue: "value", | ||
num1: "int" | ||
} | ||
]; | ||
return parseFile(linkFile, schema, { | ||
debug: true | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Use private internal separator\n"${linkFile}"`) : ""; | ||
const schema = { | ||
num4: { | ||
num1: "string", | ||
"num1...": "string", | ||
num2: "string", | ||
num3: "int", | ||
hello4: (value) => { | ||
return `The number 4 is ${value}`; | ||
} catch (error) { | ||
console.error(error); | ||
throw new Error("NOT OK"); | ||
} | ||
} else { | ||
console.error(`CANNOT ACCESS TO "${oneName}"`); | ||
//throw new Error(`CANNOT ACCESS TO ${oneName}`); | ||
} | ||
}; | ||
// "num1..." will be not deisplayed | ||
return parseFile(linkFile, schema, { | ||
debug: true, | ||
privateSeparator: "...." // we change the privateSeparator to four points (it can be any string) | ||
}); | ||
}); | ||
allFunc.push(async () => { | ||
const linkFile = "test/simple_complexe.csv"; | ||
doLog ? console.log(`- Overide the first line\n"${linkFile}"`) : ""; | ||
const newFirstLine = ["hello1", "hello2", "hello3", "hello4"]; | ||
const schema = { | ||
num4: { | ||
num4: "string", | ||
hello1: "string", | ||
hello2: "int", | ||
hello3: "float", | ||
hello4: (value) => { | ||
return `The number 4 is ${value}`; | ||
} | ||
} | ||
}; | ||
return parseFile(linkFile, schema, { | ||
overrideFirstLine: newFirstLine, | ||
debug: true | ||
}); | ||
}); | ||
} | ||
}; | ||
(async () => { | ||
for (const oneTestFunc of allFunc) { | ||
doLog ? console.log("------------------") : null; | ||
const res = await oneTestFunc(); | ||
doLog ? console.log(woaw ? JSON.stringify(res, null, 4) : JSON.stringify(res)) : null; | ||
} | ||
console.log("------------------"); | ||
console.log("OK"); | ||
})(); | ||
let [nodePath, filePath, ...args] = process.argv; | ||
test(args); |
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
64807
49
1790
134
6
1