MCPcopy Create free account
hub / github.com/apache/pouchdb / numToIndexableString

Function numToIndexableString

lib/index.es.js:3533–3570  ·  view source on GitHub ↗
(num)

Source from the content-addressed store, hash-verified

3531// y = exponent (for negative numbers negated) moved so that it's >= 0
3532// z = mantisse
3533function numToIndexableString(num) {
3534
3535 if (num === 0) {
3536 return '1';
3537 }
3538
3539 // convert number to exponential format for easier and
3540 // more succinct string sorting
3541 var expFormat = num.toExponential().split(/e\+?/);
3542 var magnitude = parseInt(expFormat[1], 10);
3543
3544 var neg = num < 0;
3545
3546 var result = neg ? '0' : '2';
3547
3548 // first sort by magnitude
3549 // it's easier if all magnitudes are positive
3550 var magForComparison = ((neg ? -magnitude : magnitude) - MIN_MAGNITUDE);
3551 var magString = padLeft((magForComparison).toString(), '0', MAGNITUDE_DIGITS);
3552
3553 result += SEP + magString;
3554
3555 // then sort by the factor
3556 var factor = Math.abs(parseFloat(expFormat[0])); // [1..10)
3557 /* istanbul ignore next */
3558 if (neg) { // for negative reverse ordering
3559 factor = 10 - factor;
3560 }
3561
3562 var factorStr = factor.toFixed(20);
3563
3564 // strip zeros from the end
3565 factorStr = factorStr.replace(/\.?0+$/, '');
3566
3567 result += SEP + factorStr;
3568
3569 return result;
3570}
3571
3572// create a comparator based on the sort object
3573function 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…