Last active
April 15, 2016 05:56
-
-
Save Kr3m/cfeb326cd63db2befa83a629dbc1b24a to your computer and use it in GitHub Desktop.
This file contains 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 characters
/** | |
* conc() Console Comments w/ print_r support by Faisalman | |
* | |
* @author Kevin Remisoski <kevin@remytek.com> | |
* @license http://www.opensource.org/licenses/mit-license.php | |
* @link https://gist.github.com/Kr3m/cfeb326cd63db2befa83a629dbc1b24a | |
* usage: conc(msg, output) e.g. conc('Hello World!', var); | |
* to do: remove redundancies (e.g. isArr) | |
*/ | |
var conc = function(msg = null, output = null) { | |
var nl = function() { | |
console.log('________________________________________________'); | |
console.log('\n'); | |
} | |
if (!msg) { | |
msg = 'Welcome to conc \n Developed by Kevin Remisoski \n kevin@remytek.com'; | |
} | |
if (msg && output) { | |
console.log('**********'); | |
console.log(msg); | |
console.log('**********'); | |
if (Object.prototype.toString.call(output) === '[object Array]') { | |
console.log(print_r(output)); | |
} else { | |
console.log(output); | |
} | |
nl(); | |
} else { | |
console.log('**********'); | |
console.log(msg); | |
console.log('**********'); | |
nl(); | |
} | |
} | |
/** | |
* 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]': | |
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 + '}'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment