addressit
Version:
Freeform Street Address Parser
2 lines (1 loc) • 8.71 kB
JavaScript
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.addressit=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){"use strict";var reNumeric=/^\d+$/;function Address(text,opts){if(!(this instanceof Address)){return new Address(text)}this.text=text;this.parts=[]}module.exports=Address;var proto=Address.prototype;proto._extractStreetParts=function(startIndex,streetPartsLength){var index=startIndex;var streetParts=[];var numberParts;var parts=this.parts;var testFn=function(){return true};while(index>=0&&testFn()){var alphaPart=isNaN(parseInt(parts[index],10));if(streetParts.length<streetPartsLength||alphaPart){streetParts.unshift(parts.splice(index--,1))}else{if(!numberParts){numberParts=[]}numberParts.unshift(parts.splice(index--,1));testFn=function(){var isAlpha=isNaN(parseInt(parts[index],10));return numberParts?!isAlpha:isAlpha}}}this.number=numberParts?numberParts.join("/"):"";this.street=streetParts.join(" ").replace(/\,/g,"")};proto.clean=function(cleaners){cleaners=cleaners||[];for(var ii=0;ii<cleaners.length;ii++){if(typeof cleaners[ii]=="function"){this.text=cleaners[ii].call(null,this.text)}else if(cleaners[ii]instanceof RegExp){this.text=this.text.replace(cleaners[ii],"")}}return this};proto.extract=function(fieldName,regexes){if(this[fieldName]){return this}var match;var rgxIdx;var ii;var lookups=[];if(typeof regexes=="object"&&typeof regexes.splice=="undefined"){var newRegexes=[];for(var key in regexes){newRegexes[newRegexes.length]=regexes[key];lookups[newRegexes.length-1]=key}regexes=newRegexes}for(ii=this.parts.length-1;ii>=0;ii--){for(rgxIdx=0;rgxIdx<regexes.length;rgxIdx++){if(this[fieldName]){continue}if(ii===0&&fieldName==="state"){if(this.parts.length>1&&this.parts[ii].length<=3){continue}}match=regexes[rgxIdx].exec(this.parts[ii]);if(match){if(match[2]){this.parts.splice(ii,1,match[2])}else{this.parts.splice(ii,1)}this[fieldName]=lookups[rgxIdx]||match[1]}else if(fieldName==="state"){var matchMultiplePart=false;var spacesInMatch=regexes[rgxIdx].source.split("\\s").length;if(spacesInMatch>1){var multiplePart=[];for(var partJoin=ii;partJoin>ii-spacesInMatch&&partJoin>=0;partJoin--){multiplePart.push(this.parts[partJoin])}multiplePart.reverse();multiplePart=multiplePart.join(" ");matchMultiplePart=regexes[rgxIdx].exec(multiplePart);if(matchMultiplePart){if(matchMultiplePart[2]){this.parts.splice(ii-spacesInMatch+1,spacesInMatch,matchMultiplePart[2]);ii-=spacesInMatch+1}else{this.parts.splice(ii-spacesInMatch+1,spacesInMatch);ii-=spacesInMatch+1}this[fieldName]=lookups[rgxIdx]||matchMultiplePart[1]}}}}}return this};proto.extractStreet=function(regexes,reSplitStreet,reNoStreet){var reNumericesque=/^(\d*|\d*\w)$/;var parts=this.parts;var streetPartsLength=2;regexes=regexes||[];function locateBestStreetPart(startIndex){var bestIndex=startIndex;for(var ii=startIndex-1;ii>=0;ii--){for(var rgxIdx=0;rgxIdx<regexes.length;rgxIdx++){if(regexes[rgxIdx].test(parts[ii])&&parts[ii-1]&&!reNumericesque.test(parts[ii-1])){bestIndex=ii;break}}}return bestIndex}for(var partIdx=parts.length;partIdx--;){for(var rgxIdx=0;rgxIdx<regexes.length;rgxIdx++){if(regexes[rgxIdx].test(parts[partIdx])&&partIdx>0){var startIndex=locateBestStreetPart(partIdx);if(reSplitStreet.test(parts[startIndex+1])){streetPartsLength=3;startIndex+=1}if(reNoStreet.test(parts[startIndex])){streetPartsLength=1}this._extractStreetParts(startIndex,streetPartsLength);break}}}return this};proto.finalize=function(){this.regions=this.parts.join(" ").split(/\,\s?/).filter(function(region){return region.length});this.parts=[];return this};proto.split=function(separator){var newParts=this.text.split(separator||" ");this.parts=[];for(var ii=0;ii<newParts.length;ii++){if(newParts[ii]){this.parts[this.parts.length]=newParts[ii]}}return this};proto.toString=function(){var output="";if(this.building){output+=this.building+"\n"}if(this.street){output+=this.number?this.number+" ":"";output+=this.street+"\n"}output+=this.regions.join(", ")+"\n";return output}},{}],2:[function(require,module,exports){"use strict";module.exports=function(input,opts){var parse=(opts||{}).locale||require("./locale/en-US");return parse(input,opts)}},{"./locale/en-US":3}],3:[function(require,module,exports){var parser=require("../parsers/en.js");var extend=require("cog/extend");module.exports=function(input,opts){return parser(input,extend({state:{AL:/(^alabama|^AL$)/i,AK:/(^alaska|^AK$)/i,AS:/(^american\ssamoa|^AS$)/i,AZ:/(^arizona|^AZ$)/i,AR:/(^arkansas|^AR$)/i,CA:/(^california|^CA$)/i,CO:/(^colorado|^CO$)/i,CT:/(^connecticut|^CT$)/i,DE:/(^delaware|^DE$)/i,DC:/(^district\sof\scolumbia|^DC$)/i,FM:/(^federated\sstates\sof\smicronesia|^FM$)/i,FL:/(^florida|^FL$)/i,GA:/(^georgia|^GA$)/i,GU:/(^guam|^GU$)/i,HI:/(^hawaii|^HI$)/i,ID:/(^idaho|^ID$)/i,IL:/(^illinois|^IL$)/i,IN:/(^indiana|^IN$)/i,IA:/(^iowa|^IA$)/i,KS:/(^kansas|^KS$)/i,KY:/(^kentucky|^KY$)/i,LA:/(^louisiana|^LA$)/i,ME:/(^maine|^ME$)/i,MH:/(^marshall\sislands|^MH$)/i,MD:/(^maryland|^MD$)/i,MA:/(^massachusetts|^MA$)/i,MI:/(^michigan|^MI$)/i,MN:/(^minnesota|^MN$)/i,MS:/(^mississippi|^MS$)/i,MO:/(^missouri|^MO$)/i,MT:/(^montana|^MT$)/i,NE:/(^nebraska|^NE$)/i,NV:/(^nevada|^NV$)/i,NH:/(^new\shampshire|^NH$)/i,NJ:/(^new\sjersey|^NJ$)/i,NM:/(^new\smexico|^NM$)/i,NY:/(^new\syork|^NY$)/i,NC:/(^north\scarolina|^NC$)/i,ND:/(^north\sdakota|^ND$)/i,MP:/(^northern\smariana\sislands|^MP$)/i,OH:/(^ohio|^OH$)/i,OK:/(^oklahoma|^OK$)/i,OR:/(^oregon|^OR$)/i,PW:/(^palau|^PW$)/i,PA:/(^pennsylvania|^PA$)/i,PR:/(^puerto\srico|^PR$)/i,RI:/(^rhode\sisland|^RI$)/i,SC:/(^south\scarolina|^SC$)/i,SD:/(^south\sdakota|^SD$)/i,TN:/(^tennessee|^TN$)/i,TX:/(^texas|^TX$)/i,UT:/(^utah|^UT$)/i,VT:/(^vermont|^VT$)/i,VI:/(^virgin\sislands|^VI$)/i,VA:/(^virginia|^VA$)/i,WA:/(^washington|^WA$)/i,WV:/(^west\svirginia|^WV$)/i,WI:/(^wisconsin|^WI$)/i,WY:/(^wyoming|^WY$)/i},country:{USA:/(^UNITED\sSTATES|^U\.?S\.?A?$)/i},rePostalCode:/(^\d{5}$)|(^\d{5}-\d{4}$)/},opts))}},{"../parsers/en.js":6,"cog/extend":4}],4:[function(require,module,exports){"use strict";module.exports=function(target){[].slice.call(arguments,1).forEach(function(source){if(!source){return}for(var prop in source){target[prop]=source[prop]}});return target}},{}],5:[function(require,module,exports){"use strict";module.exports=function(textRegexes){var regexes=[];var reStreetCleaner=/^\^?(.*)\,?\$?$/;var ii;for(ii=textRegexes.length;ii--;){regexes[ii]=new RegExp(textRegexes[ii].replace(reStreetCleaner,"^$1,?$"),"i")}return regexes}},{}],6:[function(require,module,exports){"use strict";var Address=require("../address");var compiler=require("./compiler");var streetRegexes=compiler(["ALLE?Y","APP(ROACH)?","ARC(ADE)?","AV(E|ENUE)?","(BOULEVARD|BLVD)","BROW","BYPA(SS)?","C(AUSE)?WAY","(CIRCUIT|CCT)","CIRC(US)?","CL(OSE)?","CO?PSE","(CORNER|CNR)","(C((OUR)|R)?T|CRT)","CRES(CENT)?","DR(IVE)?","ESP(LANADE)?","F(REE)?WAY","(FRONTAGE|FRNT)","(GLADE|GLD)","GR(EE)?N","(HIGHWAY|HWY)","(LANE|LN)","LINK","LOOP","MALL","MEWS","(PACKET|PCKT)","P(ARA)?DE","(PARKWAY|PKWY)","PL(ACE)?","PROM(ENADE)?","RES(ERVE)?","RISE","R(OA)?D","ROW","SQ(UARE)?","ST(REET)?","STRI?P","TARN","T(ERRA)?CE|TER?R","(THOROUGHFARE|TFRE)","TRACK?","TR(AI)?L","T(RUNK)?WAY","VI?STA","WALK","WA?Y","W(ALK)?WAY","YARD","BROADWAY"]);var reSplitStreet=/^(N|NTH|NORTH|E|EST|EAST|S|STH|SOUTH|W|WST|WEST)\,$/i;var reNoStreet=compiler(["BROADWAY"]).pop();module.exports=function(text,opts){var address=new Address(text,opts);address.clean([function(input){return input.replace(/(\w{2})\./g,"$1")},function(input){return input.replace(/^\s*SHOP\s?(\d*)\,?\s*/i,"$1/")}]).split(/\s/).extract("unit",[/^(?:\#|APT|APARTMENT)\s?(\d+)/,/^(\d+)\/(.*)/]).extractStreet(streetRegexes,reSplitStreet,reNoStreet);if(opts&&opts.state){address.extract("state",opts.state)}if(opts&&opts.country){address.extract("country",opts.country)}if(opts&&opts.rePostalCode){address.extract("postalcode",[opts.rePostalCode])}return address.finalize()}},{"../address":1,"./compiler":5}]},{},[2])(2)});