mongojs-hooks
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -68,2 +68,5 @@ var mongojs = require("mongojs"); | ||
sanitise: function (object) { | ||
if (_.isString(object)) | ||
return object.replace(/\./g, "U+FF0E").replace(/\$/g, "U+FF04"); | ||
Object.keys(object).forEach(function (key) { | ||
@@ -73,3 +76,3 @@ var sKey = key; | ||
if (/[.|$]/.test(key)) { | ||
sKey = key.replace(/\./g, "U+FF0E").replace(/\$/g, "U+FF04"); | ||
sKey = mongojs.util.sanitise(key); | ||
object[sKey] = object[key]; | ||
@@ -89,2 +92,5 @@ delete object[key]; | ||
unsanitise: function (object) { | ||
if (_.isString(object)) | ||
return object.replace(/U\+FF0E/g, ".").replace(/U\+FF04/g, "$"); | ||
Object.keys(object).forEach(function (sKey) { | ||
@@ -94,3 +100,3 @@ var key = sKey; | ||
if (/(U\+FF0E|U\+FF04)/.test(sKey)) { | ||
key = sKey.replace(/U\+FF0E/g, ".").replace(/U\+FF04/g, "$"); | ||
key = mongojs.util.unsanitise(sKey); | ||
object[key] = object[sKey]; | ||
@@ -97,0 +103,0 @@ delete object[sKey]; |
{ | ||
"name": "mongojs-hooks", | ||
"description": "Wrap monogjs in pre and post hooks", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"main": "./mongojs-hooks", | ||
@@ -6,0 +6,0 @@ "author": { |
require("should"); | ||
var sinon = require("sinon"); | ||
var stream = require("stream"); | ||
var mongo = require("../mongojs-hooks"); | ||
@@ -215,3 +216,46 @@ var mongojs = require("mongojs"); | ||
}); | ||
describe("when streaming", function () { | ||
var findStub; | ||
beforeEach(function () { | ||
//findStub = sinon.stub(mongojs.Collection.prototype, "find").callsArg(1); | ||
}); | ||
afterEach(function () { | ||
//findStub.restore(); | ||
}); | ||
it("with post() hook should get called before next transform in pipeline", function (done) { | ||
var tests = mongo.collection("tests"); | ||
var postStub = sinon.stub(); | ||
tests.post("find", function (next) { | ||
console.log("HERE", arguments); | ||
// mongojs.Collection.prototype.find.calledWith({ test: "property" }).should.be.ok; | ||
// postStub(); | ||
next(); | ||
}); | ||
var transform = stream.Transform({ objectMode: true }); | ||
transform._transform = function (object, enc, next) { | ||
console.log("trans", object); | ||
next(); | ||
}; | ||
// db.collection("tests").find({ test: "property" }, function () { | ||
// postStub.callCount.should.equal(1); | ||
// done(); | ||
// }); | ||
db.collection("tests").find({ test: "property" }) | ||
.pipe(transform) | ||
.on("finish", done); | ||
}); | ||
}); | ||
}); | ||
}); |
196
test/util.js
@@ -6,60 +6,68 @@ require("should"); | ||
describe("sanitise()", function () { | ||
it("with an object with plain keys should not change the key", function () { | ||
var object = { | ||
abcdefghiklmnopqrstuvwxyz: "just fine" | ||
}; | ||
mongo.util.sanitise(object).should.have.property("abcdefghiklmnopqrstuvwxyz"); | ||
describe("when a string", function () { | ||
it("contains a '.' should replace with 'U+FF0E'", function () { | ||
mongo.util.sanitise("hey.there").should.equal("heyU+FF0Ethere"); | ||
}); | ||
}); | ||
it("with a key containing a '.' should replace with 'U+FF0E'", function () { | ||
var object = { | ||
"test.this": "not good" | ||
}; | ||
describe("when an object", function () { | ||
it("contains plain keys should not change the key", function () { | ||
var object = { | ||
abcdefghiklmnopqrstuvwxyz: "just fine" | ||
}; | ||
mongo.util.sanitise(object).should.have.property("testU+FF0Ethis"); | ||
}); | ||
mongo.util.sanitise(object).should.have.property("abcdefghiklmnopqrstuvwxyz"); | ||
}); | ||
it("with a key containing multiple '.' chars should replace all with 'U+FF0E'", function () { | ||
var object = { | ||
"test.this.another.here": "not good" | ||
}; | ||
it("with a key containing a '.' should replace with 'U+FF0E'", function () { | ||
var object = { | ||
"test.this": "not good" | ||
}; | ||
mongo.util.sanitise(object).should.have.property("testU+FF0EthisU+FF0EanotherU+FF0Ehere"); | ||
}); | ||
mongo.util.sanitise(object).should.have.property("testU+FF0Ethis"); | ||
}); | ||
it("with a key containing a '$' should replace with 'U+FF04'", function () { | ||
var object = { | ||
"test$this": "not good" | ||
}; | ||
it("with a key containing multiple '.' chars should replace all with 'U+FF0E'", function () { | ||
var object = { | ||
"test.this.another.here": "not good" | ||
}; | ||
mongo.util.sanitise(object).should.have.property("testU+FF04this"); | ||
}); | ||
mongo.util.sanitise(object).should.have.property("testU+FF0EthisU+FF0EanotherU+FF0Ehere"); | ||
}); | ||
it("with a key containing multiple '$' chars should replace all with 'U+FF04'", function () { | ||
var object = { | ||
"test$this$another$here": "not good" | ||
}; | ||
it("with a key containing a '$' should replace with 'U+FF04'", function () { | ||
var object = { | ||
"test$this": "not good" | ||
}; | ||
mongo.util.sanitise(object).should.have.property("testU+FF04thisU+FF04anotherU+FF04here"); | ||
}); | ||
mongo.util.sanitise(object).should.have.property("testU+FF04this"); | ||
}); | ||
it("with a key containing both '.' and '$' should replace them respectively", function () { | ||
var object = { | ||
"here's a $ and now a .": "what an unlikely key!" | ||
}; | ||
it("with a key containing multiple '$' chars should replace all with 'U+FF04'", function () { | ||
var object = { | ||
"test$this$another$here": "not good" | ||
}; | ||
mongo.util.sanitise(object).should.have.property("here's a U+FF04 and now a U+FF0E"); | ||
}); | ||
mongo.util.sanitise(object).should.have.property("testU+FF04thisU+FF04anotherU+FF04here"); | ||
}); | ||
it("with a sub-object key contains a '.' should replace with 'U+FF0E'", function () { | ||
var object = { | ||
"test.one": { | ||
"test.two": "sub-object" | ||
} | ||
}; | ||
it("with a key containing both '.' and '$' should replace them respectively", function () { | ||
var object = { | ||
"here's a $ and now a .": "what an unlikely key!" | ||
}; | ||
var sanitised = mongo.util.sanitise(object); | ||
sanitised.should.have.property("testU+FF0Eone"); | ||
sanitised["testU+FF0Eone"].should.have.property("testU+FF0Etwo"); | ||
mongo.util.sanitise(object).should.have.property("here's a U+FF04 and now a U+FF0E"); | ||
}); | ||
it("with a sub-object key contains a '.' should replace with 'U+FF0E'", function () { | ||
var object = { | ||
"test.one": { | ||
"test.two": "sub-object" | ||
} | ||
}; | ||
var sanitised = mongo.util.sanitise(object); | ||
sanitised.should.have.property("testU+FF0Eone"); | ||
sanitised["testU+FF0Eone"].should.have.property("testU+FF0Etwo"); | ||
}); | ||
}); | ||
@@ -69,62 +77,70 @@ }); | ||
describe("unsanitise()", function () { | ||
it("with an object with plain keys should not change the key", function () { | ||
var object = { | ||
abcdefghiklmnopqrstuvwxyz: "just fine" | ||
}; | ||
mongo.util.unsanitise(object).should.have.property("abcdefghiklmnopqrstuvwxyz"); | ||
describe("when a string", function () { | ||
it("contains a '.' should replace with 'U+FF0E'", function () { | ||
mongo.util.unsanitise("heyU+FF0Ethere").should.equal("hey.there"); | ||
}); | ||
}); | ||
it("with a key containing a 'U+FF0E' should replace with '.'", function () { | ||
var object = { | ||
"testU+FF0Ethis": "not good" | ||
}; | ||
describe("when an object", function () { | ||
it("containing plain keys should not change the key", function () { | ||
var object = { | ||
abcdefghiklmnopqrstuvwxyz: "just fine" | ||
}; | ||
mongo.util.unsanitise(object).should.have.property("test.this"); | ||
}); | ||
mongo.util.unsanitise(object).should.have.property("abcdefghiklmnopqrstuvwxyz"); | ||
}); | ||
it("with a key containing multiple 'U+FF0E' codes should replace all with '.'", function () { | ||
var object = { | ||
"testU+FF0EthisU+FF0EanotherU+FF0Ehere": "not good" | ||
}; | ||
it("with a key containing a 'U+FF0E' should replace with '.'", function () { | ||
var object = { | ||
"testU+FF0Ethis": "not good" | ||
}; | ||
mongo.util.unsanitise(object).should.have.property("test.this.another.here"); | ||
}); | ||
mongo.util.unsanitise(object).should.have.property("test.this"); | ||
}); | ||
it("with a key containing a 'U+FF04' should replace with '$'", function () { | ||
var object = { | ||
"testU+FF04this": "not good" | ||
}; | ||
it("with a key containing multiple 'U+FF0E' codes should replace all with '.'", function () { | ||
var object = { | ||
"testU+FF0EthisU+FF0EanotherU+FF0Ehere": "not good" | ||
}; | ||
mongo.util.unsanitise(object).should.have.property("test$this"); | ||
}); | ||
mongo.util.unsanitise(object).should.have.property("test.this.another.here"); | ||
}); | ||
it("with a key containing multiple 'U+FF04' codes should replace all with '$'", function () { | ||
var object = { | ||
"testU+FF04thisU+FF04anotherU+FF04here": "not good" | ||
}; | ||
it("with a key containing a 'U+FF04' should replace with '$'", function () { | ||
var object = { | ||
"testU+FF04this": "not good" | ||
}; | ||
mongo.util.unsanitise(object).should.have.property("test$this$another$here"); | ||
}); | ||
mongo.util.unsanitise(object).should.have.property("test$this"); | ||
}); | ||
it("with a key containing both 'U+FF0E' and 'U+FF04' should replace them respectively", function () { | ||
var object = { | ||
"here's a U+FF04 and now a U+FF0E": "what an unlikely key!" | ||
}; | ||
it("with a key containing multiple 'U+FF04' codes should replace all with '$'", function () { | ||
var object = { | ||
"testU+FF04thisU+FF04anotherU+FF04here": "not good" | ||
}; | ||
mongo.util.unsanitise(object).should.have.property("here's a $ and now a ."); | ||
}); | ||
mongo.util.unsanitise(object).should.have.property("test$this$another$here"); | ||
}); | ||
it("with a sub-object key contains a 'U+FF0E' code should replace with '.'", function () { | ||
var object = { | ||
"testU+FF0Eone": { | ||
"testU+FF0Etwo": "sub-object" | ||
} | ||
}; | ||
it("with a key containing both 'U+FF0E' and 'U+FF04' should replace them respectively", function () { | ||
var object = { | ||
"here's a U+FF04 and now a U+FF0E": "what an unlikely key!" | ||
}; | ||
var unsanitised = mongo.util.unsanitise(object); | ||
unsanitised.should.have.property("test.one"); | ||
unsanitised["test.one"].should.have.property("test.two"); | ||
mongo.util.unsanitise(object).should.have.property("here's a $ and now a ."); | ||
}); | ||
it("with a sub-object key contains a 'U+FF0E' code should replace with '.'", function () { | ||
var object = { | ||
"testU+FF0Eone": { | ||
"testU+FF0Etwo": "sub-object" | ||
} | ||
}; | ||
var unsanitised = mongo.util.unsanitise(object); | ||
unsanitised.should.have.property("test.one"); | ||
unsanitised["test.one"].should.have.property("test.two"); | ||
}); | ||
}); | ||
}); | ||
}); |
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
24223
459