Skip to content

Commit

Permalink
Merge pull request #8 from chenglou/fix-falsy
Browse files Browse the repository at this point in the history
Fix behavior for non-zero params & empty string
  • Loading branch information
JedWatson committed Feb 14, 2015
2 parents 3cd4c46 + bd1e344 commit c632947
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ Install with npm, or download the [UMD version](http://wzrd.in/standalone/classn
npm install classnames
```

The `classnames` function takes any number of arguments which can be a string or object.
The argument `'foo'` is short for `{foo: true}`. If the value of the key is falsy, it won't be included in the output.
The `classNames` function takes any number of arguments which can be a string or object.
The argument `'foo'` is short for `{foo: true}`. If the value of the key is falsy, it won't be included in the output.

```js
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'); // => '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, {baz: null}, ''); // => 'bar'
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'
classNames.apply(null, array); // => 'foo bar'
```
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ function classNames() {

for (var i = 0; i < args.length; i++) {
var arg = args[i];
if (arg == null) {
if (!arg) {
continue;
}

if ('string' === typeof arg) {
if ('string' === typeof arg || 'number' === typeof arg) {
classes.push(arg);
} else if ('object' === typeof arg) {
for (var key in arg) {
Expand Down
8 changes: 6 additions & 2 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ describe('classNames', function() {
}), 'a f');
});

it('joins arrays of class names and ignore non-string values', function() {
assert.equal(classNames('a', 0, null, undefined, true, 1, 'b'), 'a b');
it('joins arrays of class names and ignore falsy values', function() {
assert.equal(classNames('a', 0, null, undefined, true, 1, 'b'), 'a 1 b');
});

it('supports heterogenous arguments', function() {
assert.equal(classNames({a: true}, 'b', 0), 'a b');
});

it('should be trimmed', function() {
assert.equal(classNames('', 'b', {}, ''), 'b');
});

it('returns an empty string for an empty configuration', function() {
assert.equal(classNames({}), '');
});
Expand Down

0 comments on commit c632947

Please sign in to comment.