Comparing version 1.6.1 to 1.7.0
@@ -54,3 +54,3 @@ /* jshint node: true */ | ||
numberParts = []; | ||
} // if | ||
} | ||
@@ -68,3 +68,3 @@ // add the current part to the building parts | ||
}; | ||
} // if..else | ||
} | ||
} // while | ||
@@ -95,3 +95,3 @@ | ||
} | ||
} // for | ||
} | ||
@@ -110,6 +110,9 @@ return this; | ||
proto.extract = function(fieldName, regexes) { | ||
// skip fields which have already been parsed | ||
if (this[fieldName]) { return this; } | ||
var match; | ||
var rgxIdx; | ||
var ii; | ||
var value; | ||
var lookups = []; | ||
@@ -132,4 +135,18 @@ | ||
// iterate over the unit regexes and test them against the various parts | ||
for (rgxIdx = 0; rgxIdx < regexes.length; rgxIdx++) { | ||
for (ii = this.parts.length; ii >= 0; ii-- ) { | ||
for (ii = this.parts.length-1; ii >= 0; ii--) { | ||
for (rgxIdx = 0; rgxIdx < regexes.length; rgxIdx++) { | ||
// skip fields which have already been parsed | ||
if (this[fieldName]){ continue; } | ||
// do not consider the first token for an abbreviated 'state' field | ||
if (ii === 0 && fieldName === 'state'){ | ||
// only where there are more than one token and the first token | ||
// is less than or equal to three characters in length. | ||
if ( this.parts.length > 1 && this.parts[ii].length <= 3 ) { | ||
continue; | ||
} | ||
} | ||
// execute regex against part | ||
match = regexes[rgxIdx].exec(this.parts[ii]); | ||
@@ -139,14 +156,18 @@ | ||
if (match) { | ||
// if we have a 2nd capture group, then replace the item with | ||
// the text of that group | ||
if (match[2]) { | ||
// if we have a 2nd capture group, then replace the item with | ||
// the text of that group | ||
this.parts.splice(ii, 1, match[2]); | ||
} else { | ||
// otherwise, just remove the element from parts | ||
this.parts.splice(ii, 1); | ||
} | ||
// otherwise, just remove the element | ||
else { | ||
this.parts.splice(ii, 1); | ||
} // if..else | ||
value = lookups[rgxIdx] || match[1]; | ||
} else if (fieldName === 'state' && value === undefined) { | ||
// set the field | ||
this[fieldName] = lookups[rgxIdx] || match[1]; | ||
} | ||
// special case for states | ||
// @todo: add code comments | ||
else if (fieldName === 'state') { | ||
var matchMultiplePart = false; | ||
@@ -174,14 +195,12 @@ var spacesInMatch = regexes[rgxIdx].source.split('\\s').length; | ||
ii -= spacesInMatch + 1; | ||
} // if..else | ||
} | ||
value = lookups[rgxIdx] || matchMultiplePart[1]; | ||
// set the field | ||
this[fieldName] = lookups[rgxIdx] || matchMultiplePart[1]; | ||
} | ||
} | ||
} // if | ||
} // for | ||
} // for | ||
} | ||
} | ||
} | ||
// update the field value | ||
this[fieldName] = value; | ||
return this; | ||
@@ -222,5 +241,5 @@ }; | ||
break; | ||
} // if | ||
} // for | ||
} // for | ||
} | ||
} | ||
} | ||
@@ -253,5 +272,5 @@ return bestIndex; | ||
break; | ||
} // if | ||
} // for | ||
} // for | ||
} | ||
} | ||
} | ||
@@ -292,4 +311,4 @@ return this; | ||
this.parts[this.parts.length] = newParts[ii]; | ||
} // if | ||
} // for | ||
} | ||
} | ||
@@ -309,3 +328,3 @@ return this; | ||
output += this.building + '\n'; | ||
} // if | ||
} | ||
@@ -312,0 +331,0 @@ if (this.street) { |
@@ -6,3 +6,3 @@ { | ||
"author": "Damon Oehlman <damon.oehlman@gmail.com>", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"stability": "stable", | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -144,1 +144,20 @@ var test = require('tape'); | ||
})); | ||
// Only parse the state once, do not modify 'Mt Tabor Park' to remove 'MT'. | ||
test('Mt Tabor Park, 6220 SE Salmon St, Portland, OR 97215, USA', expect({ | ||
number: '6220', | ||
street: 'SE Salmon St', | ||
state: 'OR', | ||
country: 'USA', | ||
regions: ['Mt Tabor Park', 'Portland', '97215'] | ||
})); | ||
// Do not parse the first token as a state abbreviation | ||
test('Mt Tabor Park', expect({ | ||
regions: ['Mt Tabor Park'] | ||
})); | ||
// Parse the first token as a state abbreviation when only one token present | ||
test('Mt', expect({ | ||
state: 'MT' | ||
})); |
Sorry, the diff of this file is not supported yet
93483
1631