Skip to content

Commit

Permalink
[Refactor] use array.prototype.flat object.values over .reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 28, 2023
1 parent 3baaf76 commit bad51d0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
"license": "MIT",
"dependencies": {
"array-includes": "^3.1.6",
"object.assign": "^4.1.4"
"array.prototype.flat": "^1.3.1",
"object.assign": "^4.1.4",
"object.values": "^1.1.6"
},
"auto-changelog": {
"output": "CHANGELOG.md",
Expand Down
10 changes: 4 additions & 6 deletions src/eventHandlers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import flat from 'array.prototype.flat';
import values from 'object.values';

/**
* Common event handlers for JSX element event binding.
*/
Expand Down Expand Up @@ -102,11 +105,6 @@ const eventHandlersByType = {
],
};

const eventHandlers = Object.keys(eventHandlersByType).reduce(
(accumulator, type) => accumulator.concat(eventHandlersByType[type]),
[],
);

export default eventHandlers;
export default flat(values(eventHandlersByType));

export { eventHandlersByType };
7 changes: 3 additions & 4 deletions src/values/expressions/ObjectExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ export default function extractValueFromObjectExpression(value) {
// eslint-disable-next-line global-require
const getValue = require('.').default;
return value.properties.reduce((obj, property) => {
const object = { ...obj };
// Support types: SpreadProperty and ExperimentalSpreadProperty
if (/^(?:Experimental)?Spread(?:Property|Element)$/.test(property.type)) {
if (property.argument.type === 'ObjectExpression') {
return assign(object, extractValueFromObjectExpression(property.argument));
return assign({}, obj, extractValueFromObjectExpression(property.argument));
}
} else {
object[getValue(property.key)] = getValue(property.value);
return assign({}, obj, { [getValue(property.key)]: getValue(property.value) });
}
return object;
return obj;
}, {});
}
15 changes: 6 additions & 9 deletions src/values/expressions/TemplateLiteral.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@ export default function extractValueFromTemplateLiteral(value) {
} = value;
const partitions = quasis.concat(expressions);

return partitions.sort(sortStarts).reduce((raw, part) => {
const {
type,
} = part;
return partitions.sort(sortStarts).map(({ type, value: { raw } = {}, name }) => {
if (type === 'TemplateElement') {
return raw + part.value.raw;
return raw;
}

if (type === 'Identifier') {
return part.name === 'undefined' ? `${raw}${part.name}` : `${raw}{${part.name}}`;
return name === 'undefined' ? name : `{${name}}`;
}

if (type.indexOf('Expression') > -1) {
return `${raw}{${type}}`;
return `{${type}}`;
}

return raw;
}, '');
return '';
}).join('');
}

0 comments on commit bad51d0

Please sign in to comment.