Skip to content

Commit 2f3367d

Browse files
committed
refactor: optimize script execution time
1 parent f61cd23 commit 2f3367d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/getClassName.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,53 @@ import type {
66
} from './types';
77

88
const isNamespacedStyleName = (styleName: string): boolean => {
9-
return styleName.includes('.');
9+
return styleName.indexOf('.') !== -1;
1010
};
1111

1212
const getClassNameForNamespacedStyleName = (styleName: string, styleModuleImportMap: StyleModuleImportMapType): string => {
13-
const [
14-
importName,
15-
moduleName
16-
] = styleName.split('.');
13+
// Note:
14+
// Do not use the desctructing syntax with Babel.
15+
// Desctructing adds _slicedToArray helper.
16+
const styleNameParts = styleName.split('.');
17+
const importName = styleNameParts[0];
18+
const moduleName = styleNameParts[1];
1719

1820
if (!moduleName) {
1921
throw new Error('Invalid style name.');
2022
}
2123

22-
if (!styleModuleImportMap.hasOwnProperty(importName)) {
24+
if (!styleModuleImportMap[importName]) {
2325
throw new Error('Import does not exist.');
2426
}
2527

26-
if (!styleModuleImportMap[importName].hasOwnProperty(moduleName)) {
28+
if (!styleModuleImportMap[importName][moduleName]) {
2729
throw new Error('Module does not exist.');
2830
}
2931

3032
return styleModuleImportMap[importName][moduleName];
3133
};
3234

3335
export default (styleNameValue: string, styleModuleImportMap: StyleModuleImportMapType): string => {
36+
const styleModuleImportMapKeys = Object.keys(styleModuleImportMap);
37+
3438
return styleNameValue
3539
.split(' ')
3640
.map((styleName) => {
3741
if (isNamespacedStyleName(styleName)) {
3842
return getClassNameForNamespacedStyleName(styleName, styleModuleImportMap);
3943
}
4044

41-
if (Object.keys(styleModuleImportMap).length === 0) {
45+
if (styleModuleImportMapKeys.length === 0) {
4246
throw new Error('Cannot use styleName attribute without importing at least one stylesheet.');
4347
}
4448

45-
if (Object.keys(styleModuleImportMap).length > 1) {
49+
if (styleModuleImportMapKeys.length > 1) {
4650
throw new Error('Cannot use anonymous style name with more than one stylesheet import.');
4751
}
4852

49-
const styleModuleMap: StyleModuleMapType = styleModuleImportMap[Object.keys(styleModuleImportMap)[0]];
53+
const styleModuleMap: StyleModuleMapType = styleModuleImportMap[styleModuleImportMapKeys[0]];
5054

51-
if (!styleModuleMap.hasOwnProperty(styleName)) {
55+
if (!styleModuleMap[styleName]) {
5256
throw new Error('Module cannot be resolved.');
5357
}
5458

0 commit comments

Comments
 (0)