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

Function numToIndexableString

lib/index-browser.es.js:3694–3731  ·  view source on GitHub ↗
(num)

Source from the content-addressed store, hash-verified

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