1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // NOTE: These type checking functions intentionally don't use `instanceof`
23 // because it is fragile and can be easily faked with `Object.create()`.
25 function isArray(arg) {
27 return Array.isArray(arg);
29 return objectToString(arg) === '[object Array]';
31 exports.isArray = isArray;
33 function isBoolean(arg) {
34 return typeof arg === 'boolean';
36 exports.isBoolean = isBoolean;
38 function isNull(arg) {
41 exports.isNull = isNull;
43 function isNullOrUndefined(arg) {
46 exports.isNullOrUndefined = isNullOrUndefined;
48 function isNumber(arg) {
49 return typeof arg === 'number';
51 exports.isNumber = isNumber;
53 function isString(arg) {
54 return typeof arg === 'string';
56 exports.isString = isString;
58 function isSymbol(arg) {
59 return typeof arg === 'symbol';
61 exports.isSymbol = isSymbol;
63 function isUndefined(arg) {
64 return arg === void 0;
66 exports.isUndefined = isUndefined;
68 function isRegExp(re) {
69 return objectToString(re) === '[object RegExp]';
71 exports.isRegExp = isRegExp;
73 function isObject(arg) {
74 return typeof arg === 'object' && arg !== null;
76 exports.isObject = isObject;
79 return objectToString(d) === '[object Date]';
81 exports.isDate = isDate;
84 return (objectToString(e) === '[object Error]' || e instanceof Error);
86 exports.isError = isError;
88 function isFunction(arg) {
89 return typeof arg === 'function';
91 exports.isFunction = isFunction;
93 function isPrimitive(arg) {
94 return arg === null ||
95 typeof arg === 'boolean' ||
96 typeof arg === 'number' ||
97 typeof arg === 'string' ||
98 typeof arg === 'symbol' || // ES6 symbol
99 typeof arg === 'undefined';
101 exports.isPrimitive = isPrimitive;
103 exports.isBuffer = Buffer.isBuffer;
105 function objectToString(o) {
106 return Object.prototype.toString.call(o);