Created
January 25, 2012 19:32
-
-
Save vlucas/1678081 to your computer and use it in GitHub Desktop.
Parsing money out of text strings in various different given formats (Craigslist postings, Facebook wall posts, etc.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPrice(str) { | |
if(str.indexOf('$') !== -1) { | |
// Dollar sign present - it always trumps other number values | |
var reg = /\$([\d,.]+)/g; | |
} else if(/(\d[\d.,]+)/.test(str)) { | |
// Digits with decimals found - more likely to be price than other number values | |
var reg = /(\d[\d.,]+)/g; | |
} else { | |
// Any numbers at all that are surrounded by spaces (fallback; worst accuracy) | |
var reg = /\s+([\d]+)\s+/g; | |
} | |
var prices =[]; | |
var price; | |
// Match padded string so we can always expect spaces before and after | |
while((price = reg.exec(' ' + str + ' '))!=null) { | |
prices.push( parseFloat(price[1]) ); | |
} | |
// Return '0' when no matches are found | |
if(prices.length === 0) { | |
return 0; | |
} | |
// Return minimum price | |
var min = prices[0]; | |
var len = prices.length; | |
for (var i = 1; i < len; i++) if (prices[i] < min) min = prices[i]; | |
// Return formatted as money | |
return min; | |
} | |
describe('Parse money from string', function() { | |
it('should choose number with dollar sign over any other numbers', function() { | |
var msg = "5 pairs girls size 6 with adjustable waist. Easy wear on white and green pair. $8 OBO"; | |
var result = 8; | |
expect(getPrice(msg)).toEqual(result); | |
}); | |
it('should choose lowest number with dollar sign', function() { | |
var msg = "4 child bottoms and 32 child tops paid over $40 want $15 obo"; | |
var result = 15; | |
expect(getPrice(msg)).toEqual(result); | |
}); | |
it('should choose lowest number with no dollar sign if no dollar signs are present', function() { | |
var msg = "Top is small child bottoms are extra small child worn once paid over 40 want 20 obo"; | |
var result = 20; | |
expect(getPrice(msg)).toEqual(result); | |
}); | |
it('should return 0 with no price or number surrounded by spaces', function() { | |
var msg = "Looking for boys 2t shirts old navy or children's place new or gently used please ! Thank u"; | |
var result = 0; | |
expect(getPrice(msg)).toEqual(result); | |
}); | |
it('should choose number with decimal point over any other numbers', function() { | |
var msg = "New only used a few times asking 40.00 OBO text me 4133612"; | |
var result = 40; | |
expect(getPrice(msg)).toEqual(result); | |
}); | |
it('should choose lowest number with decimal point over any other lower numbers', function() { | |
var msg = "Boys size 5 shirts asking 12.00"; | |
var result = 12; | |
expect(getPrice(msg)).toEqual(result); | |
}); | |
it('should choose lowest number when no dollar sign and no decimal point numbers are present', function() { | |
var msg = "Boys size 5t shirts for 7"; | |
var result = 7; | |
expect(getPrice(msg)).toEqual(result); | |
}); | |
it('should return 0 when the text "free" is present with no prices', function() { | |
var msg = "i have a stack of parents magaze, for FREE if anyone wants them"; | |
var result = 0; | |
expect(getPrice(msg)).toEqual(result); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment