Skip to content

Commit

Permalink
adhere to semistandard (except for tabs)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Sep 29, 2015
1 parent 395234b commit 722746c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 61 deletions.
21 changes: 10 additions & 11 deletions benchmarks/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ try {
var npm = require('classnames');
var npmDedupe = require('classnames/dedupe');
var npmPackage = require('./node_modules/classnames/package.json');
} catch(e) {
} catch (e) {
console.log('There was an error loading the benchmark classnames package.\n' +
'Please make sure you have run `npm install` in ./benchmarks\n');
process.exit(0);
Expand All @@ -44,13 +44,12 @@ if (localPackage.version !== npmPackage.version) {

var assert = require('assert');
var benchmark = require('benchmark');
benchmark.minTime = 1

function sortClasses(str) {
function sortClasses (str) {
return str.split(' ').sort().join(' ');
}

fixtures.forEach(function(f) {
fixtures.forEach(function (f) {
// sort assertions because dedupe returns results in a different order
assert.equal(sortClasses(local.apply(null, f.args)), sortClasses(f.expected));
assert.equal(sortClasses(dedupe.apply(null, f.args)), sortClasses(f.expected));
Expand All @@ -59,19 +58,19 @@ fixtures.forEach(function(f) {

var suite = new benchmark.Suite();

suite.add('local#' + f.description, function() {
suite.add('local#' + f.description, function () {
local.apply(null, f.args);
});

suite.add(' npm#' + f.description, function() {
suite.add(' npm#' + f.description, function () {
npm.apply(null, f.args);
});

suite.add('local/dedupe#' + f.description, function() {
suite.add('local/dedupe#' + f.description, function () {
dedupe.apply(null, f.args);
});

suite.add(' npm/dedupe#' + f.description, function() {
suite.add(' npm/dedupe#' + f.description, function () {
npmDedupe.apply(null, f.args);
});

Expand All @@ -81,13 +80,13 @@ fixtures.forEach(function(f) {
});

// other handling
suite.on('complete', function() {
suite.on('complete', function () {
console.log('\n> Fastest is' + (' ' + this.filter('fastest').pluck('name').join(' | ')).replace(/\s+/, ' ') + '\n');
});

suite.on('error', function(event) {
suite.on('error', function (event) {
throw event.target.error;
});

suite.run();
})
});
18 changes: 8 additions & 10 deletions dedupe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/
/* global define */

(function () {
'use strict';
Expand All @@ -15,8 +16,8 @@
_parse(resultSet, array[i]);
}
}
var hasOwn = {}.hasOwnProperty

var hasOwn = {}.hasOwnProperty;

function _parseNumber (resultSet, num) {
resultSet[num] = true;
Expand All @@ -27,7 +28,6 @@
if (hasOwn.call(object, k)) {
if (object[k]) {
resultSet[k] = true;

} else {
delete resultSet[k];
}
Expand All @@ -50,43 +50,41 @@
var argType = typeof arg;

// 'foo bar'
if ('string' === argType) {
if (argType === 'string') {
_parseString(resultSet, arg);

// ['foo', 'bar', ...]
} else if (Array.isArray(arg)) {
_parseArray(resultSet, arg);

// { 'foo': true, ... }
} else if ('object' === argType) {
} else if (argType === 'object') {
_parseObject(resultSet, arg);

// '130'
} else if ('number' === argType) {
} else if (argType === 'number') {
_parseNumber(resultSet, arg);
}
}

function _classNames () {
var classSet = {};
_parseArray(classSet, arguments);

return Object.keys(classSet).join(' ');
}

return _classNames;

})();

if (typeof module !== 'undefined' && module.exports) {
module.exports = classNames;
} else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd){
} else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
// AMD. Register as an anonymous module.
define(function () {
return classNames;
});
} else {
window.classNames = classNames;
}

}());
13 changes: 5 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/
/* global define */

(function () {
'use strict';

var hasOwn = {}.hasOwnProperty;

function classNames () {

var classes = '';

for (var i = 0; i < arguments.length; i++) {
Expand All @@ -19,13 +19,11 @@

var argType = typeof arg;

if ('string' === argType || 'number' === argType) {
if (argType === 'string' || argType === 'number') {
classes += ' ' + arg;

} else if (Array.isArray(arg)) {
classes += ' ' + classNames.apply(null, arg);

} else if ('object' === argType) {
} else if (argType === 'object') {
for (var key in arg) {
if (hasOwn.call(arg, key) && arg[key]) {
classes += ' ' + key;
Expand All @@ -39,13 +37,12 @@

if (typeof module !== 'undefined' && module.exports) {
module.exports = classNames;
} else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd){
} else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
// AMD. Register as an anonymous module.
define(function () {
return classNames;
});
} else {
window.classNames = classNames;
}

}());
34 changes: 17 additions & 17 deletions tests/dedupe.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* global describe, it */

var assert = require("assert");
var assert = require('assert');
var dedupe = require('../dedupe');

describe('dedupe', function () {
it('keeps object keys with truthy values', function() {
it('keeps object keys with truthy values', function () {
assert.equal(dedupe({
a: true,
b: false,
c: 0,
d: null,
e: undefined,
f: 1,
f: 1
}), 'a f');
});

Expand All @@ -25,53 +25,53 @@ describe('dedupe', function () {

it('should make sure object with falsy value wipe out previous classes', function () {
assert.equal(dedupe('foo foo', 0, null, undefined, true, 1, 'b', { 'foo': false }), '1 b');
assert.equal(dedupe("foo", "foobar", "bar", { foo: false }), "foobar bar");
assert.equal(dedupe("foo", "foo-bar", "bar", { foo: false }), "foo-bar bar");
assert.equal(dedupe("foo", "-moz-foo-bar", "bar", { foo: false }), "-moz-foo-bar bar");
assert.equal(dedupe('foo', 'foobar', 'bar', { foo: false }), 'foobar bar');
assert.equal(dedupe('foo', 'foo-bar', 'bar', { foo: false }), 'foo-bar bar');
assert.equal(dedupe('foo', '-moz-foo-bar', 'bar', { foo: false }), '-moz-foo-bar bar');
});

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

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

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

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

it('supports an array of class names', function() {
it('supports an array of class names', function () {
assert.equal(dedupe(['a', 'b']), 'a b');
});

it('joins array arguments with string arguments', function() {
it('joins array arguments with string arguments', function () {
assert.equal(dedupe(['a', 'b'], 'c'), 'a b c');
assert.equal(dedupe('c', ['a', 'b']), 'c a b');
});

it('handles multiple array arguments', function() {
it('handles multiple array arguments', function () {
assert.equal(dedupe(['a', 'b'], ['c', 'd']), 'a b c d');
});

it('handles arrays that include falsy and true values', function() {
it('handles arrays that include falsy and true values', function () {
assert.equal(dedupe(['a', 0, null, undefined, false, true, 'b']), 'a b');
});

it('handles arrays that include arrays', function() {
it('handles arrays that include arrays', function () {
assert.equal(dedupe(['a', ['b', 'c']]), 'a b c');
});

it('handles arrays that include objects', function() {
it('handles arrays that include objects', function () {
assert.equal(dedupe(['a', {b: true, c: false}]), 'a b');
});

it('handles deep array recursion', function() {
it('handles deep array recursion', function () {
assert.equal(dedupe(['a', ['b', ['c', {d: true}]]]), 'a b c d');
});
});
30 changes: 15 additions & 15 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
/* global describe, it */

var assert = require("assert");
var assert = require('assert');
var classNames = require('../');

describe('classNames', function() {
it('keeps object keys with truthy values', function() {
describe('classNames', function () {
it('keeps object keys with truthy values', function () {
assert.equal(classNames({
a: true,
b: false,
c: 0,
d: null,
e: undefined,
f: 1,
f: 1
}), 'a f');
});

it('joins arrays of class names and ignore falsy values', function() {
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() {
it('supports heterogenous arguments', function () {
assert.equal(classNames({a: true}, 'b', 0), 'a b');
});

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

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

it('supports an array of class names', function() {
it('supports an array of class names', function () {
assert.equal(classNames(['a', 'b']), 'a b');
});

it('joins array arguments with string arguments', function() {
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() {
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() {
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() {
it('handles arrays that include arrays', function () {
assert.equal(classNames(['a', ['b', 'c']]), 'a b c');
});

it('handles arrays that include objects', function() {
it('handles arrays that include objects', function () {
assert.equal(classNames(['a', {b: true, c: false}]), 'a b');
});

it('handles deep array recursion', function() {
it('handles deep array recursion', function () {
assert.equal(classNames(['a', ['b', ['c', {d: true}]]]), 'a b c d');
});
});

0 comments on commit 722746c

Please sign in to comment.