Created
March 5, 2014 20:30
-
-
Save parshap/9375939 to your computer and use it in GitHub Desktop.
node inline tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var type = require("core-util-is"); | |
var slice = Array.prototype.slice; | |
// Return a className string from the given arguments | |
// | |
// Example: | |
// | |
// classes("foo", "bar") -> "foo bar" | |
// classes("foo", { bar: true, baz : false }) -> "foo bar" | |
module.exports = function() { | |
return slice.call(arguments) | |
// Parse objects | |
.map(function(arg) { | |
if (type.isObject(arg)) { | |
return parseObject(arg); | |
} | |
return arg; | |
}) | |
// Filter any falsey values | |
.filter(Boolean) | |
.join(" "); | |
}; | |
function parseObject(obj) { | |
return Object.keys(obj).reduce(function(acc, cur) { | |
if (obj[cur]) { | |
acc.push(cur); | |
} | |
return acc; | |
}, []).join(" "); | |
} | |
// Tests | |
if (process.env.NODE_ENV === "test") { | |
var classes = module.exports; | |
var assert = require("insist"); | |
assert(classes("foo") === "foo"); | |
assert(classes("foo", "bar") === "foo bar"); | |
assert(classes("foo", false, "bar") === "foo bar"); | |
assert(classes("foo", "", "bar") === "foo bar"); | |
assert(classes("foo", undefined, "bar") === "foo bar"); | |
assert(classes({ | |
"foo": true, | |
}) === "foo"); | |
assert(classes({ | |
"foo": true, | |
"bar": true, | |
}) === "foo bar"); | |
assert(classes({ | |
"foo": true, | |
"baz": false, | |
"bar": true, | |
}) === "foo bar"); | |
assert(classes("baz", { | |
"foo": true, | |
"baz": false, | |
"bar": true, | |
}) === "baz foo bar"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment