mongojs-hooks
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -120,2 +120,24 @@ var mongojs = require("mongojs"); | ||
return newObject; | ||
}, | ||
// Flattens an object into dot-notation | ||
flatten: function (object) { | ||
var result = { }; | ||
for (var i in object) { | ||
if (!object.hasOwnProperty(i)) continue; | ||
if (isObject(object[i])) { | ||
var flatObject = this.flatten(object[i]); | ||
for (var x in flatObject) { | ||
if (!flatObject.hasOwnProperty(x)) continue; | ||
result[i + '.' + x] = flatObject[x]; | ||
} | ||
} else { | ||
result[i] = object[i]; | ||
} | ||
} | ||
return result; | ||
} | ||
@@ -122,0 +144,0 @@ }; |
{ | ||
"name": "mongojs-hooks", | ||
"description": "Wrap monogjs in pre and post hooks", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"main": "./mongojs-hooks", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -269,2 +269,63 @@ var mongo = require("../mongojs-hooks"); | ||
}); | ||
describe("flatten()", function () { | ||
describe("when an object", function () { | ||
it("and nested", function () { | ||
var object = { | ||
first: { | ||
second: "value" | ||
} | ||
}; | ||
var flattened = mongo.util.flatten(object); | ||
flattened.should.deep.equal({ | ||
"first.second": "value" | ||
}); | ||
}); | ||
it("with multiple values", function () { | ||
var object = { | ||
first: { | ||
second: "value", | ||
third: "val" | ||
} | ||
}; | ||
var flattened = mongo.util.flatten(object); | ||
flattened.should.deep.equal({ | ||
"first.second": "value", | ||
"first.third": "val" | ||
}); | ||
}); | ||
it("and contains an Array", function () { | ||
var object = { | ||
first: { | ||
second: [ "el1", "el2", "el3" ] | ||
} | ||
}; | ||
var flattened = mongo.util.flatten(object); | ||
flattened.should.deep.equal({ | ||
"first.second": [ "el1", "el2", "el3" ] | ||
}); | ||
}); | ||
it("should not mutate original object", function () { | ||
var object = { | ||
first: { | ||
second: "value" | ||
} | ||
}; | ||
mongo.util.flatten(object); | ||
object.should.not.have.property("first.second"); | ||
object.should.deep.equal({ | ||
first: { | ||
second: "value" | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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
28950
615