|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -var parse = require('space-separated-tokens').parse |
| 3 | +var spaceSeparatedTokens = require('space-separated-tokens') |
4 | 4 |
|
5 | 5 | module.exports = classnames
|
6 | 6 |
|
7 | 7 | // A bit inspired by <https://github.com/JedWatson/classnames>, but for hast.
|
8 | 8 | function classnames(node) {
|
| 9 | + var map = Object.create(null) |
| 10 | + var list = [] |
9 | 11 | 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 |
15 | 13 | var key
|
16 |
| - var map |
17 | 14 |
|
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') |
21 | 17 |
|
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 = {} |
27 | 19 |
|
28 |
| - do { |
29 |
| - add(map, conditional) |
30 |
| - conditional = arguments[++index] |
31 |
| - } while (index < length) |
| 20 | + if (node.properties.className) add(map, node.properties.className) |
32 | 21 |
|
33 |
| - list = [] |
| 22 | + node.properties.className = list |
34 | 23 |
|
35 |
| - for (key in map) { |
36 |
| - if (map[key]) { |
37 |
| - list.push(key) |
38 |
| - } |
| 24 | + index++ |
39 | 25 | }
|
40 | 26 |
|
41 |
| - if (mutate) { |
42 |
| - props.className = list |
43 |
| - return node |
| 27 | + while (++index < arguments.length) { |
| 28 | + add(map, arguments[index]) |
44 | 29 | }
|
45 | 30 |
|
46 |
| - return list |
| 31 | + for (key in map) { |
| 32 | + if (map[key]) list.push(key) |
| 33 | + } |
| 34 | + |
| 35 | + return mutate ? node : list |
47 | 36 | }
|
48 | 37 |
|
49 | 38 | function add(result, conditional) {
|
50 |
| - var kind = typeof conditional |
51 |
| - var index |
52 |
| - var length |
| 39 | + var index = -1 |
53 | 40 | var key
|
54 | 41 |
|
55 |
| - if (kind === 'number') { |
| 42 | + if (typeof conditional === 'number') { |
56 | 43 | 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) |
61 | 46 |
|
62 |
| - while (++index < length) { |
| 47 | + while (++index < conditional.length) { |
63 | 48 | result[conditional[index]] = true
|
64 | 49 | }
|
65 |
| - } else if (kind === 'object') { |
| 50 | + } else if (conditional && typeof conditional === 'object') { |
66 | 51 | if ('length' in conditional) {
|
67 |
| - index = -1 |
68 |
| - length = conditional.length |
69 |
| - |
70 |
| - while (++index < length) { |
| 52 | + while (++index < conditional.length) { |
71 | 53 | add(result, conditional[index])
|
72 | 54 | }
|
73 | 55 | } else {
|
74 | 56 | for (key in conditional) {
|
75 |
| - result[key] = Boolean(conditional[key]) |
| 57 | + result[key] = conditional[key] |
76 | 58 | }
|
77 | 59 | }
|
78 | 60 | }
|
|
0 commit comments