classnames
Advanced tools
Comparing version 1.1.4 to 1.2.0
16
index.js
function classNames() { | ||
var args = arguments; | ||
var classes = []; | ||
var classes = ''; | ||
var arg; | ||
for (var i = 0; i < args.length; i++) { | ||
var arg = args[i]; | ||
for (var i = 0; i < arguments.length; i++) { | ||
arg = arguments[i]; | ||
if (!arg) { | ||
@@ -12,3 +12,5 @@ continue; | ||
if ('string' === typeof arg || 'number' === typeof arg) { | ||
classes.push(arg); | ||
classes += ' ' + arg; | ||
} else if (Object.prototype.toString.call(arg) === '[object Array]') { | ||
classes += ' ' + classNames.apply(null, arg); | ||
} else if ('object' === typeof arg) { | ||
@@ -19,7 +21,7 @@ for (var key in arg) { | ||
} | ||
classes.push(key); | ||
classes += ' ' + key; | ||
} | ||
} | ||
} | ||
return classes.join(' '); | ||
return classes.substr(1); | ||
} | ||
@@ -26,0 +28,0 @@ |
{ | ||
"name": "classnames", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"description": "A simple utility for conditionally joining classNames together", | ||
@@ -13,3 +13,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha tests.js" | ||
"test": "mocha tests.js" | ||
}, | ||
@@ -16,0 +16,0 @@ "keywords": [ |
@@ -17,15 +17,43 @@ Classnames | ||
classNames('foo', 'bar'); // => 'foo bar' | ||
classNames('foo', {bar: true}); // => 'foo bar' | ||
classNames({foo: true}, {bar: true}); // => 'foo bar' | ||
classNames({foo: true, bar: true}); // => 'foo bar' | ||
classNames('foo', { bar: true }); // => 'foo bar' | ||
classNames({ foo: true }, { bar: true }); // => 'foo bar' | ||
classNames({ foo: true, bar: true }); // => 'foo bar' | ||
// lots of arguments of various types | ||
classNames('foo', {bar: true, duck: false}, 'baz', {quux: true}) // => 'foo bar baz quux' | ||
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }) // => 'foo bar baz quux' | ||
// other falsy values are just ignored | ||
classNames(null, false, 'bar', undefined, 0, 1, {baz: null}, ''); // => 'bar 1' | ||
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1' | ||
``` | ||
// if you have an array of these, use apply | ||
var array = ['foo', {bar: true}]; | ||
classNames.apply(null, array); // => 'foo bar' | ||
Arrays will be recursively flattened as per the rules above: | ||
```js | ||
var arr = ['b', { c: true, d: false }]; | ||
classNames('a', arr); // => 'a b c' | ||
``` | ||
## License | ||
(The MIT License) | ||
Copyright (c) 2015 Jed Watson | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
29
tests.js
@@ -31,2 +31,31 @@ var assert = require("assert"); | ||
}); | ||
it('supports an array of class names', function() { | ||
assert.equal(classNames(['a', 'b']), 'a b'); | ||
}); | ||
it('joins array arguments with string arguments', function() { | ||
assert.equal(classNames(['a', 'b'], 'c'), 'a b c'); | ||
assert.equal(classNames('c', ['a', 'b']), 'c a b'); | ||
}); | ||
it('handles multiple array arguments', function() { | ||
assert.equal(classNames(['a', 'b'], ['c', 'd']), 'a b c d'); | ||
}); | ||
it('handles arrays that include falsy and true values', function() { | ||
assert.equal(classNames(['a', 0, null, undefined, false, true, 'b']), 'a b'); | ||
}); | ||
it('handles arrays that include arrays', function() { | ||
assert.equal(classNames(['a', ['b', 'c']]), 'a b c'); | ||
}); | ||
it('handles arrays that include objects', function() { | ||
assert.equal(classNames(['a', {b: true, c: false}]), 'a b'); | ||
}); | ||
it('handles deep array recursion', function() { | ||
assert.equal(classNames(['a', ['b', ['c', {d: true}]]]), 'a b c d'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7488
108
59