csv-to-custom-json
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -0,0 +0,0 @@ #!/usr/bin/env node |
# 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 install](./How-to-install.md) | ||
- [How to use](How-to-use.md) | ||
- [How to Options](How-to-options.md) | ||
- [How to CLI](How-to-CLI.md) | ||
- [How to know more](How-to-know-more.md) |
@@ -19,3 +19,3 @@ # How to use csv-to-custom-json with CLI | ||
> Legend : | ||
> Caption : | ||
> | ||
@@ -33,3 +33,3 @@ > - You can now run it with `npm run cli` | ||
> Legend : | ||
> Caption : | ||
> | ||
@@ -36,0 +36,0 @@ > - You can change `YOUR_OPTIONS...` with corrects options |
@@ -11,3 +11,3 @@ # How to use this node_modules ? | ||
> Lengend : | ||
> Caption : | ||
> | ||
@@ -26,3 +26,3 @@ > - `npm` stands for `Node Packet Manager`, it can be used to download `node_modules` | ||
> Lengend : | ||
> Caption : | ||
> | ||
@@ -29,0 +29,0 @@ > - the name `myfile.csv` can be different, just change it with the correct name of your file |
@@ -17,9 +17,9 @@ # Tricks | ||
] | ||
} | ||
}; | ||
``` | ||
> Legend: | ||
> Caption: | ||
> | ||
> - 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 | ||
> - functions are in an array and can't be identified by a name, so we can't give to it a value parameter, so the function will receive an array with all value of the current line | ||
@@ -26,0 +26,0 @@ ## Array schema Trick |
@@ -17,3 +17,3 @@ # How to use csv-to-custom-json options | ||
- For options, when it's writed `boolean`, in reality, it can be any `true` value of javascript. Same for `false`. | ||
- For options, when it is written `boolean`, in reality, it can be any `true` value of javascript. Same for `false`. | ||
@@ -28,3 +28,3 @@ - command to run test | ||
> Legend : | ||
> Caption : | ||
> | ||
@@ -84,3 +84,3 @@ > - `-s` is to silent npm | ||
This function desactivate the parsing of values: `function`, `int`, `float`, `string` | ||
This function deactivates the parsing of values: `function`, `int`, `float`, `string` | ||
@@ -113,2 +113,4 @@ With this function all is string | ||
npm -s run test test/line_callBack.test.js | ||
npm -s run test test/line_callBack_value.test.js | ||
npm -s run test test/line_callBack_force.test.js | ||
``` | ||
@@ -176,19 +178,2 @@ | ||
### 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 | ||
@@ -195,0 +180,0 @@ |
# How to use csv-to-custom-json | ||
## In NodeJS | ||
## With requirejs | ||
@@ -19,5 +19,5 @@ Just import the function and use it ! | ||
## In browser | ||
## With ESM import | ||
We nned to import the front version : | ||
We need to import the front version : | ||
@@ -40,3 +40,3 @@ ```js | ||
> Legend : | ||
> Caption : | ||
> | ||
@@ -69,6 +69,6 @@ > - the difference with the normal file : | ||
}; | ||
const result = await converter("myfile.csv"); | ||
const result = await converter("myfile.csv", schema); | ||
``` | ||
> Legend : | ||
> Caption : | ||
> | ||
@@ -93,3 +93,3 @@ > - ad you can see the schema can contains function, or string with the type | ||
}; | ||
const result = await converter("myfile.csv"); | ||
const result = await converter("myfile.csv", schema); | ||
``` | ||
@@ -99,2 +99,2 @@ | ||
If you want to see and use options check that documentation : [How-to-options](https://github.com/Its-Just-Nans/csv-to-custom-json/wiki/How-to-options) | ||
If you want to see and use options check that documentation : [How-to-options](./How-to-options.md) |
48
front.js
@@ -26,3 +26,2 @@ | ||
separator: checkOptions(optionsUser.separator, ","), | ||
privateSeparator: checkOptions(optionsUser.privateSeparator, "..."), | ||
overrideFirstLine: checkOptions(optionsUser.overrideFirstLine, false), | ||
@@ -56,7 +55,7 @@ avoidVoidLine: checkOptions(optionsUser.avoidVoidLine, false), | ||
const createFieldsBinding = function (schemaObject, startPath = "") { | ||
const createFieldsBinding = function (schemaObject, startPath = []) { | ||
let bindings = []; | ||
for (const oneElement in schemaObject) { | ||
if (Object.prototype.hasOwnProperty.call(schemaObject, oneElement)) { | ||
const path = startPath === "" ? `${oneElement}` : `${startPath}${options.privateSeparator}${oneElement}`; | ||
const path = startPath.length === 0 ? [oneElement] : [...startPath, oneElement]; | ||
if (typeof schemaObject[oneElement] === "object" || Array.isArray(schemaObject[oneElement])) { | ||
@@ -67,15 +66,16 @@ if (Array.isArray(schemaObject[oneElement])) { | ||
path: path, | ||
type: "helper-array" | ||
type: "helper-array", | ||
}); | ||
} | ||
bindings = [ | ||
...bindings, | ||
...createFieldsBinding(schemaObject[oneElement], path) | ||
]; | ||
bindings = [...bindings, ...createFieldsBinding(schemaObject[oneElement], path)]; | ||
} else { | ||
if (Array.isArray(schemaObject) && options.arrayParse && firstLine.includes(schemaObject[oneElement])) { | ||
if ( | ||
Array.isArray(schemaObject) && | ||
options.arrayParse && | ||
firstLine.includes(schemaObject[oneElement]) | ||
) { | ||
bindings.push({ | ||
name: schemaObject[oneElement], | ||
path: path, | ||
value: "string" | ||
value: "string", | ||
}); | ||
@@ -87,3 +87,3 @@ } else { | ||
path: path, | ||
value: schemaObject[oneElement] | ||
value: schemaObject[oneElement], | ||
}); | ||
@@ -95,3 +95,3 @@ } else { | ||
type: "static", | ||
value: schemaObject[oneElement] | ||
value: schemaObject[oneElement], | ||
}); | ||
@@ -120,3 +120,3 @@ } | ||
const onePathName = oneRow.name; | ||
const allPath = onePathRow.split(options.privateSeparator); | ||
const allPath = onePathRow; | ||
let currentValue = null; | ||
@@ -129,6 +129,8 @@ if (typeof oneRow.type === "undefined" || oneRow.type === null) { | ||
} else { | ||
currentValue = allValues[index] || ""; | ||
if (index < allValues.length) { | ||
currentValue = allValues[index]; | ||
} | ||
} | ||
// Optionnal parse the value | ||
if (options.parse === true) { | ||
if (options.parse === true && currentValue != null && currentValue != "") { | ||
if (typeof schemaValue !== "undefined") { | ||
@@ -186,3 +188,3 @@ if (schemaValue === "int") { | ||
} else { | ||
obj[onePathRow] = currentValue; | ||
obj[onePathRow[0]] = currentValue; | ||
} | ||
@@ -203,3 +205,3 @@ } | ||
const resCallback = await options.lineCallBack(parsedLine, oneLine); | ||
if (typeof resCallBack === "undefined" && resCallback === null) { | ||
if ((typeof resCallBack === "undefined" && resCallback === null) || resCallback === undefined) { | ||
if (options.callBackForce) { | ||
@@ -212,2 +214,4 @@ parsedLine = resCallback; | ||
} | ||
} else { | ||
parsedLine = resCallback; | ||
} | ||
@@ -220,3 +224,7 @@ } | ||
const parsefirstLine = async (line) => { | ||
if (typeof options.overrideFirstLine !== "undefined" && options.overrideFirstLine !== null && Array.isArray(options.overrideFirstLine)) { | ||
if ( | ||
typeof options.overrideFirstLine !== "undefined" && | ||
options.overrideFirstLine !== null && | ||
Array.isArray(options.overrideFirstLine) | ||
) { | ||
firstLine = options.overrideFirstLine; // check if same length ? | ||
@@ -235,3 +243,3 @@ } else { | ||
name: element, | ||
path: element | ||
path: [element], | ||
})); | ||
@@ -251,3 +259,3 @@ } | ||
resolve(finalJson); | ||
} | ||
}; | ||
reader(); | ||
@@ -254,0 +262,0 @@ }); |
50
index.js
@@ -29,3 +29,2 @@ // front-not-used start-block | ||
separator: checkOptions(optionsUser.separator, ","), | ||
privateSeparator: checkOptions(optionsUser.privateSeparator, "..."), | ||
overrideFirstLine: checkOptions(optionsUser.overrideFirstLine, false), | ||
@@ -61,3 +60,3 @@ avoidVoidLine: checkOptions(optionsUser.avoidVoidLine, false), | ||
crlfDelay: Infinity, | ||
input: fs.createReadStream(pathToFile) | ||
input: fs.createReadStream(pathToFile), | ||
}); | ||
@@ -74,7 +73,7 @@ // front-not-used end-block | ||
const createFieldsBinding = function (schemaObject, startPath = "") { | ||
const createFieldsBinding = function (schemaObject, startPath = []) { | ||
let bindings = []; | ||
for (const oneElement in schemaObject) { | ||
if (Object.prototype.hasOwnProperty.call(schemaObject, oneElement)) { | ||
const path = startPath === "" ? `${oneElement}` : `${startPath}${options.privateSeparator}${oneElement}`; | ||
const path = startPath.length === 0 ? [oneElement] : [...startPath, oneElement]; | ||
if (typeof schemaObject[oneElement] === "object" || Array.isArray(schemaObject[oneElement])) { | ||
@@ -85,15 +84,16 @@ if (Array.isArray(schemaObject[oneElement])) { | ||
path: path, | ||
type: "helper-array" | ||
type: "helper-array", | ||
}); | ||
} | ||
bindings = [ | ||
...bindings, | ||
...createFieldsBinding(schemaObject[oneElement], path) | ||
]; | ||
bindings = [...bindings, ...createFieldsBinding(schemaObject[oneElement], path)]; | ||
} else { | ||
if (Array.isArray(schemaObject) && options.arrayParse && firstLine.includes(schemaObject[oneElement])) { | ||
if ( | ||
Array.isArray(schemaObject) && | ||
options.arrayParse && | ||
firstLine.includes(schemaObject[oneElement]) | ||
) { | ||
bindings.push({ | ||
name: schemaObject[oneElement], | ||
path: path, | ||
value: "string" | ||
value: "string", | ||
}); | ||
@@ -105,3 +105,3 @@ } else { | ||
path: path, | ||
value: schemaObject[oneElement] | ||
value: schemaObject[oneElement], | ||
}); | ||
@@ -113,3 +113,3 @@ } else { | ||
type: "static", | ||
value: schemaObject[oneElement] | ||
value: schemaObject[oneElement], | ||
}); | ||
@@ -138,3 +138,3 @@ } | ||
const onePathName = oneRow.name; | ||
const allPath = onePathRow.split(options.privateSeparator); | ||
const allPath = onePathRow; | ||
let currentValue = null; | ||
@@ -147,6 +147,8 @@ if (typeof oneRow.type === "undefined" || oneRow.type === null) { | ||
} else { | ||
currentValue = allValues[index] || ""; | ||
if (index < allValues.length) { | ||
currentValue = allValues[index]; | ||
} | ||
} | ||
// Optionnal parse the value | ||
if (options.parse === true) { | ||
if (options.parse === true && currentValue != null && currentValue != "") { | ||
if (typeof schemaValue !== "undefined") { | ||
@@ -204,3 +206,3 @@ if (schemaValue === "int") { | ||
} else { | ||
obj[onePathRow] = currentValue; | ||
obj[onePathRow[0]] = currentValue; | ||
} | ||
@@ -221,3 +223,3 @@ } | ||
const resCallback = await options.lineCallBack(parsedLine, oneLine); | ||
if (typeof resCallBack === "undefined" && resCallback === null) { | ||
if ((typeof resCallBack === "undefined" && resCallback === null) || resCallback === undefined) { | ||
if (options.callBackForce) { | ||
@@ -230,2 +232,4 @@ parsedLine = resCallback; | ||
} | ||
} else { | ||
parsedLine = resCallback; | ||
} | ||
@@ -238,3 +242,7 @@ } | ||
const parsefirstLine = async (line) => { | ||
if (typeof options.overrideFirstLine !== "undefined" && options.overrideFirstLine !== null && Array.isArray(options.overrideFirstLine)) { | ||
if ( | ||
typeof options.overrideFirstLine !== "undefined" && | ||
options.overrideFirstLine !== null && | ||
Array.isArray(options.overrideFirstLine) | ||
) { | ||
firstLine = options.overrideFirstLine; // check if same length ? | ||
@@ -253,3 +261,3 @@ } else { | ||
name: element, | ||
path: element | ||
path: [element], | ||
})); | ||
@@ -269,3 +277,3 @@ } | ||
resolve(finalJson); | ||
} | ||
}; | ||
reader(); | ||
@@ -272,0 +280,0 @@ }); |
{ | ||
"name": "csv-to-custom-json", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Easily transform your CSV to a custom JSON with cool options", | ||
@@ -5,0 +5,0 @@ "author": "n4n5", |
@@ -1,3 +0,5 @@ | ||
# 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) | ||
# 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) | ||
Looking for Python version ? Check here : [https://github.com/Its-Just-Nans/csv-to-custom-json-python](https://github.com/Its-Just-Nans/csv-to-custom-json-python) | ||
Transform your `.csv` file to a custom JSON structure :) ! In browser and NodeJS ! | ||
@@ -8,11 +10,12 @@ | ||
- [Simple documentation](#simple-documentation) | ||
- [Simple case](#simple-case) | ||
- [Structure JSON](#structure-json) | ||
- [Options](#options) | ||
- [Documentation](#documentation) | ||
- [Examples](#examples) | ||
- [Issues](#issues) | ||
- [About](#about) | ||
- [License](#license) | ||
- [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) | ||
- [Issues](#issues) | ||
- [About](#about) | ||
- [License](#license) | ||
@@ -58,3 +61,3 @@ </details> | ||
This program allow you to create complex strucured JSON, like this : | ||
This program allow you to create complex structured JSON, like this : | ||
@@ -86,3 +89,3 @@ ```javascript | ||
To use options, you need to add a third paramters which is an object with options. | ||
To use options, you need to add a third parameters which is an object with options. | ||
@@ -101,14 +104,4 @@ Example : | ||
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) | ||
A whole documentation is available on [./docs](./docs) | ||
And if you really really like it, you can even clone it with : | ||
```sh | ||
git clone https://github.com/Its-Just-Nans/csv-to-custom-json.wiki.git | ||
``` | ||
> Legend : | ||
> | ||
> - Just "wow" | ||
## Examples | ||
@@ -115,0 +108,0 @@ |
Sorry, the diff of this file is not supported yet
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
635
0
39680
128