Skip to content

making tablesorter size parser more robust #1234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions web/js/tablesorter.parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,32 @@ $.tablesorter.addParser({
return false;
},
format: function (s) {
var parts = s.match(/^([\d\.]+) ?(\w*)$/);
var num = parts[1];
/*
* This correctly handles thousand separator
* in a big number (either ',' or ' ' or none)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd maybe add bunch of concrete examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added them

*
* In our case there is a little gap between 1000 and 1023 which
* is still smaller than the next order unit. This should accept all
* values like:
* 1,000 or 1 000 or 1,023
*
* However it is more generic just in case. It should not have trouble
* with:
* 1,000,123,133.235
* 1 000.4564
* and with even mispelled numbers:
* 1,00,345,0.123 (wrong number of digits between the separator)
* 13,456 13 45.1234 (mixed separators)
* 1000,123 (wrong number of digits between the separator)
* 1,123534435,134547435.165165165 (again)
*/
var parts = s.match(/^(\d{1,3}(?:[, ]?\d{1,3})*(?:\.\d+)?|\.\d+) ?(\w*)$/);

if (parts === null || parts.length < 3) {
return 0;
}

var num = parts[1].replace(/[, ]/g, "")
var unit = parts[2];

// convert to bytes
Expand Down