Parser: named numbers parser ignores adjacent puncutation. Fixes #890

This commit is contained in:
Mottie 2015-04-30 11:56:54 -05:00
parent b3203505cc
commit 9ccd51451f
3 changed files with 9 additions and 7 deletions

View File

@ -1,2 +1,2 @@
/*! Parser: namedNumbers - updated 10/26/2014 (v2.18.0) */
!function(a){"use strict";var b,c,d={negative:["negative","minus"],numbers:{zero:0,one:1,two:2,three:3,four:4,five:5,six:6,seven:7,eight:8,nine:9,ten:10,eleven:11,twelve:12,thirteen:13,fourteen:14,fifteen:15,sixteen:16,seventeen:17,eighteen:18,nineteen:19,twenty:20,thirty:30,forty:40,fourty:40,fifty:50,sixty:60,seventy:70,eighty:80,ninety:90},hundred:"hundred",powers:{thousand:1e3,million:1e6,billion:1e9,trillion:1e12,quadrillion:1e15,quintillion:1e18,sextillion:1e21,septillion:1e24,octillion:1e27,nonillion:1e30,decillion:1e33,undecillion:1e36,duodecillion:1e39,tredecillion:1e42,quattuordecillion:1e45,quindecillion:1e48,sexdecillion:1e51,septendecillion:1e54,octodecillion:1e57,novemdecillion:1e60,vigintillion:1e63,unvigintillion:1e66,duovigintillion:1e69,trevigintillion:1e72,quattuorvigintillion:1e75,quinvigintillion:1e78,sexvigintillion:1e81,septenvigintillion:1e84,octovigintillion:1e87,novemvigintillion:1e90,trigintillion:1e93,untrigintillion:1e96,duotrigintillion:1e99,googl:1e100}},e=new RegExp("("+d.negative.join("|")+")"),f=function(e,f){var g=d.numbers.hasOwnProperty(e)?d.numbers[e]:null,h=d.powers.hasOwnProperty(e)?d.powers[e]:null;g||isNaN(e)||(g=a.tablesorter.formatFloat(e||"",f)),null!==g?c+=g:e===d.hundred?c*=100:null!==h&&(b+=c*h,c=0)};a.tablesorter.addParser({id:"namedNumbers",is:function(){return!1},format:function(g,h){b=0,c=0;var i,j=(g||"").split(/[\s-]+/),k=j.length;for(i=0;k>i;i++)f(j[i].toLowerCase(),h);return b=(b+c)*(g.match(e)?-1:1),b||d.numbers.hasOwnProperty(g)?b:a.tablesorter.formatFloat(g||"",h)},type:"numeric"})}(jQuery);
!function(a){"use strict";var b,c,d={negative:["negative","minus"],numbers:{zero:0,one:1,two:2,three:3,four:4,five:5,six:6,seven:7,eight:8,nine:9,ten:10,eleven:11,twelve:12,thirteen:13,fourteen:14,fifteen:15,sixteen:16,seventeen:17,eighteen:18,nineteen:19,twenty:20,thirty:30,forty:40,fourty:40,fifty:50,sixty:60,seventy:70,eighty:80,ninety:90},hundred:"hundred",powers:{thousand:1e3,million:1e6,billion:1e9,trillion:1e12,quadrillion:1e15,quintillion:1e18,sextillion:1e21,septillion:1e24,octillion:1e27,nonillion:1e30,decillion:1e33,undecillion:1e36,duodecillion:1e39,tredecillion:1e42,quattuordecillion:1e45,quindecillion:1e48,sexdecillion:1e51,septendecillion:1e54,octodecillion:1e57,novemdecillion:1e60,vigintillion:1e63,unvigintillion:1e66,duovigintillion:1e69,trevigintillion:1e72,quattuorvigintillion:1e75,quinvigintillion:1e78,sexvigintillion:1e81,septenvigintillion:1e84,octovigintillion:1e87,novemvigintillion:1e90,trigintillion:1e93,untrigintillion:1e96,duotrigintillion:1e99,googl:1e100}},e=new RegExp("("+d.negative.join("|")+")"),f=function(e,f){var g=e.replace(/[,."']/g,""),h=a.tablesorter.formatFloat(e||"",f),i=d.powers.hasOwnProperty(g)?d.powers[g]:null;h="number"==typeof h?h:d.numbers.hasOwnProperty(g)?d.numbers[g]:null,null!==h?c+=h:g===d.hundred?c*=100:null!==i&&(b+=c*i,c=0)};a.tablesorter.addParser({id:"namedNumbers",is:function(){return!1},format:function(g,h){b=0,c=0;var i,j=(g||"").split(/[\s-]+/),k=j.length;for(i=0;k>i;i++)f(j[i].toLowerCase(),h);return b=(b+c)*(g.match(e)?-1:1),b||d.numbers.hasOwnProperty(g)?b:a.tablesorter.formatFloat(g||"",h)},type:"numeric"})}(jQuery);

View File

@ -43,6 +43,7 @@
<p class="tip">
<em>NOTE!</em>
<ul>
<li>In <span class="version updated">v2.21.6</span>, this parser will now ignore commas, periods and quotes adjacent to the named number word (e.g. "10 million, three...").</li>
<li>This parser will convert named numbers into appropriate values so they are sorted correctly.</li>
<li>Named numbers include values:
<ul>
@ -76,7 +77,7 @@
</tr>
<tr>
<td>one hundred and fifty five</td>
<td>10 million three hundred sixty five thousand four hundred and ninety one</td>
<td>10 million, three hundred sixty five thousand, four hundred and ninety one</td>
</tr>
<tr>
<td>negative twelve</td>

View File

@ -81,12 +81,13 @@
},
result, group,
negativeRegex = new RegExp('(' + named.negative.join('|') + ')'),
calc = function ( word, table ) {
var num = named.numbers.hasOwnProperty( word ) ? named.numbers[ word ] : null,
calc = function ( rawWord, table ) {
// remove extra characters that might be next to the word
var word = rawWord.replace( /[,."']/g, '' ),
// formatFloat will deal with the commas & decimals in the number format
num = $.tablesorter.formatFloat( rawWord || '', table ),
power = named.powers.hasOwnProperty( word ) ? named.powers[ word ] : null;
if ( !num && !isNaN( word ) ) {
num = $.tablesorter.formatFloat( word || '', table );
}
num = typeof num === 'number' ? num : named.numbers.hasOwnProperty( word ) ? named.numbers[ word ] : null;
if ( num !== null ) {
group += num;
} else if ( word === named.hundred ) {