-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#2477) - use a Map and Set instead of an object when we have user su…
…pplied values
- Loading branch information
1 parent
6e8a572
commit 63bfe3a
Showing
7 changed files
with
158 additions
and
27 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
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,50 @@ | ||
'use strict'; | ||
exports.Map = global.Map || LazyMap; | ||
exports.Set = global.Set || LazySet; | ||
// based on https://github.com/montagejs/collections | ||
function LazyMap() { | ||
this.store = {}; | ||
} | ||
LazyMap.prototype.mangle = function (key) { | ||
if (typeof key !== "string") { | ||
throw new TypeError("key must be a string but Got " + key); | ||
} | ||
return '$' + key; | ||
}; | ||
LazyMap.prototype.get = function (key) { | ||
var mangled = this.mangle(key); | ||
if (mangled in this.store) { | ||
return this.store[mangled]; | ||
} else { | ||
return void 0; | ||
} | ||
}; | ||
LazyMap.prototype.set = function (key, value) { | ||
var mangled = this.mangle(key); | ||
this.store[mangled] = value; | ||
return true; | ||
}; | ||
LazyMap.prototype.has = function (key) { | ||
var mangled = this.mangle(key); | ||
return mangled in this.store; | ||
}; | ||
LazyMap.prototype.delete = function (key) { | ||
var mangled = this.mangle(key); | ||
if (mangled in this.store) { | ||
delete this.store[mangled]; | ||
return true; | ||
} | ||
return false; | ||
}; | ||
function LazySet() { | ||
this.store = new LazyMap(); | ||
} | ||
LazySet.prototype.add = function (key) { | ||
return this.store.set(key, true); | ||
}; | ||
LazySet.prototype.has = function (key) { | ||
return this.store.has(key); | ||
}; | ||
LazySet.prototype.delete = function (key) { | ||
return this.store.delete(key); | ||
}; |
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
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,67 @@ | ||
'use strict'; | ||
|
||
var adapters = [ | ||
['local', 'http'], | ||
['http', 'http'], | ||
['http', 'local'], | ||
['local', 'local'] | ||
]; | ||
|
||
adapters.forEach(function (adapters) { | ||
describe('test.reserved.js-' + adapters[0] + '-' + adapters[1], function () { | ||
|
||
var dbs = {}; | ||
|
||
beforeEach(function (done) { | ||
dbs.name = testUtils.adapterUrl(adapters[0], 'test_repl'); | ||
dbs.remote = testUtils.adapterUrl(adapters[1], 'test_repl_remote'); | ||
testUtils.cleanup([dbs.name, dbs.remote], done); | ||
}); | ||
|
||
after(function (done) { | ||
testUtils.cleanup([dbs.name, dbs.remote], done); | ||
}); | ||
|
||
it('test docs with reserved javascript ids', function () { | ||
this.timeout(5000); | ||
var db = new PouchDB(dbs.name); | ||
var remote = new PouchDB(dbs.remote); | ||
return db.bulkDocs([ | ||
{_id: 'constructor'}, | ||
{_id: 'toString'}, | ||
{_id: 'valueOf'} | ||
]).then(function () { | ||
return db.allDocs({key: 'constructor'}); | ||
}).then(function (res) { | ||
res.rows.should.have.length(1, 'allDocs with key'); | ||
return db.allDocs({keys: ['constructor']}); | ||
}).then(function (res) { | ||
res.rows.should.have.length(1, 'allDocs with keys'); | ||
return db.allDocs(); | ||
}).then(function (res) { | ||
res.rows.should.have.length(3, 'allDocs empty opts'); | ||
return db.query(function (doc) { | ||
emit(doc._id); | ||
}, {key: 'constructor'}); | ||
}).then(function (res) { | ||
res.rows.should.have.length(1, 'query with key'); | ||
return db.query(function (doc) { | ||
emit(doc._id); | ||
}, {keys: ['constructor']}); | ||
}).then(function (res) { | ||
res.rows.should.have.length(1, 'query with keys'); | ||
return new PouchDB.utils.Promise(function (resolve, reject) { | ||
db.replicate.to(remote).on('complete', resolve).on('error', reject); | ||
}); | ||
}); | ||
}); | ||
|
||
it('can create db with reserved name', function () { | ||
return new PouchDB('constructor').then(function (db) { | ||
return db.info().then(function () { | ||
return db.destroy(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |