Comparing version 0.3.1 to 0.3.2
@@ -98,4 +98,12 @@ /** | ||
sanitizeText: function sanitizeText(text) { | ||
if (typeof text === 'undefined' || typeof text === 'null') { | ||
text = typeof text; | ||
} | ||
else if (typeof text === 'object') { | ||
text = JSON.stringify(text, null, 2); | ||
} | ||
// Make it a string | ||
text = text.toString(); | ||
else text = text.toString(); | ||
@@ -102,0 +110,0 @@ // Escape pipes, backslashes, and equals signs that have not |
{ | ||
"name": "cef", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"main" : "lib/cef.js", | ||
@@ -5,0 +5,0 @@ "author": "Jed Parsons <https://github.com/jedp>", |
@@ -6,56 +6,85 @@ var vows = require('vows'); | ||
function handles(input, expected) { | ||
var context = { | ||
topic: function() { | ||
return this.context.name; | ||
} | ||
}; | ||
context["works"] = function(expected) { | ||
return function(err) { | ||
var formatter = new cef.Formatter(); | ||
assert(formatter.sanitizeText(input) === expected); | ||
}; | ||
}; | ||
return context; | ||
} | ||
var suite = vows.describe("Formatter") | ||
.addBatch({ | ||
"We escape": { | ||
"The text sanitizer": { | ||
topic: function() { | ||
return (new cef.Formatter()).sanitizeText("|I=flan\r|you=pie\r\n"); | ||
var formatter = new cef.Formatter(); | ||
var f = formatter.sanitizeText; | ||
return f(f(f("=I | like pie = glug\r\n\r\r\n"))); | ||
}, | ||
"pipes": function(text) { | ||
assert(! /[^\\]\|/.test(text)); | ||
}, | ||
"is idempotent": function(text) { | ||
assert(text === "\\=I \\| like pie \\= glug\n"); | ||
} | ||
}, | ||
"equals signs": function(text) { | ||
assert(! /[^\\]=/.test(text)); | ||
"Sanitizing a null value": { | ||
topic: function() { | ||
return (new cef.Formatter()).sanitizeText(null); | ||
}, | ||
"backslashes": function(text) { | ||
assert(! /[^\\]\\[^|=]/.test(text)); | ||
"yields 'null'": function(text) { | ||
assert(text === 'null'); | ||
} | ||
}, | ||
"The text sanitizer": { | ||
"Sanitizing an undefined value": { | ||
topic: function() { | ||
var formatter = new cef.Formatter(); | ||
var f = formatter.sanitizeText; | ||
console.log("apply f: " + f("glug| merg")); | ||
return f(f(f("=I | like pie = glug\r\n\r\r\n"))); | ||
var foo = {}; | ||
return (new cef.Formatter()).sanitizeText(foo.undefined); | ||
}, | ||
"is idempotent": function(text) { | ||
assert(text === "\\=I \\| like pie \\= glug\n"); | ||
"yields 'undefined'": function(text) { | ||
assert(text === 'undefined'); | ||
} | ||
}, | ||
"The text sanitizer": { | ||
"Sanitizing an object": { | ||
topic: function() { | ||
return (new cef.Formatter()).sanitizeText("|or else="); | ||
var obj = {"I like": "pie"}; | ||
return (new cef.Formatter()).sanitizeText(obj); | ||
}, | ||
"works on characters at string margins": function(text) { | ||
assert(text === "\\|or else\\="); | ||
"yields a valid JSON string": function(text) { | ||
assert(JSON.parse(text)["I like"] === "pie"); | ||
} | ||
}, | ||
"We collapse various newlines": { | ||
"Sanitizing a number": { | ||
topic: function() { | ||
return (new cef.Formatter()).sanitizeText("I\r\n\r\nlike\r\r\rpie"); | ||
return (new cef.Formatter()).sanitizeText(42); | ||
}, | ||
"to a single \n": function(text) { | ||
assert(text === "I\nlike\npie"); | ||
"yields a string": function(text) { | ||
assert(typeof text === 'string'); | ||
assert(text === "42"); | ||
} | ||
}, | ||
"Sanitizing": { | ||
"pipes": handles("eggman|walrus|", "eggman\\|walrus\\|"), | ||
"equals signs": handles("2+2=4, 4+4=8", "2+2\\=4, 4+4\\=8"), | ||
"backslashes": handles("C:\\blah\\blah", "C:\\\\blah\\\\blah"), | ||
"special characters at string margins": handles("|or else=", "\\|or else\\="), | ||
"multiple new-line characters": handles("I\r\n\r\nlike\r\r\rpie", "I\nlike\npie") | ||
} | ||
}) | ||
.addBatch({ | ||
"We ensure keys": { | ||
@@ -62,0 +91,0 @@ topic: function() { |
39173
1062