Skip to content

Commit bad51d0

Browse files
committed
[Refactor] use array.prototype.flat object.values over .reduce
1 parent 3baaf76 commit bad51d0

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
"license": "MIT",
6464
"dependencies": {
6565
"array-includes": "^3.1.6",
66-
"object.assign": "^4.1.4"
66+
"array.prototype.flat": "^1.3.1",
67+
"object.assign": "^4.1.4",
68+
"object.values": "^1.1.6"
6769
},
6870
"auto-changelog": {
6971
"output": "CHANGELOG.md",

src/eventHandlers.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import flat from 'array.prototype.flat';
2+
import values from 'object.values';
3+
14
/**
25
* Common event handlers for JSX element event binding.
36
*/
@@ -102,11 +105,6 @@ const eventHandlersByType = {
102105
],
103106
};
104107

105-
const eventHandlers = Object.keys(eventHandlersByType).reduce(
106-
(accumulator, type) => accumulator.concat(eventHandlersByType[type]),
107-
[],
108-
);
109-
110-
export default eventHandlers;
108+
export default flat(values(eventHandlersByType));
111109

112110
export { eventHandlersByType };

src/values/expressions/ObjectExpression.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ export default function extractValueFromObjectExpression(value) {
1010
// eslint-disable-next-line global-require
1111
const getValue = require('.').default;
1212
return value.properties.reduce((obj, property) => {
13-
const object = { ...obj };
1413
// Support types: SpreadProperty and ExperimentalSpreadProperty
1514
if (/^(?:Experimental)?Spread(?:Property|Element)$/.test(property.type)) {
1615
if (property.argument.type === 'ObjectExpression') {
17-
return assign(object, extractValueFromObjectExpression(property.argument));
16+
return assign({}, obj, extractValueFromObjectExpression(property.argument));
1817
}
1918
} else {
20-
object[getValue(property.key)] = getValue(property.value);
19+
return assign({}, obj, { [getValue(property.key)]: getValue(property.value) });
2120
}
22-
return object;
21+
return obj;
2322
}, {});
2423
}

src/values/expressions/TemplateLiteral.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,19 @@ export default function extractValueFromTemplateLiteral(value) {
1717
} = value;
1818
const partitions = quasis.concat(expressions);
1919

20-
return partitions.sort(sortStarts).reduce((raw, part) => {
21-
const {
22-
type,
23-
} = part;
20+
return partitions.sort(sortStarts).map(({ type, value: { raw } = {}, name }) => {
2421
if (type === 'TemplateElement') {
25-
return raw + part.value.raw;
22+
return raw;
2623
}
2724

2825
if (type === 'Identifier') {
29-
return part.name === 'undefined' ? `${raw}${part.name}` : `${raw}{${part.name}}`;
26+
return name === 'undefined' ? name : `{${name}}`;
3027
}
3128

3229
if (type.indexOf('Expression') > -1) {
33-
return `${raw}{${type}}`;
30+
return `{${type}}`;
3431
}
3532

36-
return raw;
37-
}, '');
33+
return '';
34+
}).join('');
3835
}

0 commit comments

Comments
 (0)