Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Created October 11, 2010 13:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitrySoshnikov/620544 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/620544 to your computer and use it in GitHub Desktop.
/**
* Console object for BESEN
* @author Dmitry A. Soshnikov <dmitry.soshnikov@gmail.com>
*/
(function initConsole(global) {
// helpers
var getClass = Object.prototype.toString;
var timeMap = {};
function repeatSring(string, times) {
return Array(times + 1).join(string);
}
function dir(object, deep, level) {
level || (level = 1);
typeof deep == "undefined" && (deep = true);
var openBracket, closeBracket;
if (Array.isArray(object)) {
openBracket = "["; closeBracket = "]"
} else {
openBracket = "{"; closeBracket = "}"
}
var props = [];
var indent = repeatSring(console.dir.indention, level);
if (console.dir.showInternals) {
props.push(indent + "[[Class]]: \"" + getClass.call(object).slice(8, -1) + "\"");
}
var data, current, currentClass;
var goDeeper = (typeof deep == "number" ? deep > level : deep);
Object.getOwnPropertyNames(object).forEach(function (property) {
current = object[property];
currentClass = getClass.call(current);
if (goDeeper && (currentClass == "[object Object]" || Array.isArray(current))) {
data = dir(current, deep, level + 1);
} else {
data = (
typeof current == "function" ? "function" : (
Array.isArray(current) ? "[" + current + "]" :
(currentClass == "[object String]" ? "\"" + current + "\"" : current)
)
);
}
props.push(indent + property + ": " + data);
});
return "".concat(
openBracket, "\n", props.join(",\n"), "\n",
(level > 1 ? repeatSring(console.dir.indention, level - 1) : ""),
closeBracket
);
}
/**
* console object;
* implements: log, dir, time, timeEnd
*/
global.console = {
/**
* simple log using toString
*/
log: println,
/**
* dir an object
* @param {Object} object
* @param {Variant} deep - level of depth, default is {Boolean} true
* can be set also to {Number} value specifying needed level of depth
* Examples:
* - console.dir(obj) // console.dir(obj, true)
* - console.dir(obj, false); // only first level is shown
* - console.dir(obj, 3); // properties of three levels are shown
*/
dir: function (object, deep) {
// if called for a primitive
if (Object(object) !== object) {
return console.log(object);
}
// else for an object
return println(dir(object, deep));
},
// time functions borrowed from Firebug
/**
* time start
*/
time: function(name) {
timeMap[name] = Date.now();
},
/**
* time end
*/
timeEnd: function(name) {
if (name in timeMap) {
var delta = Date.now() - timeMap[name];
println(name + ": ", delta + "ms");
delete timeMap[name];
}
}
};
// indention for dir, default is 4 spaces
console.dir.indention = " ";
// whether to show internal properties such as [[Class]]
console.dir.showInternals = true;
})(this);
// tests
var foo = [1, 2, 3];
foo[5] = "6";
var bar = {
a: 10,
b: 20,
c: {
x: new String("test"),
y: new Number(20),
z: {
0: 100,
1: foo
}
}
};
// full depth
console.log("Full depth:");
console.dir(bar);
// only 3 levels
console.log("Only three levels:");
console.dir(bar, 3);
// only 2 levels
console.log("Only two levels:");
console.dir(bar, 2);
// only first level
console.log("Only first level:");
console.dir(bar, 1); // the same console.dir(bar, false);
// time test
console.time("Bubble sorting");
for (var k = 1000000; k--;) var bar = 10;
console.timeEnd("Bubble sorting");
// with showInternals option
// dir shows internal properties too
console.dir(Object);
/*
{
[[Class]]: "Function",
length: 1,
getPrototypeOf: function,
getOwnPropertyDescriptor: function,
getOwnPropertyNames: function,
create: function,
defineProperty: function,
defineProperties: function,
seal: function,
freeze: function,
preventExtensions: function,
isSealed: function,
isFrozen: function,
isExtensible: function,
keys: function,
prototype: {
[[Class]]: "Object",
toString: function,
toLocaleString: function,
toSource: function,
valueOf: function,
hasOwnProperty: function,
isPrototypeOf: function,
propertyIsEnumerable: function,
constructor: function
}
}
*/
// strings in quotes
console.dir({x: "test"});
/*
{
[[Class]]: "Object",
x: "test"
}
*/
// prototype property
// of the Array constructor
// is an array
console.dir(Array);
/*
{
[[Class]]: "Function",
length: 1,
isArray: function,
prototype: [
[[Class]]: "Array",
concat: function,
constructor: function,
every: function,
filter: function,
forEach: function,
indexOf: function,
join: function,
lastIndexOf: function,
length: 0,
map: function,
pop: function,
push: function,
reduce: function,
reduceRight: function,
reverse: function,
shift: function,
slice: function,
some: function,
sort: function,
splice: function,
toLocaleString: function,
toString: function,
unshift: function
]
}
*/
// reconfigure dir:
// change the indention
console.dir.indention = " ";
// turn off showing internals
console.dir.showInternals = false;
console.dir({a: 10, b: "test", c: [1, 2, 3]});
/*
{
a: 10,
b: "test",
c: [
0: 1,
1: 2,
2: 3,
length: 3
]
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment