nedb
Advanced tools
Comparing version 1.6.0 to 1.6.1
@@ -148,3 +148,3 @@ /** | ||
criterion = criteria[i]; | ||
compare = criterion.direction * model.compareThings(model.getDotValue(a, criterion.key), model.getDotValue(b, criterion.key)); | ||
compare = criterion.direction * model.compareThings(model.getDotValue(a, criterion.key), model.getDotValue(b, criterion.key), self.db.compareStrings); | ||
if (compare !== 0) { | ||
@@ -151,0 +151,0 @@ return compare; |
@@ -24,2 +24,3 @@ var customUtils = require('./customUtils') | ||
* @param {Number} options.corruptAlertThreshold Optional, threshold after which an alert is thrown if too much data is corrupt | ||
* @param {Function} options.compareStrings Optional, string comparison function that overrides default for sorting | ||
*/ | ||
@@ -49,2 +50,5 @@ function Datastore (options) { | ||
// String comparison function | ||
this.compareStrings = options.compareStrings; | ||
// Persistence handling | ||
@@ -51,0 +55,0 @@ this.persistence = new Persistence({ db: this, nodeWebkitAppName: options.nodeWebkitAppName |
@@ -186,5 +186,8 @@ /** | ||
* Return -1 if a < b, 1 if a > b and 0 if a = b (note that equality here is NOT the same as defined in areThingsEqual!) | ||
* | ||
* @param {Function} _compareStrings String comparing function, returning -1, 0 or 1, overriding default string comparison (useful for languages with accented letters) | ||
*/ | ||
function compareThings (a, b) { | ||
var aKeys, bKeys, comp, i; | ||
function compareThings (a, b, _compareStrings) { | ||
var aKeys, bKeys, comp, i | ||
, compareStrings = _compareStrings || compareNSB; | ||
@@ -204,4 +207,4 @@ // undefined | ||
// Strings | ||
if (typeof a === 'string') { return typeof b === 'string' ? compareNSB(a, b) : -1; } | ||
if (typeof b === 'string') { return typeof a === 'string' ? compareNSB(a, b) : 1; } | ||
if (typeof a === 'string') { return typeof b === 'string' ? compareStrings(a, b) : -1; } | ||
if (typeof b === 'string') { return typeof a === 'string' ? compareStrings(a, b) : 1; } | ||
@@ -208,0 +211,0 @@ // Booleans |
{ | ||
"name": "nedb", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Louis Chatriot", |
@@ -56,2 +56,7 @@ <img src="http://i.imgur.com/9O1xHFb.png" style="width: 25%; height: 25%; float: left;"> | ||
* `corruptAlertThreshold` (optional): between 0 and 1, defaults to 10%. NeDB will refuse to start if more than this percentage of the datafile is corrupt. 0 means you don't tolerate any corruption, 1 means you don't care. | ||
* `compareStrings` (optional): function compareStrings(a, b) compares | ||
strings a and b and return -1, 0 or 1. If specified, it overrides | ||
default string comparison which is not well adapted to non-US characters | ||
in particular accented letters. Native `localCompare` will most of the | ||
time be the right choice | ||
* `nodeWebkitAppName` (optional, **DEPRECATED**): if you are using NeDB from whithin a Node Webkit app, specify its name (the same one you use in the `package.json`) in this field and the `filename` will be relative to the directory Node Webkit uses to store the rest of the application's data (local storage etc.). It works on Linux, OS X and Windows. Now that you can use `require('nw.gui').App.dataPath` in Node Webkit to get the path to the data directory for your application, you should not use this option anymore and it will be removed. | ||
@@ -58,0 +63,0 @@ |
@@ -180,3 +180,3 @@ var should = require('chai').should() | ||
} | ||
cursor.sort({ age: -1 }); | ||
@@ -191,6 +191,31 @@ cursor.exec(function (err, docs) { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("Sorting strings with custom string comparison function", function (done) { | ||
var db = new Datastore({ inMemoryOnly: true, autoload: true | ||
, compareStrings: function (a, b) { return a.length - b.length; } | ||
}); | ||
db.insert({ name: 'alpha' }); | ||
db.insert({ name: 'charlie' }); | ||
db.insert({ name: 'zulu' }); | ||
db.find({}).sort({ name: 1 }).exec(function (err, docs) { | ||
_.pluck(docs, 'name')[0].should.equal('zulu'); | ||
_.pluck(docs, 'name')[1].should.equal('alpha'); | ||
_.pluck(docs, 'name')[2].should.equal('charlie'); | ||
delete db.compareStrings; | ||
db.find({}).sort({ name: 1 }).exec(function (err, docs) { | ||
_.pluck(docs, 'name')[0].should.equal('alpha'); | ||
_.pluck(docs, 'name')[1].should.equal('charlie'); | ||
_.pluck(docs, 'name')[2].should.equal('zulu'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('With an empty collection', function (done) { | ||
@@ -202,15 +227,15 @@ async.waterfall([ | ||
, function (cb) { | ||
var cursor = new Cursor(d); | ||
cursor.sort({ age: 1 }); | ||
cursor.exec(function (err, docs) { | ||
assert.isNull(err); | ||
docs.length.should.equal(0); | ||
cb(); | ||
}); | ||
} | ||
var cursor = new Cursor(d); | ||
cursor.sort({ age: 1 }); | ||
cursor.exec(function (err, docs) { | ||
assert.isNull(err); | ||
docs.length.should.equal(0); | ||
cb(); | ||
}); | ||
} | ||
], done); | ||
}); | ||
it('Ability to chain sorting and exec', function (done) { | ||
var i; | ||
var i; | ||
async.waterfall([ | ||
@@ -229,12 +254,12 @@ function (cb) { | ||
, function (cb) { | ||
var cursor = new Cursor(d); | ||
cursor.sort({ age: -1 }).exec(function (err, docs) { | ||
assert.isNull(err); | ||
// Results are in descending order | ||
for (i = 0; i < docs.length - 1; i += 1) { | ||
assert(docs[i].age > docs[i + 1].age) | ||
} | ||
cb(); | ||
}); | ||
} | ||
var cursor = new Cursor(d); | ||
cursor.sort({ age: -1 }).exec(function (err, docs) { | ||
assert.isNull(err); | ||
// Results are in descending order | ||
for (i = 0; i < docs.length - 1; i += 1) { | ||
assert(docs[i].age > docs[i + 1].age) | ||
} | ||
cb(); | ||
}); | ||
} | ||
], done); | ||
@@ -244,3 +269,3 @@ }); | ||
it('Using limit and sort', function (done) { | ||
var i; | ||
var i; | ||
async.waterfall([ | ||
@@ -259,11 +284,11 @@ function (cb) { | ||
, function (cb) { | ||
var cursor = new Cursor(d); | ||
cursor.sort({ age: -1 }).limit(2).exec(function (err, docs) { | ||
assert.isNull(err); | ||
docs.length.should.equal(2); | ||
docs[0].age.should.equal(89); | ||
docs[1].age.should.equal(57); | ||
cb(); | ||
}); | ||
} | ||
var cursor = new Cursor(d); | ||
cursor.sort({ age: -1 }).limit(2).exec(function (err, docs) { | ||
assert.isNull(err); | ||
docs.length.should.equal(2); | ||
docs[0].age.should.equal(89); | ||
docs[1].age.should.equal(57); | ||
cb(); | ||
}); | ||
} | ||
], done); | ||
@@ -270,0 +295,0 @@ }); |
@@ -816,2 +816,7 @@ var model = require('../lib/model') | ||
it('Can specify custom string comparison function', function () { | ||
model.compareThings('hello', 'bloup', function (a, b) { return a < b ? -1 : 1; }).should.equal(1); | ||
model.compareThings('hello', 'bloup', function (a, b) { return a > b ? -1 : 1; }).should.equal(-1); | ||
}); | ||
}); // ==== End of 'Comparing things' ==== // | ||
@@ -818,0 +823,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1266473
28546
648