不要怂,就是干,撸起袖子干!

Commit 96c8316c by Sascha Depold

Merge pull request #187 from innofluence/isHash

Fix for Utils.isHash on null and objects with a user-defined length property + tests for Utils.isHash
2 parents 2f16fb3b 86ac403d
Showing with 29 additions and 1 deletions
...@@ -44,7 +44,7 @@ var Utils = module.exports = { ...@@ -44,7 +44,7 @@ var Utils = module.exports = {
return client.format.apply(client, [query, replacements]) return client.format.apply(client, [query, replacements])
}, },
isHash: function(obj) { isHash: function(obj) {
return (typeof obj == 'object') && !obj.hasOwnProperty('length') return Utils._.isObject(obj) && !Utils._.isArray(obj);
}, },
toSqlDate: function(date) { toSqlDate: function(date) {
return [ return [
......
...@@ -84,4 +84,32 @@ describe('Utils', function() { ...@@ -84,4 +84,32 @@ describe('Utils', function() {
}) })
}) })
}) })
describe('isHash', function() {
it('doesn\'t match arrays', function() {
expect(Utils.isHash([])).toBeFalsy();
});
it('doesn\'t match null', function() {
expect(Utils.isHash(null)).toBeFalsy();
});
it('matches plain objects', function() {
var values = {
'name': {
'first': 'Foo',
'last': 'Bar'
}
};
expect(Utils.isHash(values)).toBeTruthy();
});
it('matches plain objects with length property/key', function() {
var values = {
'name': {
'first': 'Foo',
'last': 'Bar'
},
'length': 1
};
expect(Utils.isHash(values)).toBeTruthy();
});
});
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!