Skip to content

Commit b680012

Browse files
committed
improve isBuffer function
1 parent 4bd5498 commit b680012

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@ function isError(obj) { return toS(obj) === '[object Error]'; }
88
function isBoolean(obj) { return toS(obj) === '[object Boolean]'; }
99
function isNumber(obj) { return toS(obj) === '[object Number]'; }
1010
function isString(obj) { return toS(obj) === '[object String]'; }
11-
function isBuffer(obj) { return typeof Buffer !== 'undefined' && typeof Buffer.isBuffer === 'function' && Buffer.isBuffer(obj); }
1211

1312
// TODO: use isarray
1413
var isArray = Array.isArray || function isArray(xs) {
1514
return Object.prototype.toString.call(xs) === '[object Array]';
1615
};
1716

17+
function isBuffer(x) {
18+
if (!x || typeof x !== 'object' || typeof x.length !== 'number') {
19+
return false;
20+
}
21+
if (typeof x.copy !== 'function' || typeof x.slice !== 'function') {
22+
return false;
23+
}
24+
if (x.length > 0 && typeof x[0] !== 'number') {
25+
return false;
26+
}
27+
28+
return !!(x.constructor && x.constructor.isBuffer && x.constructor.isBuffer(x));
29+
}
30+
1831
// TODO: use for-each?
1932
function forEach(xs, fn) {
2033
if (xs.forEach) { return xs.forEach(fn); }

0 commit comments

Comments
 (0)