Skip to content

Commit 55d4af6

Browse files
committed
Add new mixed test, update benchmarks, minor tweaks.
1 parent 2a43e59 commit 55d4af6

File tree

5 files changed

+50
-25
lines changed

5 files changed

+50
-25
lines changed

bench/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ npm i && node .
1313
Please be aware that results may vary across browsers and Node.js runtimes. All tests run on a 2.4GHz Intel Core i7 CPU with 16 GB memory.
1414

1515
<pre>
16-
<b>Classcat – Strings × 31,836,467 ops/sec</b>
17-
classNames – Strings × 3,751,299 ops/sec
16+
<p>Classcat – Strings × 20,162,778 ops/sec</p>
17+
classNames – Strings × 3,911,263 ops/sec
1818
Fastest is Classcat – Strings
1919

20-
<b>Classcat – Objects × 6,385,707 ops/sec</b>
21-
classNames – Objects × 3,260,164 ops/sec
20+
<p>Classcat – Objects × 6,270,420 ops/sec</p>
21+
classNames – Objects × 3,294,519 ops/sec
2222
Fastest is Classcat – Objects
2323

24-
<b>Classcat – Strings & Objects × 31,213,754 ops/sec</b>
25-
classNames – Strings & Objects × 3,000,682 ops/sec
24+
<p>Classcat – Strings & Objects × 19,211,576 ops/sec</p>
25+
classNames – Strings & Objects × 3,055,944 ops/sec
2626
Fastest is Classcat – Strings & Objects
2727

28-
<b>Classcat – Mixed × 29,953,843 ops/sec</b>
29-
classNames – Mixed × 2,126,420 ops/sec
28+
<p>Classcat – Mixed × 7,062,106 ops/sec</p>
29+
classNames – Mixed × 2,145,534 ops/sec
3030
Fastest is Classcat – Mixed
3131

32-
<b>Classcat – Arrays × 3,628,801 ops/sec</b>
33-
classNames – Arrays × 915,020 ops/sec
32+
<p>Classcat – Arrays × 2,381,984 ops/sec</p>
33+
classNames – Arrays × 951,920 ops/sec
3434
Fastest is Classcat – Arrays
3535
</pre>

bench/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ require("./fixtures").map((fixed, index, { length }) => {
1212
)
1313
.on("complete", function() {
1414
console.log(
15-
`Fastest is ${this.filter("fastest").map("name")}${index + 1 < length
16-
? "\n"
17-
: ""}`
15+
`Fastest is ${this.filter("fastest").map("name")}${
16+
index + 1 < length ? "\n" : ""
17+
}`
1818
)
1919
})
2020
.run()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "classcat",
33
"description": "0.3 KB JavaScript function for conditionally concatenating CSS class names.",
4-
"version": "2.0.0",
4+
"version": "1.1.3",
55
"main": "dist/classcat.js",
66
"jsnext:main": "src/index.js",
77
"module": "src/index.js",

src/index.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default function cc(classes, prefix) {
22
var value
3-
var className = ""
3+
var classNames = ""
44
var type = typeof classes
55

66
prefix = prefix || ""
@@ -9,23 +9,33 @@ export default function cc(classes, prefix) {
99
return classes ? prefix : ""
1010
}
1111

12-
if ((classes && type === "string") || type === "number") {
13-
return classes
12+
if (type === "string" || type === "number") {
13+
return prefix + (classes || "")
1414
}
1515

16-
if (Array.isArray(classes) && classes.length) {
16+
if (Array.isArray(classes) && classes.length > 0) {
1717
for (var i = 0, len = classes.length; i < len; i++) {
1818
if ((value = cc(classes[i], prefix))) {
19-
className += (className && " ") + prefix + value
19+
classNames += (classNames && " ") + value
2020
}
2121
}
22-
} else {
23-
for (var i in classes) {
24-
if (classes.hasOwnProperty(i) && (value = classes[i])) {
25-
className += (className && " ") + cc(value, prefix + i)
22+
} else if (type === "object") {
23+
for (var key in classes) {
24+
if ((value = cc(classes[key], prefix + key))) {
25+
classNames += (classNames && " ") + value
2626
}
2727
}
2828
}
2929

30-
return className
30+
return classNames
3131
}
32+
33+
/*
34+
case "object":
35+
for (var key in classes) {
36+
if (next = cc(classes[key], prefix + (isNaN(key) ? key : ""))) {
37+
classNames += (classNames ? " " : "") + next;
38+
}
39+
}
40+
return classNames;
41+
*/

tests/index.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cc from "../src"
22

3-
test("falsy", () => {
3+
test("falsey", () => {
44
expect(cc({})).toBe("")
55
expect(cc([])).toBe("")
66
expect(cc(["", null, false, undefined, 0, NaN])).toBe("")
@@ -34,6 +34,21 @@ test("mixed", () => {
3434
).toBe("foo foo-bar foo-baz")
3535
})
3636

37+
test("mixed #2", () => {
38+
expect(
39+
cc({
40+
foo: {
41+
"--bar": [
42+
"--baz",
43+
{
44+
"--qux": true
45+
}
46+
]
47+
}
48+
})
49+
).toEqual("foo--bar--baz foo--bar--qux")
50+
})
51+
3752
test("prefix", () => {
3853
expect(
3954
cc(

0 commit comments

Comments
 (0)