Skip to content

Instantly share code, notes, and snippets.

@AaronO
Created June 19, 2014 22:39
Show Gist options
  • Save AaronO/42df2945ff04be190196 to your computer and use it in GitHub Desktop.
Save AaronO/42df2945ff04be190196 to your computer and use it in GitHub Desktop.
Parse semvers, chapter levels, and hierarchy's of numbers separated by dots to a javascript float
var _ = require('lodash');
var testNumbers = [
'0',
'0.1',
'0.2',
'1',
'1.4',
'1.9.4',
'1.11.4',
'1.10',
'2.1',
'2.34',
'55',
'43',
'33.1',
'2.22',
];
// Turn strings of dot separated numbers into decimal numbers
// the numbers conserve the hierarchical order expressed by the dots
// ASSUMPTION: individual numbers (between dots), do not exceed 1000
function parseDot(x) {
return x.split('.')
.map(function(y) {
return parseInt(y, 10);
})
.reduce(function(accu, y, i) {
return accu + (y/Math.pow(1000, i));
}, 0);
}
console.log(_.sortBy(testNumbers, parseDot));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment