Skip to content

Instantly share code, notes, and snippets.

@hswolff
Created November 11, 2015 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hswolff/6d8340fa56fc0a538c42 to your computer and use it in GitHub Desktop.
Save hswolff/6d8340fa56fc0a538c42 to your computer and use it in GitHub Desktop.
Simple script to parse addresses from one form to another. requires at least node 4.0
"use strict";
// usage:
// cat test.csv | node addresses.js > done.csv
const headings = `name,street address,street address,city,state,country,zip`;
process.stdout.write(headings + '\n');
const deliminator = `"`;
let addresses = [];
function formatAddress(address) {
let lines = address.split('\n');
let output;
if (lines.length === 3) {
let name = lines[0].trim();
let address1 = lines[1].trim();
let address2 = '';
let country = '';
if (address1.indexOf(',') > -1) {
let streetAddressParts = address1.split(',');
address1 = streetAddressParts[0];
address2 = streetAddressParts[1];
}
let addressLine = lines[2];
let addressParts = addressLine.split(' ');
let zip = addressParts.pop();
let city = addressParts[0];
let state = addressParts[1];
let addressPartLength = addressParts.length;
if (addressPartLength > 2) {
let halfAddress = Math.round(addressPartLength / 2);
city = [];
state = [];
for (let i = 0; i < addressPartLength; i++) {
if (i < halfAddress) {
city.push(addressParts[i]);
} else {
state.push(addressParts[i]);
}
}
city = city.join(' ');
state = state.join(' ');
}
city = city.replace(/,/, '');
if (zip[0] === '0') {
zip = `XOXO-${zip}`;
}
output = `${name},${address1},${address2},${city},${state},${country},${zip}`
} else {
output = lines.join(' ');
}
process.stdout.write(output + '\n');
}
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
let chunkLength = chunk.length;
let newAddress;
for (let i = 0; i < chunkLength; i++) {
let char = chunk[i];
if (char === deliminator) {
if (!newAddress) {
newAddress = '';
} else if (newAddress.length < 5) {
// hack to skip bad line breaks.
newAddress = '';
} else {
addresses.push(newAddress);
formatAddress(newAddress);
newAddress = '';
}
} else {
newAddress += char;
}
}
}
});
Bob and Mary Woodsman 1111 Big Lane Springfield, Massachusettes 10101
Lou and Patrick Meryl 36 Lampshade Drive Portland Oregon 010101
Liz & Victor Beerman 1 Sutton Lane St. Louis, Missouri 101011
Sandra Go 100 Candy Street, Apartment 1 Boston, Massachusetts 10101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment