Socket
Socket
Sign inDemoInstall

i18next-json-csv-converter

Package Overview
Dependencies
1
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.1 to 0.2.0

62

index.js

@@ -13,7 +13,17 @@ #!/usr/bin/env node

const [, , inFileName, outFileName] = process.argv
const [, , inFileName, outFileName, diffFileName, masterFileName] = process.argv
const inFileNameResolved = path.resolve(inFileName)
const outFileNameResolved = path.resolve(outFileName)
const inFile = fs.readFileSync(inFileNameResolved, { encoding: 'utf8' })
if (inFileNameResolved.endsWith('.json')) {
if (diffFileName) {
const diffFileNameResolved = path.resolve(diffFileName)
const outFile = fs.readFileSync(outFileNameResolved, { encoding: 'utf8' })
let masterFile
if (masterFileName) {
const masterFileNameResolved = path.resolve(masterFileName)
masterFile = fs.readFileSync(masterFileNameResolved, { encoding: 'utf8' })
}
diffFile = diffCsv(inFile, outFile, masterFile)
fs.writeFileSync(diffFileNameResolved, diffFile, { encoding: 'utf8' })
} else if (inFileNameResolved.endsWith('.json')) {
const json = JSON.parse(inFile)

@@ -37,9 +47,7 @@ const csv = json2Csv(json)

const json = {}
const lines = csv.split(/[\r\n]+/)//.map(line => line.split(',').map(value => value.slice(1, -1).replace(/""/g, '"')))
for (const line of lines) {
if (!line) continue
const [path, value] = parseCsvLine(line)
parseCsvLines(csv).forEach(line => {
const [path, value] = line
const pathSplit = path.split(separator)
deepSet(json, pathSplit, value)
}
})
return json

@@ -54,3 +62,3 @@ }

if (typeof value === 'string') {
result.push(`"${fullKey.replace(/"/g, '""')}","${value.replace(/"/g, '""')}"`)
result.push(stringsToCsvLine([fullKey, value]))
} else if (typeof value === 'object' && value) {

@@ -63,2 +71,6 @@ result = result.concat(findKeyValues(value, fullKey, l + 1))

function stringsToCsvLine(line) {
return line.map(s => `"${s.replace(/"/g, '""')}"`).join(',')
}
function deepSet(o, path, value) {

@@ -76,2 +88,11 @@ if (path.length > 1) {

function parseCsvLines(csv) {
const lines = csvToLines(csv)
return lines.filter(Boolean).map(parseCsvLine)
}
function csvToLines(csv) {
return csv.split(/[\r\n]+/)
}
function parseCsvLine(line) {

@@ -114,2 +135,26 @@ const result = []

function diffCsv(oldCsv, newCsv, originalCsv) {
const originalValueByKey = {}
if (originalCsv) {
parseCsvLines(originalCsv).forEach(line => {
const [key, value] = line
originalValueByKey[key] = value
})
}
const diff = require('diff')
let result = []
const d = diff.diffArrays(csvToLines(oldCsv), csvToLines(newCsv))
d.forEach(part => {
part.value.forEach(line => {
if (line) {
if (!part.removed) {
const [key] = parseCsvLine(line)
result.push(`${line},"${part.added ? 'CHANGED' : ''}"${originalCsv ? `,"${originalValueByKey[key] || ''}"` : ''}`)
}
}
})
})
return result.join('\n')
}
module.exports = {

@@ -119,2 +164,3 @@ separator,

csv2Json,
diffCsv,
}

5

package.json
{
"name": "i18next-json-csv-converter",
"version": "0.1.1",
"version": "0.2.0",
"main": "index.js",

@@ -14,3 +14,6 @@ "bin": "index.js",

"test": "ava"
},
"dependencies": {
"diff": "^3.5.0"
}
}

@@ -35,3 +35,14 @@ # i18next-json-csv-converter

### Diff two CSV files
```sh
$ i18next-json-csv-converter ./en-EN-old.csv ./en-EN-new.csv ./en-EN-diff.csv
```
### Diff two CSV files and put the original labels in an extra column
```sh
$ i18next-json-csv-converter ./en-EN-old.csv ./en-EN-new.csv ./en-EN-diff.csv ./hu-HU-new.csv
```
## API

@@ -51,2 +62,6 @@

### diffCsv(oldCsv: string, newCsv: string, originalCsv?: string): string
Takes two CSV strings and outputs a CSV string with an extra `"CHANGED"` column. If the `original` argument is provided, labels with matching keys will be added as an extra column in the output, to aid translators.
## Known issues

@@ -65,2 +80,4 @@

0.1.1 Added license.
0.1.1 Added license.
0.2.0 diffCsv.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc