Skip to content

Commit 4bd4e2c

Browse files
committed
perf: according jsinspect, close #132
1 parent 22cb9b1 commit 4bd4e2c

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

src/index.js

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ const optionsDefault = {
1515
}
1616
};
1717

18+
const nodeHasContent = (node, callback) => {
19+
if (typeof node === 'object' && Object.prototype.hasOwnProperty.call(node, 'content')) {
20+
node.content = callback(node.content);
21+
}
22+
};
23+
24+
const nodeHasAttrs = (node, callback) => {
25+
if (
26+
typeof node === 'object' && Object.prototype.hasOwnProperty.call(node, 'attrs')
27+
) {
28+
node.attrs = Object.keys(node.attrs).reduce(callback, {});
29+
}
30+
};
31+
1832
const clean = tree => parser(render(tree))
1933
.filter(node => {
2034
return typeof node === 'object' || (typeof node === 'string' && (node.trim().length !== 0 || /doctype/gi.test(node)));
@@ -29,9 +43,7 @@ const clean = tree => parser(render(tree))
2943

3044
const parseConditional = tree => {
3145
return tree.map(node => {
32-
if (typeof node === 'object' && Object.prototype.hasOwnProperty.call(node, 'content')) {
33-
node.content = parseConditional(node.content);
34-
}
46+
nodeHasContent(node, parseConditional);
3547

3648
if (typeof node === 'string' && /<!(?:--)?\[[\s\S]*?]>/.test(node)) {
3749
const conditional = /^((?:<[^>]+>)?<!(?:--)?\[[\s\S]*?]>(?:<!)?(?:-->)?)([\s\S]*?)(<!(?:--<!)?\[[\s\S]*?](?:--)?>)$/
@@ -123,9 +135,7 @@ const indent = (tree, {rules: {indent, eol, blankLines}}) => {
123135

124136
const attributesBoolean = (tree, {attrs: {boolean}}) => {
125137
const removeAttributeValue = tree => tree.map(node => {
126-
if (typeof node === 'object' && Object.prototype.hasOwnProperty.call(node, 'content')) {
127-
node.content = removeAttributeValue(node.content);
128-
}
138+
nodeHasContent(node, removeAttributeValue);
129139

130140
if (typeof node === 'object' && Object.prototype.hasOwnProperty.call(node, 'attrs')) {
131141
Object.keys(node.attrs).forEach(key => {
@@ -148,9 +158,7 @@ const lowerElementName = (tree, {tags}) => {
148158
tags = tags.map(({name}) => name);
149159

150160
const bypass = tree => tree.map(node => {
151-
if (typeof node === 'object' && Object.prototype.hasOwnProperty.call(node, 'content')) {
152-
node.content = bypass(node.content);
153-
}
161+
nodeHasContent(node, bypass);
154162

155163
if (
156164
typeof node === 'object' &&
@@ -168,16 +176,9 @@ const lowerElementName = (tree, {tags}) => {
168176

169177
const lowerAttributeName = tree => {
170178
const bypass = tree => tree.map(node => {
171-
if (typeof node === 'object' && Object.prototype.hasOwnProperty.call(node, 'content')) {
172-
node.content = bypass(node.content);
173-
}
179+
nodeHasContent(node, bypass);
174180

175-
if (
176-
typeof node === 'object' &&
177-
Object.prototype.hasOwnProperty.call(node, 'attrs')
178-
) {
179-
node.attrs = Object.keys(node.attrs).reduce((previousValue, key) => Object.assign(previousValue, {[key.toLowerCase()]: node.attrs[key]}), {});
180-
}
181+
nodeHasAttrs(node, (previousValue, key) => Object.assign(previousValue, {[key.toLowerCase()]: node.attrs[key]}));
181182

182183
return node;
183184
});
@@ -189,26 +190,19 @@ const eof = (tree, {rules: {eof}}) => eof ? [...tree, eof] : tree;
189190

190191
const mini = (tree, {mini}) => {
191192
const bypass = tree => tree.map(node => {
192-
if (typeof node === 'object' && Object.prototype.hasOwnProperty.call(node, 'content')) {
193-
node.content = bypass(node.content);
194-
}
195-
196-
if (
197-
typeof node === 'object' &&
198-
Object.prototype.hasOwnProperty.call(node, 'attrs')
199-
) {
200-
node.attrs = Object.keys(node.attrs).reduce((previousValue, key) => {
201-
if (
202-
mini.removeAttribute &&
203-
mini.removeAttribute === 'empty' &&
204-
node.attrs[key].length === 0
205-
) {
206-
return previousValue;
207-
}
208-
209-
return Object.assign(previousValue, {[key.toLowerCase()]: node.attrs[key]});
210-
}, {});
211-
}
193+
nodeHasContent(node, bypass);
194+
195+
nodeHasAttrs(node, (previousValue, key) => {
196+
if (
197+
mini.removeAttribute &&
198+
mini.removeAttribute === 'empty' &&
199+
node.attrs[key].length === 0
200+
) {
201+
return previousValue;
202+
}
203+
204+
return Object.assign(previousValue, {[key.toLowerCase()]: node.attrs[key]});
205+
});
212206

213207
return node;
214208
});

0 commit comments

Comments
 (0)