Skip to content

Commit

Permalink
refactor: use native for loops instead of lodash/each
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Nov 29, 2024
1 parent 1bab701 commit bcd80ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
35 changes: 19 additions & 16 deletions src/process-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const keys = require('lodash/keys');
const union = require('lodash/union');
const pick = require('lodash/pick');
const difference = require('lodash/difference');
const each = require('lodash/each');
const has = require('lodash/has');
const extend = require('lodash/extend');
const isString = require('lodash/isString');
Expand Down Expand Up @@ -52,15 +51,16 @@ module.exports = (currentNode, attributes, props, options, aware) => {

// Merge or override elementAttributes from options provided
if (!isEmpty(options.elementAttributes)) {
each(options.elementAttributes, (modifier, tagName) => {
for (const tagName in options.elementAttributes) {
const modifier = options.elementAttributes[tagName];
if (typeof modifier === 'function' && isString(tagName)) {
tagName = tagName.toUpperCase();
const attributes = modifier(validAttributes.elementAttributes[tagName]);
const upperTagName = tagName.toUpperCase();
const attributes = modifier(validAttributes.elementAttributes[upperTagName]);
if (Array.isArray(attributes)) {
validAttributes.elementAttributes[tagName] = attributes;
validAttributes.elementAttributes[upperTagName] = attributes;
}
}
});
}
}

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

// Get additional specified attributes
each(attributes, (value, attr) => {
each(validAttributes.safelistAttributes, additionalAttr => {
if (additionalAttr === attr || (additionalAttr.endsWith('*') && attr.startsWith(additionalAttr.replace('*', '')))) {
mainNodeAttributes[attr] = value;
for (const attr in attributes) {
for (const additionalAttr of validAttributes.safelistAttributes) {
if (
additionalAttr === attr
|| (additionalAttr.endsWith('*') && attr.startsWith(additionalAttr.replace('*', '')))
) {
mainNodeAttributes[attr] = attributes[attr];
}
});
});
}
}

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

delete attributes[key];
});
}

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

mainNode.attrs = nodeAttrs.compose();
};
37 changes: 23 additions & 14 deletions src/process-props.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

const processScript = require('./process-script');
const pick = require('lodash/pick');
const each = require('lodash/each');
const assign = require('lodash/assign');
const mergeWith = require('lodash/mergeWith');
const processScript = require('./process-script');

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

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

const attributesByTypeName = {};
each(attributeTypes, type => {
for (const type of attributeTypes) {
attributesByTypeName[type] = [];
});
}

each(attributes, (value, key, attrs) => {
for (const key in attributes) {
let newKey = key;
each(attributeTypes, type => {
for (const type of attributeTypes) {
if (key.startsWith(`${type}:`)) {
newKey = newKey.replace(`${type}:`, '');
attributesByTypeName[type].push(newKey);
}
});
}

if (newKey !== key) {
attrs[newKey] = value;
delete attrs[key];
attributes[newKey] = attributes[key];
delete attributes[key];
}
});
}

// Parse JSON attributes
each(attributes, (value, key, attrs) => {
for (const key in attributes) {
try {
attrs[key] = JSON.parse(value);
attributes[key] = JSON.parse(attributes[key]);
} catch {}
});
}

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

// Process props from <script props>
const {props} = processScript(nextNode, {props: {...attributes}, $slots: filledSlots, propsScriptAttribute: options.propsScriptAttribute, propsContext: options.propsContext, utilities: options.utilities}, componentPath.replace(`.${options.fileExtension}`, '.js'));
const {props} = processScript(
nextNode,
{
props: {...attributes},
$slots: filledSlots,
propsScriptAttribute: options.propsScriptAttribute,
propsContext: options.propsContext,
utilities: options.utilities
},
componentPath.replace(`.${options.fileExtension}`, '.js')
);

if (props) {
assign(attributes, props);
Expand Down
13 changes: 7 additions & 6 deletions src/process-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const {match} = require('posthtml/lib/api');
const {render} = require('posthtml-render');
const each = require('lodash/each');
const omit = require('lodash/omit');

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

if (props) {
each(props, (value, key, attrs) => {
try {
attrs[key] = JSON.parse(value);
} catch {}
});
for (const key in props) {
if (props.hasOwnProperty(key)) {
try {
props[key] = JSON.parse(props[key]);
} catch {}
}
}
}

filledSlots[name] = {
Expand Down

0 comments on commit bcd80ef

Please sign in to comment.