-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(daleharvey/pouchdb#1658) - add indexable string
Following daleharvey/pouchdb#1658 and pouchdb/mapreduce#12, this adds the toIndexableString method, which allows us to emulate CouchDB's standard collation to a reasonable degree of fidelity (e.g. no ICU ordering for strings).
- Loading branch information
1 parent
32a5ce7
commit 720e6c0
Showing
3 changed files
with
314 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
|
||
function pad(str, padWith, upToLength) { | ||
var padding = ''; | ||
var targetLength = upToLength - str.length; | ||
while (padding.length < targetLength) { | ||
padding += padWith; | ||
} | ||
return padding; | ||
} | ||
|
||
exports.padLeft = function (str, padWith, upToLength) { | ||
var padding = pad(str, padWith, upToLength); | ||
return padding + str; | ||
}; | ||
|
||
exports.padRight = function (str, padWith, upToLength) { | ||
var padding = pad(str, padWith, upToLength); | ||
return str + padding; | ||
}; | ||
|
||
exports.stringLexCompare = function (a, b) { | ||
|
||
var aLen = a.length; | ||
var bLen = b.length; | ||
|
||
var i; | ||
for (i = 0; i < aLen; i++) { | ||
if (i === bLen) { | ||
// b is shorter substring of a | ||
return 1; | ||
} | ||
var aChar = a.charAt(i); | ||
var bChar = b.charAt(i); | ||
if (aChar !== bChar) { | ||
return aChar < bChar ? -1 : 1; | ||
} | ||
} | ||
|
||
if (aLen < bLen) { | ||
// a is shorter substring of b | ||
return -1; | ||
} | ||
|
||
return 0; | ||
}; | ||
|
||
/* | ||
* returns the decimal form for the given integer, i.e. writes | ||
* out all the digits (in base-10) instead of using scientific notation | ||
*/ | ||
exports.intToDecimalForm = function (int) { | ||
|
||
var isNeg = int < 0; | ||
var result = ''; | ||
|
||
do { | ||
var remainder = isNeg ? -Math.ceil(int % 10) : Math.floor(int % 10); | ||
|
||
result = remainder + result; | ||
int = isNeg ? Math.ceil(int / 10) : Math.floor(int / 10); | ||
} while (int); | ||
|
||
|
||
if (isNeg && result !== '0') { | ||
result = '-' + result; | ||
} | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters