Skip to content

Instantly share code, notes, and snippets.

@tuncatunc
Created August 17, 2017 04:50
Show Gist options
  • Save tuncatunc/35e5449905159928e718d82c06bc66da to your computer and use it in GitHub Desktop.
Save tuncatunc/35e5449905159928e718d82c06bc66da to your computer and use it in GitHub Desktop.
const fs = require('fs')
const JSONStream = require('JSONStream');
var es = require('event-stream');
const filePath = './location-history.json'
const fileOutputPath = './transform-location-history.js'
const fileStream = fs.createReadStream(filePath);
const fileOutputStream = fs.createWriteStream(fileOutputPath)
let index = 0;
const transformer = (data) => {
const location = {
latitude: data.latitudeE7 / 10000000,
longitude: data.longitudeE7 / 10000000
};
let result = JSON.stringify(location) + ',';
if (index === 0) {
result = 'const locationHistory = [' + result
}
index++;
if (index < 100)
fileOutputStream.write(result);
}
const end = () => {
const finish = ']; export default locationHistory\n'
fileOutputStream.write(finish, () => {
fileOutputStream.close()
})
console.log(`${index} objects are written to file`)
}
fileStream
.pipe(JSONStream.parse('locations.*'))
.pipe(es.through(transformer, end))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment