pouchdb-collate
Advanced tools
Comparing version 0.2.0 to 1.0.0
!function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.pouchCollate=e():"undefined"!=typeof global?global.pouchCollate=e():"undefined"!=typeof self&&(self.pouchCollate=e())}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
'use strict'; | ||
exports.collate = function (a, b) { | ||
a = exports.normalizeKey(a); | ||
b = exports.normalizeKey(b); | ||
var ai = collationIndex(a); | ||
var bi = collationIndex(b); | ||
if ((ai - bi) !== 0) { | ||
return ai - bi; | ||
} | ||
if (a === null) { | ||
return 0; | ||
} | ||
if (typeof a === 'number') { | ||
return a - b; | ||
} | ||
if (typeof a === 'boolean') { | ||
return a === b ? 0 : (a < b ? -1 : 1); | ||
} | ||
if (typeof a === 'string') { | ||
return stringCollate(a, b); | ||
} | ||
if (Array.isArray(a)) { | ||
return arrayCollate(a, b); | ||
} | ||
if (typeof a === 'object') { | ||
return objectCollate(a, b); | ||
} | ||
} | ||
// couch considers null/NaN/Infinity/-Infinity === undefined, | ||
// for the purposes of mapreduce indexes. also, dates get stringified. | ||
exports.normalizeKey = function (key) { | ||
if (typeof key === 'undefined') { | ||
return null; | ||
} else if (typeof key === 'number') { | ||
if (key === Infinity || key === -Infinity || isNaN(key)) { | ||
return null; | ||
} | ||
} else if (key instanceof Date) { | ||
return key.toJSON(); | ||
} | ||
return key; | ||
} | ||
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]); | ||
var sort = exports.collate(a[i], b[i]); | ||
if (sort !== 0) { | ||
@@ -26,3 +69,3 @@ return sort; | ||
// First sort the keys | ||
var sort = pouchCollate(ak[i], bk[i]); | ||
var sort = exports.collate(ak[i], bk[i]); | ||
if (sort !== 0) { | ||
@@ -32,3 +75,3 @@ return sort; | ||
// if the keys are equal sort the values | ||
sort = pouchCollate(a[ak[i]], b[bk[i]]); | ||
sort = exports.collate(a[ak[i]], b[bk[i]]); | ||
if (sort !== 0) { | ||
@@ -45,2 +88,3 @@ return sort; | ||
// arrays, then objects | ||
// null/undefined/NaN/Infinity/-Infinity are all considered null | ||
function collationIndex(x) { | ||
@@ -58,28 +102,3 @@ var id = ['boolean', 'number', 'string', 'object']; | ||
} | ||
module.exports = pouchCollate; | ||
function pouchCollate(a, b) { | ||
var ai = collationIndex(a); | ||
var bi = collationIndex(b); | ||
if ((ai - bi) !== 0) { | ||
return ai - bi; | ||
} | ||
if (a === null) { | ||
return 0; | ||
} | ||
if (typeof a === 'number') { | ||
return a - b; | ||
} | ||
if (typeof a === 'boolean') { | ||
return a < b ? -1 : 1; | ||
} | ||
if (typeof a === 'string') { | ||
return stringCollate(a, b); | ||
} | ||
if (Array.isArray(a)) { | ||
return arrayCollate(a, b); | ||
} | ||
if (typeof a === 'object') { | ||
return objectCollate(a, b); | ||
} | ||
};; | ||
},{}]},{},[1]) | ||
@@ -86,0 +105,0 @@ (1) |
'use strict'; | ||
exports.collate = function (a, b) { | ||
a = exports.normalizeKey(a); | ||
b = exports.normalizeKey(b); | ||
var ai = collationIndex(a); | ||
var bi = collationIndex(b); | ||
if ((ai - bi) !== 0) { | ||
return ai - bi; | ||
} | ||
if (a === null) { | ||
return 0; | ||
} | ||
if (typeof a === 'number') { | ||
return a - b; | ||
} | ||
if (typeof a === 'boolean') { | ||
return a === b ? 0 : (a < b ? -1 : 1); | ||
} | ||
if (typeof a === 'string') { | ||
return stringCollate(a, b); | ||
} | ||
if (Array.isArray(a)) { | ||
return arrayCollate(a, b); | ||
} | ||
if (typeof a === 'object') { | ||
return objectCollate(a, b); | ||
} | ||
} | ||
// couch considers null/NaN/Infinity/-Infinity === undefined, | ||
// for the purposes of mapreduce indexes. also, dates get stringified. | ||
exports.normalizeKey = function (key) { | ||
if (typeof key === 'undefined') { | ||
return null; | ||
} else if (typeof key === 'number') { | ||
if (key === Infinity || key === -Infinity || isNaN(key)) { | ||
return null; | ||
} | ||
} else if (key instanceof Date) { | ||
return key.toJSON(); | ||
} | ||
return key; | ||
} | ||
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]); | ||
var sort = exports.collate(a[i], b[i]); | ||
if (sort !== 0) { | ||
@@ -25,3 +68,3 @@ return sort; | ||
// First sort the keys | ||
var sort = pouchCollate(ak[i], bk[i]); | ||
var sort = exports.collate(ak[i], bk[i]); | ||
if (sort !== 0) { | ||
@@ -31,3 +74,3 @@ return sort; | ||
// if the keys are equal sort the values | ||
sort = pouchCollate(a[ak[i]], b[bk[i]]); | ||
sort = exports.collate(a[ak[i]], b[bk[i]]); | ||
if (sort !== 0) { | ||
@@ -44,2 +87,3 @@ return sort; | ||
// arrays, then objects | ||
// null/undefined/NaN/Infinity/-Infinity are all considered null | ||
function collationIndex(x) { | ||
@@ -56,32 +100,2 @@ var id = ['boolean', 'number', 'string', 'object']; | ||
} | ||
if (typeof x === 'undefined') { | ||
// CouchDB indexes both null/undefined as null | ||
return 1; | ||
} | ||
} | ||
module.exports = pouchCollate; | ||
function pouchCollate(a, b) { | ||
var ai = collationIndex(a); | ||
var bi = collationIndex(b); | ||
if ((ai - bi) !== 0) { | ||
return ai - bi; | ||
} | ||
if (a === null || typeof a === 'undefined') { | ||
return 0; | ||
} | ||
if (typeof a === 'number') { | ||
return a - b; | ||
} | ||
if (typeof a === 'boolean') { | ||
return a === b ? 0 : (a < b ? -1 : 1); | ||
} | ||
if (typeof a === 'string') { | ||
return stringCollate(a, b); | ||
} | ||
if (Array.isArray(a)) { | ||
return arrayCollate(a, b); | ||
} | ||
if (typeof a === 'object') { | ||
return objectCollate(a, b); | ||
} | ||
};; |
{ | ||
"name": "pouchdb-collate", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -0,3 +1,7 @@ | ||
'use strict'; | ||
var should = require('chai').should(); | ||
var collate = require('../lib'); | ||
var pouchCollate = require('../lib'); | ||
var collate = pouchCollate.collate; | ||
var normalizeKey = pouchCollate.normalizeKey; | ||
@@ -99,2 +103,33 @@ describe('collate',function(){ | ||
}); | ||
}); | ||
describe('normalizeKey',function(){ | ||
it('verify key normalizations', function(){ | ||
var normalizations = [ | ||
[null, null], | ||
[NaN, null], | ||
[undefined, null], | ||
[Infinity, null], | ||
[-Infinity, null], | ||
['', ''], | ||
['foo', 'foo'], | ||
['0', '0'], | ||
['1', '1'], | ||
[0, 0], | ||
[1, 1], | ||
[Number.MAX_VALUE, Number.MAX_VALUE], | ||
[new Date('1982-11-30T00:00:00.000Z'), '1982-11-30T00:00:00.000Z'] // date Thriller was released | ||
]; | ||
normalizations.forEach(function(normalization){ | ||
var original = normalization[0]; | ||
var expected = normalization[1]; | ||
var normalized = normalizeKey(original); | ||
var message = 'check normalization of ' + JSON.stringify(original) + ' to ' + JSON.stringify(expected) + | ||
', got ' + JSON.stringify(normalized);; | ||
should.equal(normalized, expected, message); | ||
}); | ||
}); | ||
}); |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17256
9
603
1