-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
classnames.js
105 lines (87 loc) · 2.41 KB
/
classnames.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Ported from `classnames` for compatibility checks.
*/
import test from 'tape';
import fn from '../src';
test('(compat) keeps object keys with truthy values', t => {
const out = fn({ a:true, b:false, c:0, d:null, e:undefined, f:1 });
t.is(out, 'a f');
t.end();
});
test('(compat) joins arrays of class names and ignore falsy values', t => {
const out = fn('a', 0, null, undefined, true, 1, 'b');
t.is(out, 'a 1 b');
t.end();
});
test('(compat) supports heterogenous arguments', t => {
t.is(fn({ a:true }, 'b', 0), 'a b');
t.end();
});
test('(compat) should be trimmed', t => {
t.is(fn('', 'b', {}, ''), 'b');
t.end();
});
test('(compat) returns an empty string for an empty configuration', t => {
t.is(fn({}), '');
t.end();
});
test('(compat) supports an array of class names', t => {
t.is(fn(['a', 'b']), 'a b');
t.end();
});
test('(compat) joins array arguments with string arguments', t => {
t.is(fn(['a', 'b'], 'c'), 'a b c');
t.is(fn('c', ['a', 'b']), 'c a b');
t.end();
});
test('(compat) handles multiple array arguments', t => {
t.is(fn(['a', 'b'], ['c', 'd']), 'a b c d');
t.end();
});
test('(compat) handles arrays that include falsy and true values', t => {
t.is(fn(['a', 0, null, undefined, false, true, 'b']), 'a b');
t.end();
});
test('(compat) handles arrays that include arrays', t => {
t.is(fn(['a', ['b', 'c']]), 'a b c');
t.end();
});
test('(compat) handles arrays that include objects', t => {
t.is(fn(['a', { b:true, c:false }]), 'a b');
t.end();
});
test('(compat) handles deep array recursion', t => {
t.is(fn(['a', ['b', ['c', { d:true }]]]), 'a b c d');
t.end();
});
test('(compat) handles arrays that are empty', t => {
t.is(fn('a', []), 'a');
t.end();
});
test('(compat) handles nested arrays that have empty nested arrays', t => {
t.is(fn('a', [[]]), 'a');
t.end();
});
test('(compat) handles all types of truthy and falsy property values as expected', t => {
const out = fn({
// falsy:
null: null,
emptyString: '',
noNumber: NaN,
zero: 0,
negativeZero: -0,
false: false,
undefined: undefined,
// truthy (literally anything else):
nonEmptyString: 'foobar',
whitespace: ' ',
function: Object.prototype.toString,
emptyObject: {},
nonEmptyObject: {a: 1, b: 2},
emptyList: [],
nonEmptyList: [1, 2, 3],
greaterZero: 1
});
t.is(out, 'nonEmptyString whitespace function emptyObject nonEmptyObject emptyList nonEmptyList greaterZero');
t.end();
});