Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Quoted Reserved Words #3

Open
wants to merge 24 commits into from

1 participant

Commits on Mar 14, 2011
  1. Added quoted reserved words check.

    Joris Röling authored
Commits on Apr 14, 2011
  1. @jorisroling

    added HTML support

    jorisroling authored
Commits on May 3, 2011
  1. @jorisroling

    eyes class

    jorisroling authored
Commits on May 6, 2011
  1. @jorisroling
Commits on May 10, 2011
  1. @jorisroling
Commits on Sep 9, 2011
  1. added json options form JSON compatible output

    Joris Röling authored
Commits on Sep 10, 2011
  1. no reserved word quotes if json=true

    Joris Röling authored
Commits on Sep 19, 2011
  1. added escape option (for whitespace)

    Joris Röling authored
  2. upped version by 1

    Joris Röling authored
  3. version has a build number with a dash

    Joris Röling authored
Commits on Sep 22, 2011
  1. add function option

    Joris Röling authored
  2. Merge branch 'master' of https://github.com/jjupiter/eyes.js

    Joris Röling authored
    Conflicts:
    	lib/eyes.js
  3. bumped version again

    Joris Röling authored
Commits on Sep 30, 2011
  1. added exclude option for level 1 keys

    Joris Röling authored
  2. bumped version

    Joris Röling authored
Commits on Nov 1, 2011
  1. iJJR

    Joris Röling authored
Commits on Nov 21, 2011
  1. htmlspecialchars

    Joris Röling authored
  2. bumped version

    Joris Röling authored
Commits on Jan 31, 2012
  1. @jorisroling

    version 1.6.4

    jorisroling authored
  2. @jorisroling

    version 1.6.4joris

    jorisroling authored
Commits on Mar 4, 2012
  1. @jorisroling

    no Buffer

    jorisroling authored
  2. @jorisroling

    no Buffer

    jorisroling authored
Commits on Aug 6, 2012
  1. @jorisroling

    leaks

    jorisroling authored
This page is out of date. Refresh to see the latest.
Showing with 170 additions and 55 deletions.
  1. +155 −50 lib/eyes.js
  2. +1 −1  package.json
  3. +14 −4 test/eyes-test.js
View
205 lib/eyes.js
@@ -21,6 +21,7 @@ eyes.defaults = {
other: 'inverted', // Objects which don't have a literal representation, such as functions
key: 'bold', // The keys in object literals, like 'a' in `{a: 1}`
special: 'grey', // null, undefined...
+ date: 'silver', // JJR
string: 'green',
number: 'magenta',
bool: 'blue', // true false
@@ -28,16 +29,26 @@ eyes.defaults = {
},
pretty: true, // Indent object literals
hideFunctions: false,
- stream: process.stdout,
- maxLength: 2048 // Truncate output if longer
+ stream: process && process.stdout,
+ maxLength: 2048, // Truncate output if longer
+ colors: true,
+ html: false,
+ json: false,
+ escape: true,
+ functions: false
};
// Return a curried inspect() function, with the `options` argument filled in.
eyes.inspector = function (options) {
var that = this;
return function (obj, label, opts) {
- return that.inspect.call(that, obj, label,
- merge(options || {}, opts || {}));
+ var myopts=merge(options || {}, opts || {});
+ var result=that.inspect.call(that, obj, label, myopts);
+ if (myopts.html && !myopts.stream) {
+ return '<pre class="eyes" style="padding:8px;background-color: black;'+((myopts && myopts.styles && myopts.styles.all)?('color:'+myopts.styles.all+';'):'')+'">'+result+'</pre>';
+ } else {
+ return result;
+ }
};
};
@@ -45,11 +56,11 @@ eyes.inspector = function (options) {
// if not, we just return the stringified object.
eyes.inspect = function (obj, label, options) {
options = merge(this.defaults, options || {});
-
+ stack = [];
if (options.stream) {
return this.print(stringify(obj, options), label, options);
} else {
- return stringify(obj, options) + (options.styles ? '\033[39m' : '');
+ return stringify(obj, options) + (options.styles ? (options.html?'':'\033[39m') : '');
}
};
@@ -61,41 +72,63 @@ eyes.inspect = function (obj, label, options) {
// versus a printable character (`c`). So we resort to counting the
// length manually.
eyes.print = function (str, label, options) {
- for (var c = 0, i = 0; i < str.length; i++) {
- if (str.charAt(i) === '\033') { i += 4 } // `4` because '\033[25m'.length + 1 == 5
- else if (c === options.maxLength) {
- str = str.slice(0, i - 1) + '';
- break;
- } else { c++ }
+ if (!this.html) {
+ for (var c = 0, i = 0; i < str.length; i++) {
+ if (str.charAt(i) === '\033') { i += 4 } // `4` because '\033[25m'.length + 1 == 5
+ else if (c === options.maxLength) {
+ str = str.slice(0, i - 1) + '';
+ break;
+ } else { c++ }
+ }
}
return options.stream.write.call(options.stream, (label ?
- this.stylize(label, options.styles.label, options.styles) + ': ' : '') +
- this.stylize(str, options.styles.all, options.styles) + '\033[0m' + "\n");
+ this.stylize(label, options.styles.label, options) + ': ' : '') +
+ this.stylize(str, options.styles.all, options) + (this.html?'':'\033[0m') + "\n");
};
// Apply a style to a string, eventually,
// I'd like this to support passing multiple
// styles.
-eyes.stylize = function (str, style, styles) {
- var codes = {
- 'bold' : [1, 22],
- 'underline' : [4, 24],
- 'inverse' : [7, 27],
- 'cyan' : [36, 39],
- 'magenta' : [35, 39],
- 'blue' : [34, 39],
- 'yellow' : [33, 39],
- 'green' : [32, 39],
- 'red' : [31, 39],
- 'grey' : [90, 39]
- }, endCode;
-
- if (style && codes[style]) {
- endCode = (codes[style][1] === 39 && styles.all) ? codes[styles.all][0]
- : codes[style][1];
- return '\033[' + codes[style][0] + 'm' + str +
- '\033[' + endCode + 'm';
- } else { return str }
+eyes.stylize = function (str, style, options) {
+ if (options.colors) {
+ if (options.html) {
+ var codes = {
+ 'bold' : 'font-weight:bold',
+ 'underline' : 'text-decoration: underline',
+ };
+ if (style) {
+ if (codes[style]) {
+ return '<span style="'+style+'">'+str+'</span>';
+ } else {
+ return '<span style="color:'+style+'">'+str+'</span>';
+ }
+ } else {
+ return str;
+ }
+ } else {
+ var codes = {
+ 'bold' : [1, 22],
+ 'underline' : [4, 24],
+ 'inverse' : [7, 27],
+ 'cyan' : [36, 39],
+ 'magenta' : [35, 39],
+ 'blue' : [34, 39],
+ 'yellow' : [33, 39],
+ 'green' : [32, 39],
+ 'red' : [31, 39],
+ 'grey' : [90, 39]
+ }, endCode;
+
+ if (style && codes[style]) {
+ endCode = (codes[style][1] === 39 && options.styles.all) ? codes[options.styles.all][0]
+ : codes[style][1];
+ return '\033[' + codes[style][0] + 'm' + str +
+ '\033[' + endCode + 'm';
+ } else { return str }
+ }
+ } else {
+ return str;
+ }
};
// Convert any object to a string, ready for output.
@@ -104,7 +137,7 @@ eyes.stylize = function (str, style, styles) {
// stringify().
function stringify(obj, options) {
var that = this, stylize = function (str, style) {
- return eyes.stylize(str, options.styles[style], options.styles)
+ return eyes.stylize(str, options.styles[style], options)
}, index, result;
if ((index = stack.indexOf(obj)) !== -1) {
@@ -112,18 +145,19 @@ function stringify(obj, options) {
}
stack.push(obj);
- result = (function (obj) {
+
+ result = (function (obj) {
switch (typeOf(obj)) {
- case "string" : obj = stringifyString(obj.indexOf("'") === -1 ? "'" + obj + "'"
- : '"' + obj + '"');
+
+ case "string" : obj = stringifyString((obj.indexOf("'") === -1 && !options.json)? ("'" + obj.replace(/'/g,"\\'") + "'") : ('"' + obj.replace(/"/g,'\\"') + '"'),options);
return stylize(obj, 'string');
case "regexp" : return stylize('/' + obj.source + '/', 'regexp');
case "number" : return stylize(obj + '', 'number');
- case "function" : return options.stream ? stylize("Function", 'other') : '[Function]';
+ case "function" : return options.stream ? stylize(options.functions?obj.toString():"Function", 'other') : '[Function]';
case "null" : return stylize("null", 'special');
case "undefined": return stylize("undefined", 'special');
case "boolean" : return stylize(obj + '', 'bool');
- case "date" : return stylize(obj.toUTCString());
+ case "date" : return stylize(obj.toString(),'date');
case "array" : return stringifyArray(obj, options, stack.length);
case "object" : return stringifyObject(obj, options, stack.length);
}
@@ -133,13 +167,67 @@ function stringify(obj, options) {
return result;
};
+function htmlspecialchars (string, quote_style, charset, double_encode) {
+ var optTemp = 0,
+ i = 0,
+ noquotes = false;
+ if (typeof quote_style === 'undefined' || quote_style === null) {
+ quote_style = 2;
+ }
+ string = string.toString();
+ if (double_encode !== false) {
+ string = string.replace(/&/g, '&amp;');
+ }
+ string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');
+// console.log(string);
+ var OPTS = {
+ 'ENT_NOQUOTES': 0,
+ 'ENT_HTML_QUOTE_SINGLE': 1,
+ 'ENT_HTML_QUOTE_DOUBLE': 2,
+ 'ENT_COMPAT': 2,
+ 'ENT_QUOTES': 3,
+ 'ENT_IGNORE': 4
+ };
+ if (quote_style === 0) {
+ noquotes = true;
+ }
+ if (typeof quote_style !== 'number') {
+ quote_style = [].concat(quote_style);
+ for (var i = 0; i < quote_style.length; i++) {
+ if (OPTS[quote_style[i]] === 0) {
+ noquotes = true;
+ }
+ else if (OPTS[quote_style[i]]) {
+ optTemp = optTemp | OPTS[quote_style[i]];
+ }
+ }
+ quote_style = optTemp;
+ }
+ if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
+ string = string.replace(/'/g, '&#039;');
+ }
+ if (!noquotes) {
+ string = string.replace(/"/g, '&quot;');
+ }
+ return string;
+}
+
// Escape invisible characters in a string
function stringifyString (str, options) {
- return str.replace(/\\/g, '\\\\')
- .replace(/\n/g, '\\n')
- .replace(/[\u0001-\u001F]/g, function (match) {
- return '\\0' + match[0].charCodeAt(0).toString(8);
- });
+ if (options.escape) {
+ var result=str./*replace(/\\/g, '\\\\').*/replace(/\n/g, '\\n').replace(/\t/g, '\\t').replace(/[\u0001-\u001F]/g, function (match) {
+ return '\\0' + match[0].charCodeAt(0).toString(8);
+ });
+ if (options.html) {
+// console.log(options);
+ result=htmlspecialchars(result);
+// console.log(result);
+// process.exit();
+ }
+ return result;
+ } else {
+ return str;
+ }
}
// Convert an array to a string, such as [1, 2, 3].
@@ -148,7 +236,7 @@ function stringifyString (str, options) {
function stringifyArray(ary, options, level) {
var out = [];
var pretty = options.pretty && (ary.length > 4 || ary.some(function (o) {
- return (typeof(o) === 'object' && Object.keys(o).length > 0) ||
+ return (typeof(o) === 'object' && o && Object.keys(o).length > 0) ||
(Array.isArray(o) && o.length > 0);
}));
var ws = pretty ? '\n' + new(Array)(level * 4 + 1).join(' ') : ' ';
@@ -167,20 +255,37 @@ function stringifyArray(ary, options, level) {
}
};
+var quote_reserved_words = function(word) {
+ var reserved_words=['abstract', 'else', 'instanceof', 'switch', 'boolean', 'enum', 'int', 'synchronized', 'break', 'export', 'interface', 'this', 'byte', 'extends', 'long', 'throw', 'case', 'false', 'native', 'throws', 'catch', 'final', 'new', 'transient', 'char', 'finally', 'null', 'true', 'class', 'float', 'package', 'try', 'const', 'for', 'private', 'typeof', 'continue', 'function', 'protected', 'var', 'debugger', 'goto', 'public', 'void', 'default', 'if', 'return', 'volatile', 'delete', 'implements', 'short', 'while', 'do', 'import', 'static', 'with', 'double', 'in', 'super'];
+ return function(word) {
+ for (var i in reserved_words) {
+ if (word === reserved_words[i]) {
+ return "'" + word + "'";
+ }
+ }
+ return word;
+ }
+}();
+
+
// Convert an object to a string, such as {a: 1}.
// This function calls stringify() for each of its values,
// and does not output functions or prototype values.
function stringifyObject(obj, options, level) {
+ if ( obj instanceof Buffer) return 'Buffer';
var out = [];
var pretty = options.pretty && (Object.keys(obj).length > 2 ||
Object.keys(obj).some(function (k) { return typeof(obj[k]) === 'object' }));
var ws = pretty ? '\n' + new(Array)(level * 4 + 1).join(' ') : ' ';
Object.keys(obj).forEach(function (k) {
- if (obj.hasOwnProperty(k) && !(obj[k] instanceof Function && options.hideFunctions)) {
- out.push(eyes.stylize(k, options.styles.key, options.styles) + ': ' +
- stringify(obj[k], options));
- }
+ if (!(level== 1 && options.exclude && ~options.exclude.indexOf(k))) {
+ if (Object.prototype.hasOwnProperty.call(obj,k) && !(obj[k] instanceof Function && options.hideFunctions)) {
+
+ out.push((options.json?'"':'')+eyes.stylize(options.json?k:quote_reserved_words(k), options.styles.key, options) + (options.json?'"':'') + ': ' +
+ stringify(obj[k], options));
+ }
+ }
});
if (out.length === 0) {
View
2  package.json
@@ -8,7 +8,7 @@
"licenses" : ["MIT"],
"dependencies" : [],
"main" : "./lib/eyes",
- "version" : "0.1.6",
+ "version" : "0.1.6jorisc",
"directories" : { "lib": "./lib", "test": "./test" },
"engines" : { "node": "> 0.1.90" }
}
View
18 test/eyes-test.js
@@ -2,6 +2,7 @@ var sys = require('sys');
var eyes = require('../lib/eyes');
eyes.inspect({
+ html:true,
number: 42,
string: "John Galt",
regexp: /[a-z]+/,
@@ -10,7 +11,8 @@ eyes.inspect({
bool: false,
nil: null,
undef: undefined,
- object: {attr: []}
+ object: {attr: []},
+ buffer: new Buffer('joris'),
}, "native types");
eyes.inspect({
@@ -27,10 +29,10 @@ var obj = {};
obj.that = { self: obj };
obj.self = obj;
-eyes.inspect(obj, "circular object");
+eyes.inspect(obj, "circular\nobject");
eyes.inspect({hello: 'moto'}, "small object");
eyes.inspect({hello: new(Array)(6) }, "big object");
-eyes.inspect(["hello 'world'", 'hello "world"'], "quotes");
+eyes.inspect(["hello 'world'", 'hello\t"world"'], "quotes");
eyes.inspect({
recommendations: [{
id: 'a7a6576c2c822c8e2bd81a27e41437d8',
@@ -47,9 +49,17 @@ eyes.inspect({
}, 'complex');
-var inspect = eyes.inspector({ stream: null });
+var inspect = eyes.inspector({ stream: null,html:true });
sys.puts(inspect('something', "something"));
sys.puts(inspect("something else"));
sys.puts(inspect(["no color"], null, { styles: false }));
+
+eyes.inspect({
+ default: 1234,
+}, "quoted_reserved_words");
+
+
+inspect_html = eyes.inspector({ html:true });
+inspect_html('<i>Joris</i>=<b>GEK</b>');
Something went wrong with that request. Please try again.