Skip to content

Instantly share code, notes, and snippets.

@jonchretien
Last active December 18, 2015 00:18
Show Gist options
  • Select an option

  • Save jonchretien/5695568 to your computer and use it in GitHub Desktop.

Select an option

Save jonchretien/5695568 to your computer and use it in GitHub Desktop.
Quick script used for splitting mailing addresses into multiple columns for Excel.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.column {
display: inline-block;
}
</style>
</head>
<body>
<ul id="js-names" class="column"></ul>
<ul id="js-streets" class="column"></ul>
<ul id="js-apartments" class="column"></ul>
<ul id="js-city_states" class="column"></ul>
<ul id="js-zips" class="column"></ul>
<script>
(function() {
'use strict';
/*
original data format (copy and pasted from a Google Spreadsheet):
"John Doe
1224 Main Street
Somewhere, Someplace 65445"
"Susie Sample
180 East 34th Street
Apartment 2D
Brooklyn, NY 11238"
"Greg Guy
234 Walt Avenue
Sunnyvale, CA 65456-7845"
*/
var arr = [
"John Doe| 1224 Main Street| Somewhere, Someplace 65445", "Susie Sample| 180 East 34th Street| Apartment 2D| Brooklyn, NY 11238", "Greg Guy| 234 Walt Avenue| Sunnyvale, CA 65456-7845"
],
apartments = [],
city_states = [],
names = [],
streets = [],
zips = [];
/**
* Initializes app.
*/
function init() {
extractAddressData();
convertResultsToList(names, 'js-names');
convertResultsToList(streets, 'js-streets');
convertResultsToList(apartments, 'js-apartments');
convertResultsToList(city_states, 'js-city_states');
convertResultsToList(zips, 'js-zips');
}
/**
* Splits up addresses and pushes data to separate arrays.
*/
function extractAddressData() {
// create regex's
var namesRegex = /^[A-Za-z][^0-9]+/,
streetsRegex = /\|\s[^a-zA-Z][a-zA-Z0-9\/\s,'-]*/gi,
apartmentsRegex = /(Apartment|Apt|Unit|Number|Suite|#)\s\d*([A-Za-z-]?)+/gi,
cityStatesRegex = /([A-Za-z]+)([\sA-Za-z]+)?,([\sA-Za-z]+)([\sA-Za-z]+)?/gi,
zipCodeRegex = /[0-9]{5}(?!\s)(-[0-9]{4})?/gi;
// cache array length
var len = arr.length;
// loop through addresses and split up content into new arrays
for ( var i=0; i < len; i++ ) {
// debugging help
console.log(arr[i]);
// find names
var strName = arr[i].match(namesRegex)[0];
names.push(strName.trim().substring(0, strName.length - 2));
// find street addresses
var strStreet = arr[i].match(streetsRegex)[0];
streets.push(strStreet.slice(1).trim());
// find apartments and account for N/As
if ( arr[i].search(apartmentsRegex) > 0 ) {
apartments.push(arr[i].match(apartmentsRegex)[0]);
} else {
apartments.push('N/A');
}
// find cities and states
city_states.push(arr[i].match(cityStatesRegex)[0].trim());
// find zip codes
zips.push(arr[i].match(zipCodeRegex)[0]);
}
// log results
console.log('names', names.length, names);
console.log('streets', streets.length, streets);
console.log('apartments', apartments.length, apartments);
console.log('city_states', city_states.length, city_states);
console.log('zips', zips.length, zips);
}
/**
* Creates lists out of address arrays and inserts into DOM.
* {Array} result
* {String} container - DOM id.
*/
function convertResultsToList(result, container) {
// declare variables
var fragment = document.createDocumentFragment(),
list = document.getElementById(container),
h1, len, str;
// create list heading and add to fragment
h1 = document.createElement('h1');
str = list.getAttribute('id').replace(/js-/, '');
h1.innerHTML = str[0].toUpperCase() + str.slice(1);
fragment.appendChild(h1);
// create list items and add to fragment
len = result.length;
for ( var i=0; i < len; i++ ) {
var el = document.createElement('li');
el.setAttribute('class', 'list-item');
el.innerHTML = result[i];
fragment.appendChild(el);
}
// append to DOM
list.appendChild(fragment);
}
init();
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment