diff options
author | isaacs <i@izs.me> | 2013-07-26 14:38:08 -0700 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-08-01 15:08:01 -0700 |
commit | 22c68fdc1dae40f0ed9c71a02f66e5b2c6353691 (patch) | |
tree | b6d14cad6f5a9ba78172f77c5c2ec10b8a6cb89c /lib/util.js | |
parent | 9a29aa8c552eb2b120c101c1cfd66ba565a17ed5 (diff) | |
download | node-new-22c68fdc1dae40f0ed9c71a02f66e5b2c6353691.tar.gz |
src: Replace macros with util functions
Diffstat (limited to 'lib/util.js')
-rw-r--r-- | lib/util.js | 107 |
1 files changed, 81 insertions, 26 deletions
diff --git a/lib/util.js b/lib/util.js index 23de9df84b..b59bae5067 100644 --- a/lib/util.js +++ b/lib/util.js @@ -21,7 +21,7 @@ var formatRegExp = /%[sdj%]/g; exports.format = function(f) { - if (!IS_STRING(f)) { + if (!isString(f)) { var objects = []; for (var i = 0; i < arguments.length; i++) { objects.push(inspect(arguments[i])); @@ -49,7 +49,7 @@ exports.format = function(f) { } }); for (var x = args[i]; i < len; x = args[++i]) { - if (IS_NULL(x) || !IS_OBJECT(x)) { + if (isNull(x) || !isObject(x)) { str += ' ' + x; } else { str += ' ' + inspect(x); @@ -63,6 +63,13 @@ exports.format = function(f) { // Returns a modified function which warns once by default. // If --no-deprecation is set, then it is a no-op. exports.deprecate = function(fn, msg) { + // Allow for deprecating things in the process of starting up. + if (isUndefined(global.process)) { + return function() { + return exports.deprecate(fn, msg).apply(this, arguments); + }; + } + if (process.noDeprecation === true) { return fn; } @@ -87,8 +94,10 @@ exports.deprecate = function(fn, msg) { var debugs = {}; -var debugEnviron = process.env.NODE_DEBUG || ''; +var debugEnviron; exports.debuglog = function(set) { + if (isUndefined(debugEnviron)) + debugEnviron = process.env.NODE_DEBUG || ''; set = set.toUpperCase(); if (!debugs[set]) { if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { @@ -122,7 +131,7 @@ function inspect(obj, opts) { // legacy... if (arguments.length >= 3) ctx.depth = arguments[2]; if (arguments.length >= 4) ctx.colors = arguments[3]; - if (IS_BOOLEAN(opts)) { + if (isBoolean(opts)) { // legacy... ctx.showHidden = opts; } else if (opts) { @@ -130,10 +139,10 @@ function inspect(obj, opts) { exports._extend(ctx, opts); } // set default options - if (IS_UNDEFINED(ctx.showHidden)) ctx.showHidden = false; - if (IS_UNDEFINED(ctx.depth)) ctx.depth = 2; - if (IS_UNDEFINED(ctx.colors)) ctx.colors = false; - if (IS_UNDEFINED(ctx.customInspect)) ctx.customInspect = true; + if (isUndefined(ctx.showHidden)) ctx.showHidden = false; + if (isUndefined(ctx.depth)) ctx.depth = 2; + if (isUndefined(ctx.colors)) ctx.colors = false; + if (isUndefined(ctx.customInspect)) ctx.customInspect = true; if (ctx.colors) ctx.stylize = stylizeWithColor; return formatValue(ctx, obj, ctx.depth); } @@ -204,13 +213,13 @@ function formatValue(ctx, value, recurseTimes) { // Check that value is an object with an inspect function on it if (ctx.customInspect && value && - IS_FUNCTION(value.inspect) && + isFunction(value.inspect) && // Filter out the util module, it's inspect function is special value.inspect !== exports.inspect && // Also filter out any prototype objects using the circular check. !(value.constructor && value.constructor.prototype === value)) { var ret = value.inspect(recurseTimes); - if (!IS_STRING(ret)) { + if (!isString(ret)) { ret = formatValue(ctx, ret, recurseTimes); } return ret; @@ -232,7 +241,7 @@ function formatValue(ctx, value, recurseTimes) { // Some type of object without properties can be shortcutted. if (keys.length === 0) { - if (IS_FUNCTION(value)) { + if (isFunction(value)) { var name = value.name ? ': ' + value.name : ''; return ctx.stylize('[Function' + name + ']', 'special'); } @@ -256,7 +265,7 @@ function formatValue(ctx, value, recurseTimes) { } // Make functions say that they are functions - if (IS_FUNCTION(value)) { + if (isFunction(value)) { var n = value.name ? ': ' + value.name : ''; base = ' [Function' + n + ']'; } @@ -306,20 +315,20 @@ function formatValue(ctx, value, recurseTimes) { function formatPrimitive(ctx, value) { - if (IS_UNDEFINED(value)) + if (isUndefined(value)) return ctx.stylize('undefined', 'undefined'); - if (IS_STRING(value)) { + if (isString(value)) { var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') .replace(/'/g, "\\'") .replace(/\\"/g, '"') + '\''; return ctx.stylize(simple, 'string'); } - if (IS_NUMBER(value)) + if (isNumber(value)) return ctx.stylize('' + value, 'number'); - if (IS_BOOLEAN(value)) + if (isBoolean(value)) return ctx.stylize('' + value, 'boolean'); // For some reason typeof null is "object", so special case here. - if (IS_NULL(value)) + if (isNull(value)) return ctx.stylize('null', 'null'); } @@ -368,7 +377,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { } if (!str) { if (ctx.seen.indexOf(desc.value) < 0) { - if (IS_NULL(recurseTimes)) { + if (isNull(recurseTimes)) { str = formatValue(ctx, desc.value, null); } else { str = formatValue(ctx, desc.value, recurseTimes - 1); @@ -388,7 +397,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { str = ctx.stylize('[Circular]', 'special'); } } - if (IS_UNDEFINED(name)) { + if (isUndefined(name)) { if (array && key.match(/^\d+$/)) { return str; } @@ -432,28 +441,74 @@ function reduceToSingleString(output, base, braces) { // NOTE: These type checking functions intentionally don't use `instanceof` // because it is fragile and can be easily faked with `Object.create()`. function isArray(ar) { - return IS_ARRAY(ar); + return Array.isArray(ar); } exports.isArray = isArray; +function isBoolean(arg) { + return typeof arg === 'boolean'; +} +exports.isBoolean = isBoolean; + +function isNull(arg) { + return arg === null; +} +exports.isNull = isNull; + +function isNullOrUndefined(arg) { + return arg == null; +} +exports.isNullOrUndefined = isNullOrUndefined; + +function isNumber(arg) { + return typeof arg === 'number'; +} +exports.isNumber = isNumber; + +function isString(arg) { + return typeof arg === 'string'; +} +exports.isString = isString; + +function isSymbol(arg) { + return typeof arg === 'symbol'; +} +exports.isSymbol = isSymbol; + +function isUndefined(arg) { + return arg === void 0; +} +exports.isUndefined = isUndefined; function isRegExp(re) { - return IS_OBJECT(re) && objectToString(re) === '[object RegExp]'; + return isObject(re) && objectToString(re) === '[object RegExp]'; } exports.isRegExp = isRegExp; +function isObject(arg) { + return typeof arg === 'object' && arg; +} +exports.isObject = isObject; function isDate(d) { - return IS_OBJECT(d) && objectToString(d) === '[object Date]'; + return isObject(d) && objectToString(d) === '[object Date]'; } exports.isDate = isDate; - function isError(e) { - return IS_OBJECT(e) && objectToString(e) === '[object Error]'; + return isObject(e) && objectToString(e) === '[object Error]'; } exports.isError = isError; +function isFunction(arg) { + return typeof arg === 'function'; +} +exports.isFunction = isFunction; + +function isBuffer(arg) { + return arg instanceof Buffer; +} +exports.isBuffer = isBuffer; function objectToString(o) { return Object.prototype.toString.call(o); @@ -511,7 +566,7 @@ exports.inherits = function(ctor, superCtor) { exports._extend = function(origin, add) { // Don't do anything if add isn't an object - if (!add || !IS_OBJECT(add)) return origin; + if (!add || !isObject(add)) return origin; var keys = Object.keys(add); var i = keys.length; @@ -606,7 +661,7 @@ exports.pump = exports.deprecate(function(readStream, writeStream, callback) { var uv; exports._errnoException = function(err, syscall) { - if (IS_UNDEFINED(uv)) uv = process.binding('uv'); + if (isUndefined(uv)) uv = process.binding('uv'); var errname = uv.errname(err); var e = new Error(syscall + ' ' + errname); e.code = errname; |