Skip to content

Commit

Permalink
Replace matchAll with exec to broaden browser support (#104)
Browse files Browse the repository at this point in the history
Co-authored-by: Rogin Farrer <rfarrer@wayfair.com>
  • Loading branch information
roginfarrer and Rogin Farrer committed Feb 9, 2023
1 parent 51822b9 commit fabac72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-hornets-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rainbow-sprinkles': patch
---

Replace String.matchAll with RegExp.exec to broaden browser and Node version support
20 changes: 11 additions & 9 deletions packages/rainbow-sprinkles/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { CreateStylesOutput } from './types';
/**
* Parses a string for things with '$'
*
* (?<negated>-)? -> optionally captures '-', names it "negated"
* (-)? -> optionally captures '-', names it "negated"
* \B\$ -> capture '$' when preceded by a "non-word" (whitespace, punctuation)
* (?<token>\w+) -> capture the "word" following the '$'
* (\w+) -> capture the "word" following the '$'
* /g -> capture all instances
*/
export const VALUE_REGEX = /(?<negated>-)?\B\$(?<token>\w+)/g;
export const VALUE_REGEX = /(-)?\B\$(\w+)/g;

export function mapValues<
Value,
Expand Down Expand Up @@ -36,8 +36,7 @@ export function replaceVarsInValue(
scale: CreateStylesOutput['dynamicScale'],
) {
const parsed = propValue.replace(VALUE_REGEX, (match, ...args) => {
const { negated, token }: { negated?: '-'; token?: string } =
args[args.length - 1];
const [negated, token] = args;
const v = `${negated ? '-' : ''}${token}`;
if (scale?.[v]) {
return scale[v];
Expand All @@ -55,10 +54,13 @@ export function getValueConfig(
propValue: string,
scale: CreateStylesOutput['values'],
): CreateStylesOutput['values'][keyof CreateStylesOutput['values']] | null {
const parsed = [...propValue.matchAll(VALUE_REGEX)];
if (parsed.length === 1) {
const { negated, token }: { negated?: '-'; token?: string } =
parsed[0].groups;
let match: RegExpExecArray | null;
const parsed: string[] = [];
while ((match = VALUE_REGEX.exec(propValue))) {
parsed.push(...match.slice(1));
}
if (parsed.length === 2) {
const [negated, token] = parsed;
const v = `${negated ? '-' : ''}${token}`;
if (v in scale) {
return scale[v];
Expand Down

0 comments on commit fabac72

Please sign in to comment.