util.js (3021B)
1 // Copyright Joyent, Inc. and other Node contributors. 2 // 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: 10 // 11 // The above copyright notice and this permission notice shall be included 12 // in all copies or substantial portions of the Software. 13 // 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. 21 22 // NOTE: These type checking functions intentionally don't use `instanceof` 23 // because it is fragile and can be easily faked with `Object.create()`. 24 25 function isArray(arg) { 26 if (Array.isArray) { 27 return Array.isArray(arg); 28 } 29 return objectToString(arg) === '[object Array]'; 30 } 31 exports.isArray = isArray; 32 33 function isBoolean(arg) { 34 return typeof arg === 'boolean'; 35 } 36 exports.isBoolean = isBoolean; 37 38 function isNull(arg) { 39 return arg === null; 40 } 41 exports.isNull = isNull; 42 43 function isNullOrUndefined(arg) { 44 return arg == null; 45 } 46 exports.isNullOrUndefined = isNullOrUndefined; 47 48 function isNumber(arg) { 49 return typeof arg === 'number'; 50 } 51 exports.isNumber = isNumber; 52 53 function isString(arg) { 54 return typeof arg === 'string'; 55 } 56 exports.isString = isString; 57 58 function isSymbol(arg) { 59 return typeof arg === 'symbol'; 60 } 61 exports.isSymbol = isSymbol; 62 63 function isUndefined(arg) { 64 return arg === void 0; 65 } 66 exports.isUndefined = isUndefined; 67 68 function isRegExp(re) { 69 return objectToString(re) === '[object RegExp]'; 70 } 71 exports.isRegExp = isRegExp; 72 73 function isObject(arg) { 74 return typeof arg === 'object' && arg !== null; 75 } 76 exports.isObject = isObject; 77 78 function isDate(d) { 79 return objectToString(d) === '[object Date]'; 80 } 81 exports.isDate = isDate; 82 83 function isError(e) { 84 return (objectToString(e) === '[object Error]' || e instanceof Error); 85 } 86 exports.isError = isError; 87 88 function isFunction(arg) { 89 return typeof arg === 'function'; 90 } 91 exports.isFunction = isFunction; 92 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'; 100 } 101 exports.isPrimitive = isPrimitive; 102 103 exports.isBuffer = Buffer.isBuffer; 104 105 function objectToString(o) { 106 return Object.prototype.toString.call(o); 107 }