conversikit
Advanced tools
Comparing version 1.8.7 to 1.8.8
43
index.js
@@ -154,3 +154,3 @@ const fs = require('fs'); | ||
if (!path.endsWith(".csv")) { | ||
throw new Error("invalid file name") | ||
throw new Error("invalid file name, its must be .csv") | ||
} | ||
@@ -174,8 +174,8 @@ | ||
for (let j = 0; j < csvData.length; j++) { | ||
if (occure) { | ||
if (csvData[j].endsWith('"')) { | ||
concatData += csvData[j]; | ||
obj[JSON.stringify(header[j- dataOccur].split('\r')[0])] = concatData || ""; | ||
obj[JSON.stringify(header[j - dataOccur].split('\r')[0])] = concatData || ""; | ||
concatData = ""; | ||
@@ -197,7 +197,7 @@ occure = false; | ||
if (header[j - dataOccur]) { | ||
obj[JSON.stringify(header[j- dataOccur].split('\r')[0])] = csvData[j] || ""; | ||
obj[JSON.stringify(header[j - dataOccur].split('\r')[0])] = csvData[j] || ""; | ||
} | ||
if(j === csvData.length -1){ | ||
obj[JSON.stringify(header[j- dataOccur].split('\r')[0])] = csvData[j].split('\r')[0] || ""; | ||
if (j === csvData.length - 1) { | ||
obj[JSON.stringify(header[j - dataOccur].split('\r')[0])] = csvData[j].split('\r')[0] || ""; | ||
} | ||
@@ -214,3 +214,34 @@ | ||
} | ||
}, | ||
toConvertTSVToJson: async (path) => { | ||
try { | ||
if (!path.endsWith(".tsv")) { | ||
throw new Error("invalid file name, its must be .tsv") | ||
} | ||
let data = await fs.readFileSync(path, 'utf8'); | ||
const rows = data.split('\r'); | ||
const header = rows[0].split('\t'); | ||
//let create a json data for this tsv file | ||
let jsonArray = []; | ||
for (let i = 1; i < rows.length; i++) { | ||
let csvData = rows[i].split('\t'); | ||
let obj = {}; | ||
for (let j = 0; j < csvData.length; j++) { | ||
// | ||
if (j === 0) { | ||
obj[header[j]] = csvData[j].split('\n')[1] || ""; | ||
continue; | ||
} | ||
if (header[j]) { obj[header[j]] = csvData[j] || ""; } | ||
} | ||
if (obj) jsonArray.push(obj) | ||
} | ||
return jsonArray; | ||
} catch (error) { throw error; } | ||
} | ||
} |
{ | ||
"name": "conversikit", | ||
"version": "1.8.7", | ||
"version": "1.8.8", | ||
"description": "convert anything", | ||
@@ -21,3 +21,29 @@ "main": "index.js", | ||
"JSON-conversion", | ||
"CSV-conversion" | ||
"CSV-conversion", | ||
"JavaScript library", | ||
"Text transformation", | ||
"Data conversion", | ||
"String manipulation", | ||
"Case conversion", | ||
"CSV handling", | ||
"TSV handling", | ||
"Data formatting", | ||
"Text formatting", | ||
"Data transformation", | ||
"Utility library", | ||
"Text utilities", | ||
"JSON utilities", | ||
"CSV to JSON", | ||
"TSV to JSON", | ||
"String case", | ||
"JSON manipulation", | ||
"Data processing", | ||
"Text tools", | ||
"Case formatting", | ||
"Data conversion functions", | ||
"Text manipulation functions", | ||
"CSV to JSON converter", | ||
"TSV to JSON converter", | ||
"Data processing library", | ||
"String utilities" | ||
], | ||
@@ -24,0 +50,0 @@ "repository": { |
@@ -5,4 +5,28 @@ <div align="center"> | ||
**conversikit** is a versatile JavaScript library that provides functions to convert and transform text and data in various ways. Whether you need to change letter casing, convert between JSON and arrays, apply camel case formatting, or handle CSV data, conversikit has you covered. | ||
<p> | ||
<a href="https://www.npmjs.com/package/llama-forms-react" target="_blank"> | ||
<img alt="Version" src="https://img.shields.io/npm/v/conversikit.svg"> | ||
</a> | ||
<a href="https://github.com/nettantra/llama-forms-react#readme" target="_blank"> | ||
<img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" /> | ||
</a> | ||
<a href="https://github.com/nettantra/llama-forms-react/graphs/commit-activity" target="_blank"> | ||
<img alt="Maintenance" src="https://img.shields.io/badge/Maintained%3F-yes-green.svg" /> | ||
</a> | ||
</p> | ||
**CONVERSIKIT** is a versatile JavaScript library that provides a wide range of functions for converting and transforming text and data. Whether you need to change letter casing, convert between different data formats, apply camel case formatting, or handle CSV and TSV data, **CONVERSIKIT** has you covered. | ||
## Overview | ||
**CONVERSIKIT** is designed to simplify data processing and text manipulation tasks in your JavaScript projects. It offers a set of easy-to-use functions, each tailored for specific data transformation needs. | ||
## Features | ||
- Case Conversion: Easily convert text to uppercase, lowercase, or capitalize case to meet your formatting requirements. | ||
- JSON and Array Conversion: Seamlessly switch between JSON and array data structures, making data interchange more convenient. | ||
- Camel Case Formatting: Convert strings to camel case, a common practice in JavaScript coding. | ||
- CSV and TSV Handling: Efficiently work with CSV (Comma-Separated Values) and TSV (Tab-Separated Values) data, both for reading and writing. | ||
## Functions | ||
@@ -42,2 +66,8 @@ | ||
### `toConvertTSVToJson(path)` | ||
- Converts a TSV (Tab-Separated Values) file into JSON format. | ||
This function is designed to take a path to a TSV file as its parameter and convert the content of the TSV file into a JSON format. It reads the TSV file, parses its contents, and returns the data as an array of JSON objects. | ||
- **path (String):** The file path to the TSV file you want to convert to JSON. | ||
## Examples | ||
@@ -137,2 +167,36 @@ | ||
# Convert TSV to JSON | ||
```javascript | ||
const conversikit = require("conversikit"); | ||
const data = await conversikit.toConvertTSVToJson("give your file path name"); // e.g., "data.csv" | ||
Name Age City | ||
John 30 New York | ||
Alice 25 Los Angeles | ||
Bob 35 Chicago | ||
``` | ||
Output: | ||
```javascript | ||
Result = [ | ||
{ | ||
"Name": "John", | ||
"Age": "30", | ||
"City": "New York" | ||
}, | ||
{ | ||
"Name": "Alice", | ||
"Age": "25", | ||
"City": "Los Angeles" | ||
}, | ||
{ | ||
"Name": "Bob", | ||
"Age": "35", | ||
"City": "Chicago" | ||
} | ||
]; | ||
``` | ||
# Author | ||
@@ -139,0 +203,0 @@ |
14603
199
208