Skip to content

Instantly share code, notes, and snippets.

@heypoom
Last active January 2, 2019 08:04
Show Gist options
  • Save heypoom/fc6398acc17be6283e7e945441278c35 to your computer and use it in GitHub Desktop.
Save heypoom/fc6398acc17be6283e7e945441278c35 to your computer and use it in GitHub Desktop.
Fixes the incorrect latin-1 encoding for Thai JSON files. Put in ~/bin/fix-json-encoding
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
function traverse(o, func) {
for (let i in o) {
o[i] = func(i, o[i])
if (o[i] && typeof o[i] == 'object') {
//going one step down in the object tree!!
traverse(o[i], func)
}
}
}
// argv[0] and [1] is used for ['node', 'fix-encoding']
let [_, __, inputPath, outputPath] = process.argv
if (!inputPath) {
console.error('Usage: fix-encoding <input-file> <output-file?>')
process.exit(1)
}
if (!outputPath) {
outputPath = inputPath
console.log('> Overwriting the input file...')
}
const file = fs.readFileSync(path.resolve(inputPath))
const object = JSON.parse(file.toString())
console.log('> Fixing Encoding of', inputPath)
traverse(object, (key, value) => {
if (typeof value === 'string') {
const txt = Buffer.from(value, 'latin1').toString()
// console.log(txt)
return txt
}
return value
})
fs.writeFileSync(path.resolve(outputPath), JSON.stringify(object, null, 2))
console.log('> Files with correct encoding is written to', outputPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment