Skip to content

Commit

Permalink
As linted as it'll ever be
Browse files Browse the repository at this point in the history
  - The tests explicitly compare number and string primitives to
    boxed versions. JsLint does not like this.
  • Loading branch information
cjohansen committed Jul 19, 2012
1 parent 55242ef commit 2bbb45f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
23 changes: 23 additions & 0 deletions autolint.js
Original file line number Diff line number Diff line change
@@ -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,
eqeq: true,
predef: [
"_",
"define",
"assert",
"refute",
"buster"
]
}
};
39 changes: 22 additions & 17 deletions lib/samsam.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
(typeof define === "function" && define.amd && function (m) {
((typeof define === "function" && define.amd && function (m) {
define("lodash", m);
} ||
typeof module === "object" && typeof require === "function" && function (m) {
module.exports = m(require("lodash"));
} ||
function (m) { this.samsam = m(_); }
}) || (typeof module === "object" &&
typeof require === "function" && function (m) {
module.exports = m(require("lodash"));
}) || function (m) { this.samsam = m(_); }
)(function (_) {
var o = Object.prototype;
var div = typeof document !== "undefined" && document.createElement("div");
Expand All @@ -13,7 +12,8 @@
// Unlike global isNaN, this avoids type coercion
// typeof check avoids IE host object issues, hat tip to
// lodash
return typeof value === "number" && value !== value;
var val = value; // JsLint thinks value !== value is "weird"
return typeof value === "number" && value !== val;
}

function getClass(value) {
Expand Down Expand Up @@ -87,10 +87,11 @@
var type1 = typeof obj1;
var type2 = typeof obj2;

// == null also matches undefined
if (obj1 === obj2 ||
isNaN(obj1) || isNaN(obj2) ||
obj1 == null || obj2 == null || // == null also matches undefined
type1 !== "object" || type2 !== "object") {
isNaN(obj1) || isNaN(obj2) ||
obj1 == null || obj2 == null ||
type1 !== "object" || type2 !== "object") {
return identical(obj1, obj2);
}

Expand All @@ -117,7 +118,7 @@
if (obj1.length !== obj2.length) { return false; }
} else {
if (type1 !== type2 || class1 !== class2 ||
keys1.length !== keys2.length) {
keys1.length !== keys2.length) {
return false;
}
}
Expand All @@ -127,14 +128,16 @@
for (i = 0, l = keys1.length; i < l; i++) {
key = keys1[i];
if (!o.hasOwnProperty.call(obj2, key) ||
!deepEqual(obj1[key], obj2[key])) {
!deepEqual(obj1[key], obj2[key])) {
return false;
}
}

return true;
}

var match;

function arrayContains(array, subset) {
var i, l, j, k;
for (i = 0, l = array.length; i < l; ++i) {
Expand All @@ -155,7 +158,7 @@
*
* Compare arbitrary value ``object`` with matcher.
*/
function match(object, matcher) {
match = function match(object, matcher) {
if (matcher && typeof matcher.test === "function") {
return matcher.test(object);
}
Expand All @@ -167,11 +170,12 @@
if (typeof matcher === "string") {
matcher = matcher.toLowerCase();
var notNull = typeof object === "string" || !!object;
return notNull && ("" + object).toLowerCase().indexOf(matcher) >= 0;
return notNull &&
(String(object)).toLowerCase().indexOf(matcher) >= 0;
}

if (typeof matcher === "number") {
return matcher == object;
return matcher === object;
}

if (typeof matcher === "boolean") {
Expand All @@ -183,7 +187,8 @@
}

if (matcher && typeof matcher === "object") {
for (var prop in matcher) {
var prop;
for (prop in matcher) {
if (!match(object[prop], matcher[prop])) {
return false;
}
Expand All @@ -193,7 +198,7 @@

throw new Error("Matcher was not a string, a number, a " +
"function, a boolean or an object");
}
};

return {
isElement: isElement,
Expand Down
2 changes: 1 addition & 1 deletion test/samsam-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ if (typeof module === "object" && typeof require === "function") {
function gather() { return arguments; }
var arrayLike = { length: 4, "0": 1, "1": 2, "2": {}, "3": [] };

pass("arguments to array", [1,2,{},[]], gather(1, 2, {}, []));
pass("arguments to array", [1, 2, {}, []], gather(1, 2, {}, []));
pass("array to arguments", gather(), []);

pass("arguments to array like object",
Expand Down

0 comments on commit 2bbb45f

Please sign in to comment.