Last active
September 13, 2021 02:57
-
-
Save faisalman/879208 to your computer and use it in GitHub Desktop.
Revisions
-
faisalman revised this gist
Apr 22, 2013 . 1 changed file with 10 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +1,29 @@ /** * PHP-like print_r() equivalent for JavaScript Object * * @author Faisalman <fyzlman@gmail.com> * @license http://www.opensource.org/licenses/mit-license.php * @link http://gist.github.com/879208 */ var print_r = function (obj, t) { // define tab spacing var tab = t || ''; // check if it's array var isArr = Object.prototype.toString.call(obj) === '[object Array]'; // use {} for object, [] for array var str = isArr ? ('Array\n' + tab + '[\n') : ('Object\n' + tab + '{\n'); // walk through it's properties for (var prop in obj) { if (obj.hasOwnProperty(prop)) { var val1 = obj[prop]; var val2 = ''; var type = Object.prototype.toString.call(val1); switch (type) { // recursive if object/array case '[object Array]': case '[object Object]': @@ -42,8 +42,7 @@ var print_r = function(obj,t){ } // remove extra comma for last property str = str.substring(0, str.length - 2) + '\n' + tab; return isArr ? (str + ']') : (str + '}'); }; -
faisalman revised this gist
Apr 11, 2011 . 1 changed file with 37 additions and 82 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,91 +4,46 @@ * @author Faisalman <[email protected]> * @license http://www.opensource.org/licenses/mit-license.php * @link http://gist.github.com/879208 */ var print_r = function(obj,t){ // define tab spacing var tab = t || ''; // check if it's array var isArr = Object.prototype.toString.call(obj) === '[object Array]' ? true : false; // use {} for object, [] for array var str = isArr ? ('Array\n' + tab + '[\n') : ('Object\n' + tab + '{\n'); // walk through it's properties for(var prop in obj){ if (obj.hasOwnProperty(prop)) { var val1 = obj[prop]; var val2 = ''; var type = Object.prototype.toString.call(val1); switch(type){ // recursive if object/array case '[object Array]': case '[object Object]': val2 = print_r(val1, (tab + '\t')); break; case '[object String]': val2 = '\'' + val1 + '\''; break; default: val2 = val1; } str += tab + '\t' + prop + ' => ' + val2 + ',\n'; } } // remove extra comma for last property str = str.substring(0, str.length-2) + '\n' + tab; return isArr ? (str + ']') : (str + '}'); }; var var_dump = print_r; // equivalent function -
faisalman revised this gist
Apr 3, 2011 . 2 changed files with 24 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,29 +6,31 @@ * @link http://gist.github.com/879208 * @require type.of() */ var print_r = function(obj,t){ var isArray = type.of(obj)==='array'; var tab = t || ''; var dump = isArray ? 'Array\n'+tab+'[\n' : 'Object\n'+tab+'{\n'; for(var i in obj){ if (obj.hasOwnProperty(i)) { var param = obj[i]; var val = ''; var paramType = type.of(param); switch(paramType){ case 'array': case 'object': val = print_r(param,tab+'\t'); break; case 'boolean': val = param ? 'true' : 'false'; break; case 'string': val = '\''+param+'\''; break; default: val = param; } dump += tab+'\t'+i+' => '+val+',\n'; } } dump = dump.substring(0, dump.length-2)+'\n'+tab; return isArray ? dump+']' : dump+'}'; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ Usage example var obj = { foo: 'foo', bar: { foo: true, bar: null, baz: function(){alert('Hello');}}, qux: [1, 3, 2, 'qwerty', 55], quxx: /^[0-9]/ }; -- Then print_r(obj) or var_dump(obj) will return: Object { -
faisalman revised this gist
Mar 21, 2011 . 2 changed files with 39 additions and 61 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,51 +3,32 @@ * * @author Faisalman <[email protected]> * @license http://www.opensource.org/licenses/mit-license.php * @link http://gist.github.com/879208 * @require type.of() */ var print_r = function(obj,tab){ var isArray = type.of(obj)=='array'; var tab = tab || ''; var dump = isArray ? 'Array\n'+tab+'[\n' : 'Object\n'+tab+'{\n'; for(i in obj){ var param = isArray ? eval('obj['+i+']') : eval('obj.'+i); var val = ''; var paramType = type.of(param); switch(paramType){ case 'array': case 'object': val = var_dump(param,tab+'\t'); break; case 'boolean': val = param ? 'true' : 'false'; break; case 'string': val = '\''+param+'\''; break; default: val = param; } dump += tab+'\t'+i+' => '+val+',\n'; } dump = dump.substring(0, dump.length-2)+'\n'+tab; return isArray ? dump+']' : dump+'}'; @@ -108,35 +89,4 @@ var type={ } } } }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ Usage example ============== -- Consider there is an object: var obj = { foo: 'foo', bar: { foo: true, bar: null, baz: function(){alert('Hello');}}, qux: [1, 3, 2, 'qwerty', 55], quxx: /^[0-9]/ }; -- Then, print_r(obj) or var_dump(obj) will return this string: Object { foo => 'foo', bar => Object { foo => true, bar => null, baz => function (){alert('Hello');} }, qux => Array [ 0 => 1, 1 => 3, 2 => 2, 3 => 'qwerty', 4 => 55 ], quxx => /^[0-9]/ } -
faisalman created this gist
Mar 21, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,142 @@ /** * PHP-like print_r() & var_dump() equivalent for JavaScript Object * * @author Faisalman <[email protected]> * @license http://www.opensource.org/licenses/mit-license.php * @link http://gist.github.com/faisalman * @require type.of() */ var print_r = function(obj,isArray,tab){ if(isArray == undefined){ isArray = false; } if(tab == undefined){ tab = ''; } var dump = isArray ? tab+'[\n' : tab+'{\n'; for(i in obj){ var paramName = i; var param = ''; var val = ''; if(obj.length){ param = eval('obj['+i+']'); } else { param = eval('obj.'+i); } var paramType = type.of(param); switch(paramType){ case 'array': val = '\n'+var_dump(param,true,tab+' '); break; case 'boolean': val = param ? 'true' : 'false'; break; case 'function': case 'number': case 'regexp': val = param; break; case 'object': val = '\n'+var_dump(param,false,tab+' '); break; case 'string': val = '\''+param+'\''; break; default: val = paramType; } dump += tab+' '+paramName; dump += isArray ? ' => ' : ' : '; dump += val+',\n'; } dump = dump.substring(0, dump.length-2)+'\n'+tab; return isArray ? dump+']' : dump+'}'; }; var var_dump = print_r; /** * type.of() – a more specific typeof() * by Rolando Garza * * http://rolandog.com/archives/2007/01/18/typeof-a-more-specific-typeof/ * http://snipplr.com/view/1996 */ var is={ Null:function(a){ return a===null; }, Undefined:function(a){ return a===undefined; }, nt:function(a){ return(a===null||a===undefined); }, Function:function(a){ return(typeof(a)==='function')?a.constructor.toString().match(/Function/)!==null:false; }, String:function(a){ return(typeof(a)==='string')?true:(typeof(a)==='object')?a.constructor.toString().match(/string/i)!==null:false; }, Array:function(a){ return(typeof(a)==='object')?a.constructor.toString().match(/array/i)!==null||a.length!==undefined:false; }, Boolean:function(a){ return(typeof(a)==='boolean')?true:(typeof(a)==='object')?a.constructor.toString().match(/boolean/i)!==null:false; }, Date:function(a){ return(typeof(a)==='date')?true:(typeof(a)==='object')?a.constructor.toString().match(/date/i)!==null:false; }, HTML:function(a){ return(typeof(a)==='object')?a.constructor.toString().match(/html/i)!==null:false; }, Number:function(a){ return(typeof(a)==='number')?true:(typeof(a)==='object')?a.constructor.toString().match(/Number/)!==null:false; }, Object:function(a){ return(typeof(a)==='object')?a.constructor.toString().match(/object/i)!==null:false; }, RegExp:function(a){ return(typeof(a)==='function')?a.constructor.toString().match(/regexp/i)!==null:false; } }; var type={ of:function(a){ for(var i in is){ if(is[i](a)){ return i.toLowerCase(); } } } }; /** * Usage example * === * * -- Consider an object: * * var obj = { foo: 'foo', bar: { foo: true, bar: null, baz: function(){alert('Hello');}}, qux: [1, 3, 2, 'qwerty', 55], quxx: /^[0-9]/ }; * * -- Then print_r(obj) or var_dump(obj) will return: * * { * foo : 'foo', * bar : * { * foo : true, * bar : null, * baz : function (){alert('Hello');} * }, * qux : * [ * 0 => 1, * 1 => 3, * 2 => 2, * 3 => 'qwerty', * 4 => 55 * ], * quxx : /^[0-9]/ * } */