Skip to content

Commit

Permalink
speed up hyphenating style names (#3251)
Browse files Browse the repository at this point in the history
by:
1. avoiding work if not needed
2. lower casing individual upper case characters instead of entire strings
  • Loading branch information
willheslam committed Sep 2, 2020
1 parent 792e41d commit dd6b302
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/styled-components/src/utils/hyphenateStyleName.js
Expand Up @@ -5,8 +5,10 @@
* https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/hyphenateStyleName.js
*/

const uppercasePattern = /([A-Z])/g;
const uppercaseCheck = /([A-Z])/;
const uppercasePattern = new RegExp(uppercaseCheck, 'g');
const msPattern = /^ms-/;
const prefixAndLowerCase = (char: string): string => `-${char.toLowerCase()}`;

/**
* Hyphenates a camelcased CSS property name, for example:
Expand All @@ -25,8 +27,9 @@ const msPattern = /^ms-/;
* @return {string}
*/
export default function hyphenateStyleName(string: string): string {
return string
.replace(uppercasePattern, '-$1')
.toLowerCase()
.replace(msPattern, '-ms-');
return uppercaseCheck.test(string)
? string
.replace(uppercasePattern, prefixAndLowerCase)
.replace(msPattern, '-ms-')
: string;
}

0 comments on commit dd6b302

Please sign in to comment.