Skip to content

Commit bcd80ef

Browse files
committed
refactor: use native for loops instead of lodash/each
1 parent 1bab701 commit bcd80ef

3 files changed

Lines changed: 49 additions & 36 deletions

File tree

src/process-attributes.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const keys = require('lodash/keys');
88
const union = require('lodash/union');
99
const pick = require('lodash/pick');
1010
const difference = require('lodash/difference');
11-
const each = require('lodash/each');
1211
const has = require('lodash/has');
1312
const extend = require('lodash/extend');
1413
const isString = require('lodash/isString');
@@ -52,15 +51,16 @@ module.exports = (currentNode, attributes, props, options, aware) => {
5251

5352
// Merge or override elementAttributes from options provided
5453
if (!isEmpty(options.elementAttributes)) {
55-
each(options.elementAttributes, (modifier, tagName) => {
54+
for (const tagName in options.elementAttributes) {
55+
const modifier = options.elementAttributes[tagName];
5656
if (typeof modifier === 'function' && isString(tagName)) {
57-
tagName = tagName.toUpperCase();
58-
const attributes = modifier(validAttributes.elementAttributes[tagName]);
57+
const upperTagName = tagName.toUpperCase();
58+
const attributes = modifier(validAttributes.elementAttributes[upperTagName]);
5959
if (Array.isArray(attributes)) {
60-
validAttributes.elementAttributes[tagName] = attributes;
60+
validAttributes.elementAttributes[upperTagName] = attributes;
6161
}
6262
}
63-
});
63+
}
6464
}
6565

6666
// Attributes to be excluded
@@ -76,15 +76,18 @@ module.exports = (currentNode, attributes, props, options, aware) => {
7676
const mainNodeAttributes = pick(attributes, validElementAttributes);
7777

7878
// Get additional specified attributes
79-
each(attributes, (value, attr) => {
80-
each(validAttributes.safelistAttributes, additionalAttr => {
81-
if (additionalAttr === attr || (additionalAttr.endsWith('*') && attr.startsWith(additionalAttr.replace('*', '')))) {
82-
mainNodeAttributes[attr] = value;
79+
for (const attr in attributes) {
80+
for (const additionalAttr of validAttributes.safelistAttributes) {
81+
if (
82+
additionalAttr === attr
83+
|| (additionalAttr.endsWith('*') && attr.startsWith(additionalAttr.replace('*', '')))
84+
) {
85+
mainNodeAttributes[attr] = attributes[attr];
8386
}
84-
});
85-
});
87+
}
88+
}
8689

87-
each(mainNodeAttributes, (value, key) => {
90+
for (const key in mainNodeAttributes) {
8891
if (['class', 'style'].includes(key)) {
8992
if (!has(nodeAttrs, key)) {
9093
nodeAttrs[key] = key === 'class' ? [] : {};
@@ -100,16 +103,16 @@ module.exports = (currentNode, attributes, props, options, aware) => {
100103
}
101104

102105
delete attributes[key];
103-
});
106+
}
104107

105108
// The plugin posthtml-attrs-parser compose() method expects a string,
106109
// but since we are JSON parsing, values like "-1" become number -1.
107110
// So below we convert non string values to string.
108-
each(nodeAttrs, (value, key) => {
111+
for (const key in nodeAttrs) {
109112
if (key !== 'compose' && !isObject(nodeAttrs[key]) && !isString(nodeAttrs[key])) {
110113
nodeAttrs[key] = nodeAttrs[key].toString();
111114
}
112-
});
115+
}
113116

114117
mainNode.attrs = nodeAttrs.compose();
115118
};

src/process-props.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict';
22

3-
const processScript = require('./process-script');
43
const pick = require('lodash/pick');
5-
const each = require('lodash/each');
64
const assign = require('lodash/assign');
75
const mergeWith = require('lodash/mergeWith');
6+
const processScript = require('./process-script');
87

98
const attributeTypes = ['aware', 'merge'];
109

@@ -23,31 +22,31 @@ module.exports = (currentNode, nextNode, filledSlots, options, componentPath, pr
2322
let attributes = {...currentNode.attrs};
2423

2524
const attributesByTypeName = {};
26-
each(attributeTypes, type => {
25+
for (const type of attributeTypes) {
2726
attributesByTypeName[type] = [];
28-
});
27+
}
2928

30-
each(attributes, (value, key, attrs) => {
29+
for (const key in attributes) {
3130
let newKey = key;
32-
each(attributeTypes, type => {
31+
for (const type of attributeTypes) {
3332
if (key.startsWith(`${type}:`)) {
3433
newKey = newKey.replace(`${type}:`, '');
3534
attributesByTypeName[type].push(newKey);
3635
}
37-
});
36+
}
3837

3938
if (newKey !== key) {
40-
attrs[newKey] = value;
41-
delete attrs[key];
39+
attributes[newKey] = attributes[key];
40+
delete attributes[key];
4241
}
43-
});
42+
}
4443

4544
// Parse JSON attributes
46-
each(attributes, (value, key, attrs) => {
45+
for (const key in attributes) {
4746
try {
48-
attrs[key] = JSON.parse(value);
47+
attributes[key] = JSON.parse(attributes[key]);
4948
} catch {}
50-
});
49+
}
5150

5251
// Merge or extend attribute props
5352
if (attributes[options.propsAttribute]) {
@@ -65,7 +64,17 @@ module.exports = (currentNode, nextNode, filledSlots, options, componentPath, pr
6564
attributes = mergeWith({}, options.expressions.locals, attributes, options.mergeCustomizer);
6665

6766
// Process props from <script props>
68-
const {props} = processScript(nextNode, {props: {...attributes}, $slots: filledSlots, propsScriptAttribute: options.propsScriptAttribute, propsContext: options.propsContext, utilities: options.utilities}, componentPath.replace(`.${options.fileExtension}`, '.js'));
67+
const {props} = processScript(
68+
nextNode,
69+
{
70+
props: {...attributes},
71+
$slots: filledSlots,
72+
propsScriptAttribute: options.propsScriptAttribute,
73+
propsContext: options.propsContext,
74+
utilities: options.utilities
75+
},
76+
componentPath.replace(`.${options.fileExtension}`, '.js')
77+
);
6978

7079
if (props) {
7180
assign(attributes, props);

src/process-slots.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const {match} = require('posthtml/lib/api');
44
const {render} = require('posthtml-render');
5-
const each = require('lodash/each');
65
const omit = require('lodash/omit');
76

87
/**
@@ -25,11 +24,13 @@ function setFilledSlots(currentNode, filledSlots, {fill, slotSeparator, propsSlo
2524
const props = omit(fillNode.attrs, ['append', 'prepend', 'aware']);
2625

2726
if (props) {
28-
each(props, (value, key, attrs) => {
29-
try {
30-
attrs[key] = JSON.parse(value);
31-
} catch {}
32-
});
27+
for (const key in props) {
28+
if (props.hasOwnProperty(key)) {
29+
try {
30+
props[key] = JSON.parse(props[key]);
31+
} catch {}
32+
}
33+
}
3334
}
3435

3536
filledSlots[name] = {

0 commit comments

Comments
 (0)