Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revert changes to the various isType functions. Make non-configurable…

… properties always display as constants instead of just capslock ones. Reorganize a bit to more closely match original organization of util.js.
  • Loading branch information...
commit 16049ea585809eaafd1524003d6aaa401ec5d8da 1 parent 1402bd5
@Benvie authored
Showing with 140 additions and 151 deletions.
  1. +140 −151 lib/util.js
View
291 lib/util.js
@@ -80,96 +80,41 @@ var error = exports.error = function(x) {
};
+/**
+ * Echos the value of a value. Trys to print the value out
+ * in the best way possible given the different types.
+ *
+ * @param {Object} obj The object to print out.
+ * @param {Boolean} showHidden Flag that shows hidden (not enumerable)
+ * properties of objects.
+ * @param {Number} depth Depth in which to descend in object. Default is 2.
+ * @param {Boolean} colors Flag to turn on ANSI escape codes to color the
+ * output. Default is false (no coloring).
+ * @param {Boolean} resolveGetters Attempt to resolve getters to their value.
+ * If an error is thrown the value is set to the error. Default is false.
+ */
-
-// callbind parameterizes `this`, useful for native prototype function
-// commonly used functionally
-var callbind = Function.prototype.call.bind.bind(Function.prototype.call);
-
-
-// each of the builtin's toString functions differ and many are useful
-var toString = {
- Date: callbind(Date.prototype.toString),
- Error: callbind(Error.prototype.toString),
- Function: callbind(Function.prototype.toString),
- Object: callbind(Object.prototype.toString),
- RegExp: callbind(RegExp.prototype.toString)
-};
-
-
-// slice '[object Class]' to 'Class' for use in dict lookups
-function getClass(o){
- return toString.Object(o).slice(8, -1);
-}
-
-
-// accepts either a string specifying classname, or an object to get it from
-function is(type){
- // cache the string
- type = typeof type === 'string'
- ? '[object ' + type + ']'
- : toString.Object(type);
-
- // return function to compare against cached class string
- return function(obj){
- return toString.Object(obj) === type;
+function inspect(obj, showHidden, depth, colors, resolveGetters) {
+ var settings = {
+ showHidden: showHidden, // show non-enumerables
+ resolveGetters: resolveGetters, // resolve getters to their value
+ style: colors ? color : reflect,
+ seen: []
};
-}
-exports.is = is;
-
-
-// returns true for strings, numbers, booleans, null, undefined, NaN
-function isPrimitive(o){
- return Object(o) !== o;
-}
-exports.isPrimitive = isPrimitive;
-
-
-var isUndefined = is('Undefined'),
- isFunction = is('Function'),
- isBoolean = is('Boolean'),
- isString = is('String'),
- isNumber = is('Number'),
- isRegExp = is('RegExp'),
- isObject = is('Object'), // inherits from Object, "plain object"
- isArray = Array.isArray,
- isError = is('Error'),
- isDate = is('Date'),
- isNull = is('Null');
-
-exports.isUndefined = isUndefined;
-exports.isFunction = isFunction;
-exports.isBoolean = isBoolean;
-exports.isString = isString;
-exports.isNumber = isNumber;
-exports.isRegExp = isRegExp;
-exports.isObject = isObject;
-exports.isArray = isArray;
-exports.isError = isError;
-exports.isDate = isDate;
-exports.isNull = isNull;
+ // cache formatted brackets
+ settings.square = [
+ settings.style('[', 'Square'),
+ settings.style(']', 'Square')
+ ];
+ settings.curly = [
+ settings.style('{', 'Curly'),
+ settings.style('}', 'Curly')
+ ];
-// formatter for functions shared with constructor formatter
-function functionLabel(fn, isCtor){
- var type = isCtor ? 'Constructor' : 'Function',
- label = fn.name ? ': ' + fn.name : '';
- return '[' + type + label + ']';
+ return formatValue(obj, depth || 2, settings);
}
-
-var formatters = {
- Boolean : String,
- Constructor : function(f){ return functionLabel(f, true); },
- Date : toString.Date,
- Error : function(f){ return '['+toString.Error(f)+']'; },
- Function : function(f){ return functionLabel(f, false); },
- Null : String,
- Number : String,
- RegExp : toString.RegExp,
- String : quotes,
- Undefined : String
-};
-
+exports.inspect = inspect;
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
@@ -195,48 +140,98 @@ var ansi = {
// map types to a color
var styles = {
- Constructor : 'boldyellow',
+ // falsey
Undefined : 'boldblack',
- Circular : 'boldcyan',
+ Null : 'boldblack',
+ // constructor functions
+ Constructor : 'boldyellow',
+ // normal types
Function : 'boldmagenta',
- Accessor : 'boldcyan',
- HConstant : 'cyan',
- Constant : 'cyan',
Boolean : 'magenta',
+ Date : 'red',
+ Error : 'boldred',
Number : 'yellow',
RegExp : 'red',
+ // proprty names and strings
HString : 'green',
String : 'boldgreen',
- Square : 'boldblue',
- Error : 'boldred',
- Curly : 'cyan',
+ HConstant : 'cyan',
+ Constant : 'cyan',
HName : 'boldblack',
Name : 'boldwhite',
- Date : 'red',
- Null : 'boldblack',
- More : 'boldcyan'
+ // meta-labels
+ More : 'boldcyan',
+ Accessor : 'boldcyan',
+ Circular : 'boldcyan',
+ // brackets
+ Square : 'boldblue',
+ Curly : 'cyan'
};
+
+// callbind parameterizes `this`
+var callbind = Function.prototype.call.bind.bind(Function.prototype.call);
+
+var objectToString = callbind(Object.prototype.toString);
+
+
+// slice '[object Class]' to 'Class' for use in dict lookups
+function getClass(o){
+ return objectToString(o).slice(8, -1);
+}
+
+
+// formatter for functions shared with constructor formatter
+function functionLabel(fn, isCtor){
+ var type = isCtor ? 'Constructor' : 'Function',
+ label = fn.name ? ': ' + fn.name : '';
+ return '[' + type + label + ']';
+}
+
+
+// most formatting determined by internal [[class]]
+var formatters = {
+ Boolean : String,
+ Constructor : function(f){ return functionLabel(f, true); },
+ Date : callbind(Date.prototype.toString),
+ Error : callbind(Error.prototype.toString),
+ Function : function(f){ return functionLabel(f, false); },
+ Null : String,
+ Number : String,
+ RegExp : callbind(RegExp.prototype.toString),
+ String : quotes,
+ Undefined : String
+};
+
+
// wrap a string with ansi escapes for coloring
-function color(str, style) {
- return !styles[style] ? str : '\033['+ansi[styles[style]][0]+'m' +
- str + '\033['+ansi[styles[style]][1]+'m';
+function color(str, style, special) {
+ var out = special ? '\u00AB' + str + '\u00BB' : str;
+ if (styles[style]) {
+ out = '\033[' + ansi[styles[style]][0] + 'm' + out +
+ '\033[' + ansi[styles[style]][1] + 'm';
+ }
+ return out;
}
+
// return passed value unmodified
function reflect(x) {
return x;
}
+
// use fancy quotes to differentiate strings from unescaped values,
// then unescape quotes and backslashes
function quotes(s){
- return '\u201C' + String(s).replace(/\\(["'\\])/g, '$1') //unescape
- .replace(/^["']/, '') //remove leading quotes
- .replace(/["']$/, '') //remove trailing quotes
+ return '\u201C' + String(s)
+ .replace(/\\(["'\\])/g, '$1') //unescape
+ .replace(/^["']/, '') //remove leading quotes
+ .replace(/["']$/, '') //remove trailing quotes
+ '\u201D';
}
+<<<<<<< HEAD
/**
* Echos the value of a value. Trys to print the value out
* in the best way possible given the different types.
@@ -271,6 +266,8 @@ function inspect(obj, showHidden, depth, colors, resolveGetters) {
}
exports.inspect = inspect;
+=======
+>>>>>>> Revert changes to the various isType functions. Make non-configurable properties always display as constants instead of just capslock ones. Reorganize a bit to more closely match original organization of util.js.
function formatValue(value, depth, settings) {
// Provide a hook for user-specified inspect functions.
@@ -292,7 +289,7 @@ function formatValue(value, depth, settings) {
if (type in formatters) {
if (!isPrimitive(value) && type === 'Function') {
// check for constructors
- if (value.prototype &&
+ if (!isPrimitive(value.prototype) &&
value === value.prototype.constructor &&
Object.getOwnPropertyNames(value.prototype).length > 1) {
// label constructors separately from functions
@@ -308,14 +305,14 @@ function formatValue(value, depth, settings) {
// depth limit reached
if (depth < 0) {
- return settings.style('\u00ABMore\u00BB', 'More');
+ return settings.style('More', 'More', true);
}
var properties;
if (settings.showHidden) {
properties = Object.getOwnPropertyNames(value).filter(function(key){
// filter out [constructor, length, prototype, arguments, caller]
- return !(key in Function.constructor);
+ return !(key in Function);
});
} else {
properties = Object.keys(value);
@@ -326,7 +323,7 @@ function formatValue(value, depth, settings) {
if (base) {
return base;
}
- if (!array || value.length == 0) {
+ if (!array || value.length === 0) {
return braces.join('');
}
}
@@ -381,46 +378,6 @@ function formatProperty(value, key, depth, settings, array) {
.map(function(line){ return ' ' + line; })
.join('\n');
-<<<<<<< HEAD
-function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
- var name, str, desc;
- desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
- if (desc.get) {
- if (desc.set) {
- str = ctx.stylize('[Getter/Setter]', 'special');
- } else {
- str = ctx.stylize('[Getter]', 'special');
- }
- } else {
- if (desc.set) {
- str = ctx.stylize('[Setter]', 'special');
- }
- }
- if (visibleKeys.indexOf(key) < 0) {
- name = '[' + key + ']';
- }
- if (!str) {
- if (ctx.seen.indexOf(desc.value) < 0) {
- if (recurseTimes === null) {
- str = formatValue(ctx, desc.value, null);
- } else {
- str = formatValue(ctx, desc.value, recurseTimes - 1);
- }
- if (str.indexOf('\n') > -1) {
- if (array) {
- str = str.split('\n').map(function(line) {
- return ' ' + line;
- }).join('\n').substr(2);
- } else {
- str = '\n' + str.split('\n').map(function(line) {
- return ' ' + line;
- }).join('\n');
- }
- }
- } else {
- str = ctx.stylize('[Circular]', 'special');
- }
-=======
// trim the edges
str = array ? substring(str, 2) : '\n' + str;
}
@@ -430,17 +387,16 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
// array indexes don't display their name
if (array && /^\d+$/.test(key)) {
return str;
->>>>>>> Significant modifications to util.js. Changes includes new type detection functions and new inspect formatting.
}
// allow different coloring for non-enumerable names
var nameFormat = val.enumerable ? '' : 'H';
- if (/^[a-zA-Z_\$][a-zA-Z0-9\$_]*$/.test(key)) {
+ if (/^[a-zA-Z_\$][a-zA-Z0-9_\$]*$/.test(key)) {
// valid JavaScript name not requiring quotes
- if (!val.configurable && /^[^a-z]*$/.test(key)) {
- // constants are all capitals and non-configurable
+ if (!val.configurable) {
+ // color non-configurable differently
nameFormat += 'Constant';
} else {
// regular name
@@ -466,7 +422,7 @@ function combine(output, base, braces) {
}, 0);
if (base.length) {
- // if given base check that it's not too long
+ // if given base make it so that it's not too long
if (length > 60) {
base = ' ' + base;
output.unshift(lines > 1 ? '' : ' ');
@@ -485,6 +441,39 @@ function combine(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 Array.isArray(ar) ||
+ (typeof ar === 'object' && objectToString(ar) === '[object Array]');
+}
+exports.isArray = isArray;
+
+
+function isRegExp(re) {
+ return typeof re === 'object' && objectToString(re) === '[object RegExp]';
+}
+exports.isRegExp = isRegExp;
+
+
+function isDate(d) {
+ return typeof d === 'object' && objectToString(d) === '[object Date]';
+}
+exports.isDate = isDate;
+
+
+function isError(e) {
+ return typeof e === 'object' && objectToString(e) === '[object Error]';
+}
+exports.isError = isError;
+
+
+// returns true for strings, numbers, booleans, null, undefined, NaN
+function isPrimitive(o){
+ return Object(o) !== o;
+}
+exports.isPrimitive = isPrimitive;
+
exports.p = function() {
for (var i = 0, len = arguments.length; i < len; ++i) {
Please sign in to comment.
Something went wrong with that request. Please try again.