Skip to content

Commit ec8ed15

Browse files
committed
Better check for isPlainObject
1 parent 5620aeb commit ec8ed15

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
var TEST_REGEX = /^\$|\./,
44
REPLACE_REGEX = /^\$|\./g;
55

6-
var withEach = function(target, cb) {
6+
function isPlainObject(obj) {
7+
if(obj === null || typeof obj !== 'object') {
8+
return false;
9+
}
10+
var proto = Object.getPrototypeOf(obj);
11+
return proto === Object.prototype || proto === null;
12+
}
13+
14+
function withEach(target, cb) {
715
var act = function(obj) {
816
if(Array.isArray(obj)) {
917
obj.forEach(act);
1018

11-
} else if(obj instanceof Object) {
19+
} else if(isPlainObject(obj)) {
1220
Object.keys(obj).forEach(function(key) {
1321
var val = obj[key];
1422
var resp = cb(obj, val, key);
@@ -20,9 +28,9 @@ var withEach = function(target, cb) {
2028
};
2129

2230
act(target);
23-
};
31+
}
2432

25-
var has = function(target) {
33+
function has(target) {
2634
var hasProhibited = false;
2735
withEach(target, function(obj, val, key) {
2836
if(TEST_REGEX.test(key)) {
@@ -34,9 +42,9 @@ var has = function(target) {
3442
});
3543

3644
return hasProhibited;
37-
};
45+
}
3846

39-
var sanitize = function(target, options) {
47+
function sanitize(target, options) {
4048
options = options || {};
4149

4250
var replaceWith = null;
@@ -64,9 +72,9 @@ var sanitize = function(target, options) {
6472
});
6573

6674
return target;
67-
};
75+
}
6876

69-
var middleware = function(options) {
77+
function middleware(options) {
7078
return function(req, res, next) {
7179
['body', 'params', 'query'].forEach(function(k) {
7280
if(req[k]) {
@@ -75,7 +83,7 @@ var middleware = function(options) {
7583
});
7684
next();
7785
};
78-
};
86+
}
7987

8088
module.exports = middleware;
8189
module.exports.sanitize = sanitize;

0 commit comments

Comments
 (0)