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
Expand Up @@ -63,7 +63,9 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"array-includes": "^3.1.6", "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": { "auto-changelog": {
"output": "CHANGELOG.md", "output": "CHANGELOG.md",
Expand Down
10 changes: 4 additions & 6 deletions src/eventHandlers.js
@@ -1,3 +1,6 @@
import flat from 'array.prototype.flat';
import values from 'object.values';

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


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

export default eventHandlers;


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


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


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


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


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

0 comments on commit bad51d0

Please sign in to comment.