Skip to content

Commit 93e5059

Browse files
committed
Refactor to improve bundle size
1 parent e4292c5 commit 93e5059

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

index.js

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,60 @@
11
'use strict'
22

3-
var parse = require('space-separated-tokens').parse
3+
var spaceSeparatedTokens = require('space-separated-tokens')
44

55
module.exports = classnames
66

77
// A bit inspired by <https://github.com/JedWatson/classnames>, but for hast.
88
function classnames(node) {
9+
var map = Object.create(null)
10+
var list = []
911
var mutate = node && typeof node === 'object' && 'type' in node
10-
var length
11-
var index
12-
var conditional
13-
var props
14-
var list
12+
var index = -1
1513
var key
16-
var map
1714

18-
if (mutate && node.type !== 'element') {
19-
throw new Error('Expected element node')
20-
}
15+
if (mutate) {
16+
if (node.type !== 'element') throw new Error('Expected element node')
2117

22-
length = arguments.length
23-
index = mutate ? 0 : -1
24-
props = mutate ? node.properties || (node.properties = {}) : {}
25-
conditional = props.className
26-
map = Object.create(null)
18+
if (!node.properties) node.properties = {}
2719

28-
do {
29-
add(map, conditional)
30-
conditional = arguments[++index]
31-
} while (index < length)
20+
if (node.properties.className) add(map, node.properties.className)
3221

33-
list = []
22+
node.properties.className = list
3423

35-
for (key in map) {
36-
if (map[key]) {
37-
list.push(key)
38-
}
24+
index++
3925
}
4026

41-
if (mutate) {
42-
props.className = list
43-
return node
27+
while (++index < arguments.length) {
28+
add(map, arguments[index])
4429
}
4530

46-
return list
31+
for (key in map) {
32+
if (map[key]) list.push(key)
33+
}
34+
35+
return mutate ? node : list
4736
}
4837

4938
function add(result, conditional) {
50-
var kind = typeof conditional
51-
var index
52-
var length
39+
var index = -1
5340
var key
5441

55-
if (kind === 'number') {
42+
if (typeof conditional === 'number') {
5643
result[conditional] = true
57-
} else if (kind === 'string') {
58-
conditional = parse(conditional)
59-
index = -1
60-
length = conditional.length
44+
} else if (typeof conditional === 'string') {
45+
conditional = spaceSeparatedTokens.parse(conditional)
6146

62-
while (++index < length) {
47+
while (++index < conditional.length) {
6348
result[conditional[index]] = true
6449
}
65-
} else if (kind === 'object') {
50+
} else if (conditional && typeof conditional === 'object') {
6651
if ('length' in conditional) {
67-
index = -1
68-
length = conditional.length
69-
70-
while (++index < length) {
52+
while (++index < conditional.length) {
7153
add(result, conditional[index])
7254
}
7355
} else {
7456
for (key in conditional) {
75-
result[key] = Boolean(conditional[key])
57+
result[key] = conditional[key]
7658
}
7759
}
7860
}

test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ test('hast-util-classnames', function (t) {
2828

2929
t.deepEqual(classnames(h('a'), 123), h('a.123'), 'should support a number')
3030

31+
t.deepEqual(
32+
classnames(h('a'), null, undefined, 2),
33+
h('a.2'),
34+
'should support (ignore) nullish values'
35+
)
36+
3137
t.deepEqual(
3238
classnames(h('a'), {alpha: true, bravo: false, charlie: 1}),
3339
h('a.alpha.charlie'),

0 commit comments

Comments
 (0)