Sha256: 7bc2f41d409b5535627c5c5c7c96fbcc59db22c01f8614d54de87f63af564b29

Contents?: true

Size: 1.42 KB

Versions: 43

Compression:

Stored size: 1.42 KB

Contents

// taken and adapted from http://scriptnode.com/article/javascript-print_r-or-var_dump-equivalent/

/**
 * Concatenates the values of a variable into an easily readable string
 * by Matt Hackett [scriptnode.com]
 * @param {Object} x The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r(x, max, sep, l) {

	l = l || 0;
	max = max || 10;
	sep = sep || ' ';

	if (l > max) {
		throw("Too much recursion");
	};

	var r = '';

	if (x === null) {
		r += "null";
	} else if (is_array(x)) {
	  r += '[' + x.map(function(i) {
	    return print_r(i, max, sep, (l + 1));
	  }).join(', ') + ']';
	} else if(is_object(x)) {
    r += '{'
    var pairs = [];
    for (i in x) {
			pairs.push('"' + i + '": ' + print_r(x[i], max, sep, (l + 1)));
		}
		r += pairs.join(', ');
    r += '}';
	} else if(is_string(x)) {
	  r += '"' + x + "\"";
  } else {
    r += x;
  };

  return r;
  
  function is_string(a) {
    return typeof a === 'string';
  };
	
	function is_array(a) {
    return (a &&
      typeof a === 'object' &&
      a.constructor === Array);
  };
  
  function is_object(a) {
    return a && typeof a == 'object'
  };
  
};

Version data entries

43 entries across 43 versions & 3 rubygems

Version Path
couch_potato-0.7.1 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.7.0 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.7.0.pre.1 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.6.0 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.5.7 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-rails2-0.5.10 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-rails2-0.5.9 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-rails2-0.5.8 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-rails2-0.5.7 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-rails2-0.5.6 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.5.6 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.5.5 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.5.4 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.5.3 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.5.2 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.5.1 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.5.0 lib/couch_potato/rspec/matchers/print_r.js
davber_couch_potato-0.4.0 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.4.0 lib/couch_potato/rspec/matchers/print_r.js
couch_potato-0.3.2 lib/couch_potato/rspec/matchers/print_r.js