MCPcopy
hub / github.com/apache/pouchdb / numToIndexableString

Function numToIndexableString

lib/index-browser.js:3698–3735  ·  view source on GitHub ↗
(num)

Source from the content-addressed store, hash-verified

3696// y = exponent (for negative numbers negated) moved so that it's >= 0
3697// z = mantisse
3698function numToIndexableString(num) {
3699
3700 if (num === 0) {
3701 return '1';
3702 }
3703
3704 // convert number to exponential format for easier and
3705 // more succinct string sorting
3706 var expFormat = num.toExponential().split(/e\+?/);
3707 var magnitude = parseInt(expFormat[1], 10);
3708
3709 var neg = num < 0;
3710
3711 var result = neg ? '0' : '2';
3712
3713 // first sort by magnitude
3714 // it's easier if all magnitudes are positive
3715 var magForComparison = ((neg ? -magnitude : magnitude) - MIN_MAGNITUDE);
3716 var magString = padLeft((magForComparison).toString(), '0', MAGNITUDE_DIGITS);
3717
3718 result += SEP + magString;
3719
3720 // then sort by the factor
3721 var factor = Math.abs(parseFloat(expFormat[0])); // [1..10)
3722 /* istanbul ignore next */
3723 if (neg) { // for negative reverse ordering
3724 factor = 10 - factor;
3725 }
3726
3727 var factorStr = factor.toFixed(20);
3728
3729 // strip zeros from the end
3730 factorStr = factorStr.replace(/\.?0+$/, '');
3731
3732 result += SEP + factorStr;
3733
3734 return result;
3735}
3736
3737// create a comparator based on the sort object
3738function createFieldSorter(sort) {

Callers 1

indexifyFunction · 0.70

Calls 2

padLeftFunction · 0.70
toStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…