Skip to content

Commit

Permalink
Computed attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
thewebartisan7 committed Oct 14, 2022
1 parent b89c90a commit 049f153
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ root = true

[*]
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
charset = utf-8
quote_type = single
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,18 @@ function parseLocals(options, {attrs}, html) {
// Handle attributes to be merged with default
// only for Array or Objects
const mergeAttributeWithDefault = [];
const computedAttributes = [];
Object.keys(attributes).forEach(attribute => {
if (attribute.startsWith('merge:')) {
const newAttributeName = attribute.replace('merge:', '');
attributes[newAttributeName] = attributes[attribute];
delete attributes[attribute];
mergeAttributeWithDefault.push(newAttributeName);
} else if (attribute.startsWith('computed:')) {
const newAttributeName = attribute.replace('computed:', '');
attributes[newAttributeName] = attributes[attribute];
delete attributes[attribute];
computedAttributes.push(newAttributeName);
}
});

Expand Down Expand Up @@ -152,6 +158,8 @@ function parseLocals(options, {attrs}, html) {
Object.keys(locals).forEach(local => {
if (mergeAttributeWithDefault.includes(local)) {
attributes[local] = merge({...locals[local]}, {...attributes[local]});
} else if (computedAttributes.includes(local)) {
attributes[local] = locals[local];
} else if (typeof attributes[local] === 'undefined') {
attributes[local] = locals[local];
}
Expand Down

0 comments on commit 049f153

Please sign in to comment.