Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Commit

Permalink
Lint that sucker
Browse files Browse the repository at this point in the history
  - Use lodash instead of custom keys function
  • Loading branch information
cjohansen committed Jul 19, 2012
1 parent e3cdec8 commit 5f3d208
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 73 deletions.
23 changes: 23 additions & 0 deletions autolint.js
@@ -0,0 +1,23 @@
module.exports = {
paths: [
"lib/*.js",
"test/*.js"
],
linterOptions: {
node: true,
browser: true,
plusplus: true,
vars: true,
nomen: true,
forin: true,
sloppy: true,
regexp: true,
predef: [
"samsam",
"define",
"assert",
"refute",
"buster"
]
}
};
102 changes: 47 additions & 55 deletions lib/formatio.js
@@ -1,7 +1,9 @@
(typeof define === "function" && define.amd && function (m) { define(["samsam"], m); } ||
typeof module === "object" && function (m) { module.exports = m(require("samsam")); } ||
function (m) { this.formatio = m(this.samsam); }
)(function (samsam) {
((typeof define === "function" && define.amd && function (m) {
define(["samsam", "lodash"], m);
}) || (typeof module === "object" && function (m) {
module.exports = m(require("samsam"), require("lodash"));
}) || function (m) { this.formatio = m(this.samsam, this._); }
)(function (samsam, _) {
"use strict";

var formatio = {
Expand All @@ -16,7 +18,10 @@
specialObjects.push({ object: global, value: "[object global]" });
}
if (typeof document !== "undefined") {
specialObjects.push({ object: document, value: "[object HTMLDocument]" });
specialObjects.push({
object: document,
value: "[object HTMLDocument]"
});
}
if (typeof window !== "undefined") {
specialObjects.push({ object: window, value: "[object Window]" });
Expand All @@ -27,15 +32,17 @@
if (func.displayName) { return func.displayName; }
if (func.name) { return func.name; }
var matches = func.toString().match(/function\s+([^\(]+)/m);
return matches && matches[1] || "";
return (matches && matches[1]) || "";
}

function constructorName(object) {
var name = functionName(object && object.constructor);
var excludes = this.excludeConstructors || formatio.excludeConstructors || [];
var excludes = this.excludeConstructors ||
formatio.excludeConstructors || [];

for (var i = 0, l = excludes.length; i < l; ++i) {
if (typeof excludes[i] == "string" && excludes[i] == name) {
var i, l;
for (i = 0, l = excludes.length; i < l; ++i) {
if (typeof excludes[i] === "string" && excludes[i] === name) {
return "";
} else if (excludes[i].test && excludes[i].test(name)) {
return "";
Expand All @@ -45,58 +52,44 @@
return name;
}

function keys(object) {
var k = Object.keys && Object.keys(object) || [];
if (k.length == 0) {
var includedToString = false;
for (var prop in object) {
if (hasOwn.call(object, prop)) {
if (prop === "toString") { includedToString = true; }
k.push(prop);
}
}
if (!includedToString && hasOwn.call(object, "toString")) {
k.push("toString");
}
}
return k.sort();
}

function isCircular(object, objects) {
if (typeof object !== "object") { return false; }
for (var i = 0, l = objects.length; i < l; ++i) {
var i, l;
for (i = 0, l = objects.length; i < l; ++i) {
if (objects[i] === object) { return true; }
}
return false;
}

function ascii(object, processed, indent) {
if (typeof object === "string") {
var quote = typeof this.quoteStrings !== "boolean" || this.quoteStrings;
var qs = this.quoteStrings;
var quote = typeof qs !== "boolean" || qs;
return processed || quote ? '"' + object + '"' : object;
}

if (typeof object == "function" && !(object instanceof RegExp)) {
if (typeof object === "function" && !(object instanceof RegExp)) {
return ascii.func(object);
}

processed = processed || [];

if (isCircular(object, processed)) { return "[Circular]"; }

if (Object.prototype.toString.call(object) == "[object Array]") {
if (_.isArray(object)) {
return ascii.array.call(this, object, processed);
}

if (!object) { return "" + object; }
if (!object) { return String(object); }
if (samsam.isElement(object)) { return ascii.element(object); }

if (typeof object.toString === "function" &&
object.toString !== Object.prototype.toString) {
object.toString !== Object.prototype.toString) {
return object.toString();
}

for (var i = 0, l = specialObjects.length; i < l; i++) {
var i, l;
for (i = 0, l = specialObjects.length; i < l; i++) {
if (object === specialObjects[i].object) {
return specialObjects[i].value;
}
Expand All @@ -112,8 +105,8 @@
ascii.array = function (array, processed) {
processed = processed || [];
processed.push(array);
var pieces = [];
for (var i = 0, l = array.length; i < l; ++i) {
var i, l, pieces = [];
for (i = 0, l = array.length; i < l; ++i) {
pieces.push(ascii.call(this, array[i], processed));
}
return "[" + pieces.join(", ") + "]";
Expand All @@ -123,11 +116,9 @@
processed = processed || [];
processed.push(object);
indent = indent || 0;
var pieces = [], properties = keys(object), prop, str, obj;
var is = "";
var pieces = [], properties = _.keys(object).sort();
var length = 3;

for (var i = 0, l = indent; i < l; ++i) { is += " "; }
var prop, str, obj, i, l;

for (i = 0, l = properties.length; i < l; ++i) {
prop = properties[i];
Expand All @@ -146,26 +137,26 @@

var cons = constructorName.call(this, object);
var prefix = cons ? "[" + cons + "] " : "";
var is = "";
for (i = 0, l = indent; i < l; ++i) { is += " "; }

return (length + indent) > 80 ?
prefix + "{\n " + is + pieces.join(",\n " + is) + "\n" + is + "}" :
prefix + "{ " + pieces.join(", ") + " }";
if (length + indent > 80) {
return prefix + "{\n " + is + pieces.join(",\n " + is) + "\n" +
is + "}";
}
return prefix + "{ " + pieces.join(", ") + " }";
};

ascii.element = function (element) {
var tagName = element.tagName.toLowerCase();
var attrs = element.attributes, attribute, pairs = [], attrName;

for (var i = 0, l = attrs.length; i < l; ++i) {
attribute = attrs.item(i);
attrName = attribute.nodeName.toLowerCase().replace("html:", "");

if (attrName == "contenteditable" && attribute.nodeValue == "inherit") {
continue;
}

if (!!attribute.nodeValue) {
pairs.push(attrName + "=\"" + attribute.nodeValue + "\"");
var attrs = element.attributes, attr, pairs = [], attrName, i, l, val;

for (i = 0, l = attrs.length; i < l; ++i) {
attr = attrs.item(i);
attrName = attr.nodeName.toLowerCase().replace("html:", "");
val = attr.nodeValue;
if (attrName !== "contenteditable" || val !== "inherit") {
if (!!val) { pairs.push(attrName + "=\"" + val + "\""); }
}
}

Expand All @@ -176,7 +167,8 @@
content = content.substr(0, 20) + "[...]";
}

var res = formatted + pairs.join(" ") + ">" + content + "</" + tagName + ">";
var res = formatted + pairs.join(" ") + ">" + content +
"</" + tagName + ">";

return res.replace(/ contentEditable="inherit"/, "");
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -22,7 +22,8 @@
"test": "./node_modules/buster/bin/buster-test"
},
"dependencies": {
"samsam": "~0.1"
"samsam": "~0.1",
"lodash": "~0.4"
},
"devDependencies": {
"buster": "0.6.2"
Expand Down
45 changes: 28 additions & 17 deletions test/formatio-test.js
@@ -1,6 +1,7 @@
(typeof module === "object" && typeof require === "function" && function (t) {
/*global formatio*/
((typeof module === "object" && typeof require === "function" && function (t) {
t(require("buster"), require("../lib/formatio"));
} || function (t) {
}) || function (t) {
t(buster, formatio);
})(function (buster, formatio) {
buster.testCase("formatio.ascii", {
Expand Down Expand Up @@ -28,11 +29,13 @@
},

"formats regexp using toString": function () {
assert.equals(formatio.ascii(/[a-zA-Z0-9]+\.?/), "/[a-zA-Z0-9]+\\.?/");
assert.equals(formatio.ascii(/[a-zA-Z0-9]+\.?/),
"/[a-zA-Z0-9]+\\.?/");
},

"formats functions with name": function () {
assert.equals(formatio.ascii(function doIt() {}), "function doIt() {}");
var fn = function doIt() {};
assert.equals(formatio.ascii(fn), "function doIt() {}");
},

"formats functions without name": function () {
Expand All @@ -50,8 +53,7 @@
function doIt() {
var i;
function hey() {}
for (; i < 10; i++) {
}
for (i = 0; i < 10; i++) { console.log(i); }
}

assert.equals(formatio.ascii(doIt), "function doIt() {}");
Expand All @@ -73,7 +75,8 @@
assert.equals(str, '["String", 123, /a-z/, null]');

str = formatio.ascii([ohNo, array]);
assert.equals(str, '[function ohNo() {}, ["String", 123, /a-z/, null]]');
assert.equals(str,
'[function ohNo() {}, ["String", 123, /a-z/, null]]');
},

"does not trip on circular arrays": function () {
Expand Down Expand Up @@ -144,7 +147,8 @@

var person = new Person("Christian");

assert.equals(formatio.ascii(person), "[Person] { name: \"Christian\" }");
assert.equals(formatio.ascii(person),
"[Person] { name: \"Christian\" }");
},

"does not include one letter constructors": function () {
Expand All @@ -157,7 +161,7 @@
assert.equals(formatio.ascii(person), "{ name: \"Christian\" }");
},

"includes one letter constructors when configured to do so": function () {
"includes one letter constructors when configured so": function () {
function C(name) {
this.name = name;
}
Expand All @@ -166,7 +170,8 @@
var formatter = buster.create(formatio);
formatter.excludeConstructors = [];

assert.equals(formatter.ascii(person), "[C] { name: \"Christian\" }");
assert.equals(formatter.ascii(person),
"[C] { name: \"Christian\" }");
},

"excludes constructors when configured to do so": function () {
Expand All @@ -181,7 +186,7 @@
assert.equals(formatter.ascii(person), "{ name: \"Christian\" }");
},

"excludes constructors by pattern when configured to do so": function () {
"excludes constructors by pattern when configured so": function () {
function Person(name) { this.name = name; }
function Ninja(name) { this.name = name; }
function Pervert(name) { this.name = name; }
Expand All @@ -193,8 +198,10 @@
formatter.excludeConstructors = [/^Per/];

assert.equals(formatter.ascii(person), "{ name: \"Christian\" }");
assert.equals(formatter.ascii(ninja), "[Ninja] { name: \"Haruhachi\" }");
assert.equals(formatter.ascii(pervert), "{ name: \"Mr. Garrison\" }");
assert.equals(formatter.ascii(ninja),
"[Ninja] { name: \"Haruhachi\" }");
assert.equals(formatter.ascii(pervert),
"{ name: \"Mr. Garrison\" }");
},

"excludes constructors when run on other objects": function () {
Expand Down Expand Up @@ -225,7 +232,8 @@
var object = { someProp: {} };
object.someProp.foo = object;

assert.equals(formatio.ascii(object), "{ someProp: { foo: [Circular] } }");
assert.equals(formatio.ascii(object),
"{ someProp: { foo: [Circular] } }");
},

"formats nested array nicely": function () {
Expand Down Expand Up @@ -301,7 +309,8 @@

"truncates dom element content": function () {
var element = document.createElement("div");
element.innerHTML = "Oh hi! I'm Christian, and this is a lot of content";
element.innerHTML = "Oh hi! I'm Christian, and this " +
"is a lot of content";

assert.equals(formatio.ascii(element),
"<div>Oh hi! I'm Christian[...]</div>");
Expand All @@ -311,10 +320,12 @@
var element = document.createElement("div");
element.id = "anid";
element.lang = "en";
element.innerHTML = "Oh hi! I'm Christian, and this is a lot of content";
element.innerHTML = "Oh hi! I'm Christian, and this " +
"is a lot of content";
var str = formatio.ascii(element);

assert.match(str, /<div (.*)>Oh hi! I'm Christian\[\.\.\.\]<\/div>/);
assert.match(str,
/<div (.*)>Oh hi! I'm Christian\[\.\.\.\]<\/div>/);
assert.match(str, /lang="en"/);
assert.match(str, /id="anid"/);
},
Expand Down

0 comments on commit 5f3d208

Please sign in to comment.