-
Notifications
You must be signed in to change notification settings - Fork 19
/
helper.simplifyNode.js
127 lines (106 loc) · 3.09 KB
/
helper.simplifyNode.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*eslint func-names: 0, no-magic-numbers:0 */
/*global describe, it */
const helper = require('../lib/helper');
const should = require('chai').should();
describe('helper.simplifyNode()', () => {
it('should return the already simplified', () => {
should.not.exist(helper.simplifyNode(null));
helper.simplifyNode('simple').should.equal('simple');
});
it('should return only text when really simple', () => {
const input = {
$name: 'title',
$attrs: {},
$text: 'this is a title'
};
helper.simplifyNode(input, true).should.equal('this is a title');
helper.simplifyNode(input).should.not.equal('this is a title');
});
it('should return only attributes when really simple', () => {
const input = {
$name: 'div',
$attrs: { id: '34', type: 'thing' }
};
helper.simplifyNode(input).should.deep.equal({ $name: 'div', id: '34', type: 'thing' });
helper.simplifyNode(input, true).should.deep.equal({ id: '34', type: 'thing' });
});
it('should drop unecessary properties', () => {
const input = {
$name: 'title',
$attrs: { id: '34' },
$text: null
};
const output = {
$name: 'title',
id: '34'
};
helper.simplifyNode(input).should.deep.equal(output);
helper.simplifyNode(input, true).should.deep.equal(input.$attrs);
});
it('should not oversimplify empty nodes', () => {
const input = {
$name: 'title',
$text: null
};
const output = {
$name: 'title'
};
helper.simplifyNode(input).should.deep.equal(output);
});
it('should simplify $markup', () => {
const input = {
$name: 'title',
$attrs: {},
$markup: [{ $name: 'p', $attrs: {}, $markup: [ 'stuff' ]}]
};
const output = {
$name: 'title',
$markup: [{ $name: 'p', $markup: [ 'stuff' ]}]
};
helper.simplifyNode(input).should.deep.equal(output);
});
it('should strip single-element arrays', () => {
helper.simplifyNode({
$name: 'tag',
stuff: [ 'test' ]
}).should.deep.equal({
$name: 'tag',
stuff: 'test'
});
helper.simplifyNode([ 'test' ]).should.equal('test');
});
it('should preserve arrays when asked', () => {
helper.simplifyNode({
$name: 'tag',
stuff: [ 'test' ]
}, false, true).should.deep.equal({
$name: 'tag',
stuff: [ 'test' ]
});
helper.simplifyNode([ 'test' ], false, true).should.deep.equal([ 'test' ]);
});
it('should simplify arrays as arrays', () => {
const input = {
items: [{ id: 1 }, { id: 2 }]
};
const output = [
{ id: 1 }, { id: 2 }
];
helper.simplifyNode(input).should.deep.equal(output);
});
it('should not simplify when things get interesting', () => {
const input = {
$name: 'header',
$attrs: { id: '3' },
$markup: [ 'some text' ]
};
const output = {
$name: 'header',
$attrs: { id: '3' },
$markup: [ 'some text' ]
};
helper.simplifyNode(input).should.deep.equal(output);
delete output.$name;
helper.simplifyNode(input, true).should.deep.equal(output);
});
});