pouchdb-collate
Advanced tools
Comparing version 0.0.0 to 0.1.0
'use strict'; | ||
var arrayCollate = require('./array'); | ||
var stringCollate = require('./string'); | ||
var objectCollate = require('./object'); | ||
function arrayCollate(a, b) { | ||
var len = Math.min(a.length, b.length); | ||
for (var i = 0; i < len; i++) { | ||
var sort = pouchCollate(a[i], b[i]); | ||
if (sort !== 0) { | ||
return sort; | ||
} | ||
} | ||
return (a.length === b.length) ? 0 : | ||
(a.length > b.length) ? 1 : -1; | ||
} | ||
function stringCollate(a, b) { | ||
// See: https://github.com/daleharvey/pouchdb/issues/40 | ||
// This is incompatible with the CouchDB implementation, but its the | ||
// best we can do for now | ||
return (a === b) ? 0 : ((a > b) ? 1 : -1); | ||
} | ||
function objectCollate(a, b) { | ||
var ak = Object.keys(a), bk = Object.keys(b); | ||
var len = Math.min(ak.length, bk.length); | ||
for (var i = 0; i < len; i++) { | ||
// First sort the keys | ||
var sort = pouchCollate(ak[i], bk[i]); | ||
if (sort !== 0) { | ||
return sort; | ||
} | ||
// if the keys are equal sort the values | ||
sort = pouchCollate(a[ak[i]], b[bk[i]]); | ||
if (sort !== 0) { | ||
return sort; | ||
} | ||
} | ||
return (ak.length === bk.length) ? 0 : | ||
(ak.length > bk.length) ? 1 : -1; | ||
} | ||
// The collation is defined by erlangs ordered terms | ||
// the atoms null, true, false come first, then numbers, strings, | ||
// arrays, then objects | ||
var collationIndex = function(x) { | ||
function collationIndex(x) { | ||
var id = ['boolean', 'number', 'string', 'object']; | ||
@@ -20,5 +53,5 @@ if (id.indexOf(typeof x) !== -1) { | ||
} | ||
}; | ||
module.exports = function(a, b) { | ||
} | ||
module.exports = pouchCollate; | ||
function pouchCollate(a, b) { | ||
var ai = collationIndex(a); | ||
@@ -25,0 +58,0 @@ var bi = collationIndex(b); |
{ | ||
"name": "pouchdb-collate", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2380
3
76