Skip to content

Commit b6413bd

Browse files
Merge pull request #1003 from ssi02014/refac/utilities
refactor(utilities): refactoring Utilities constants
2 parents eaa5c38 + 76d08f3 commit b6413bd

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

lib/utilities.js

+27-26
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ function invertObject(obj, override) {
1313
throw new TypeError('First argument must be an object');
1414
}
1515

16-
var key;
17-
var value;
1816
var isOverridePresent = typeof override === 'function';
1917
var overrides = {};
2018
var result = {};
2119

22-
for (key in obj) {
23-
value = obj[key];
20+
for (var key in obj) {
21+
var value = obj[key];
2422

2523
if (isOverridePresent) {
2624
overrides = override(key, value);
@@ -47,31 +45,34 @@ function invertObject(obj, override) {
4745
* @param {object} props - The props being passed to the element.
4846
* @returns - Whether tag is custom component.
4947
*/
48+
49+
var RESERVED_SVG_MATHML_ELEMENTS = new Set([
50+
'annotation-xml',
51+
'color-profile',
52+
'font-face',
53+
'font-face-src',
54+
'font-face-uri',
55+
'font-face-format',
56+
'font-face-name',
57+
'missing-glyph'
58+
]);
59+
5060
function isCustomComponent(tagName, props) {
5161
if (tagName.indexOf('-') === -1) {
5262
return props && typeof props.is === 'string';
5363
}
54-
55-
switch (tagName) {
56-
// These are reserved SVG and MathML elements.
57-
// We don't mind this whitelist too much because we expect it to never grow.
58-
// The alternative is to track the namespace in a few places which is convoluted.
59-
// https://w3c.github.io/webcomponents/spec/custom/#custom-elements-core-concepts
60-
case 'annotation-xml':
61-
case 'color-profile':
62-
case 'font-face':
63-
case 'font-face-src':
64-
case 'font-face-uri':
65-
case 'font-face-format':
66-
case 'font-face-name':
67-
case 'missing-glyph':
68-
return false;
69-
default:
70-
return true;
64+
// These are reserved SVG and MathML elements.
65+
// We don't mind this whitelist too much because we expect it to never grow.
66+
// The alternative is to track the namespace in a few places which is convoluted.
67+
// https://w3c.github.io/webcomponents/spec/custom/#custom-elements-core-concepts
68+
if (RESERVED_SVG_MATHML_ELEMENTS.has(tagName)) {
69+
return false;
7170
}
71+
return true;
7272
}
7373

74-
var styleToJSOptions = { reactCompat: true };
74+
// styleToJSOptions
75+
var STYLE_TO_JS_OPTIONS = { reactCompat: true };
7576

7677
/**
7778
* Sets style prop.
@@ -84,7 +85,7 @@ function setStyleProp(style, props) {
8485
return;
8586
}
8687
try {
87-
props.style = styleToJS(style, styleToJSOptions);
88+
props.style = styleToJS(style, STYLE_TO_JS_OPTIONS);
8889
} catch (err) {
8990
props.style = {};
9091
}
@@ -98,7 +99,7 @@ var PRESERVE_CUSTOM_ATTRIBUTES = React.version.split('.')[0] >= 16;
9899

99100
// Taken from
100101
// https://github.com/facebook/react/blob/cae635054e17a6f107a39d328649137b83f25972/packages/react-dom/src/client/validateDOMNesting.js#L213
101-
var elementsWithNoTextChildren = new Set([
102+
var ELEMENTS_WITH_NO_TEXT_CHILDREN = new Set([
102103
'tr',
103104
'tbody',
104105
'thead',
@@ -117,7 +118,7 @@ var elementsWithNoTextChildren = new Set([
117118
* @returns - Whether node can contain text nodes.
118119
*/
119120
function canTextBeChildOfNode(node) {
120-
return !elementsWithNoTextChildren.has(node.name);
121+
return !ELEMENTS_WITH_NO_TEXT_CHILDREN.has(node.name);
121122
}
122123

123124
function returnFirstArg(arg) {
@@ -126,10 +127,10 @@ function returnFirstArg(arg) {
126127

127128
module.exports = {
128129
PRESERVE_CUSTOM_ATTRIBUTES: PRESERVE_CUSTOM_ATTRIBUTES,
130+
ELEMENTS_WITH_NO_TEXT_CHILDREN: ELEMENTS_WITH_NO_TEXT_CHILDREN,
129131
invertObject: invertObject,
130132
isCustomComponent: isCustomComponent,
131133
setStyleProp: setStyleProp,
132134
canTextBeChildOfNode: canTextBeChildOfNode,
133-
elementsWithNoTextChildren: elementsWithNoTextChildren,
134135
returnFirstArg: returnFirstArg
135136
};

test/utilities.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const React = require('react');
22
const {
33
PRESERVE_CUSTOM_ATTRIBUTES,
4+
ELEMENTS_WITH_NO_TEXT_CHILDREN,
45
invertObject,
56
isCustomComponent,
67
setStyleProp,
7-
elementsWithNoTextChildren,
88
canTextBeChildOfNode
99
} = require('../lib/utilities');
1010

@@ -141,7 +141,7 @@ describe('setStyleProp', () => {
141141
});
142142

143143
describe('canTextBeChildOfNode', () => {
144-
it.each(Array.from(elementsWithNoTextChildren))(
144+
it.each(Array.from(ELEMENTS_WITH_NO_TEXT_CHILDREN))(
145145
'returns false since text node cannot be child of %s',
146146
nodeName => {
147147
const node = {

0 commit comments

Comments
 (0)