Skip to content

Commit

Permalink
Merge pull request #835 from qawolf/improve-is-dynamic
Browse files Browse the repository at this point in the history
fix: isDynamic
  • Loading branch information
jperl committed Sep 9, 2020
2 parents b8ffd4e + 060d9a8 commit e882319
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/web/isDynamic.ts
Expand Up @@ -41,14 +41,17 @@ const SCORE_THRESHOLD = 0.5;
export const getTokens = (value: string): string[] => {
const tokens = [];

// split by space, dash, colon, and camel case
value.split(/[ \-_:]+|(?=[A-Z])/).forEach((token) => {
if (!token) return; // ignore empty string

tokens.push(token.toLowerCase());
// split by space, dash, underscore, colon
value.split(/[ \-_:]+/).forEach((token) => {
if (token.match(/\d/)) {
tokens.push(token);
} else {
// split by and camel case when there are no numbers
tokens.push(...token.split(/(?=[A-Z])/));
}
});

return tokens;
return tokens.map((token) => token.toLowerCase());
};

export const isDynamic = (
Expand Down
24 changes: 23 additions & 1 deletion test/web/isDynamic.test.ts
@@ -1,4 +1,25 @@
import { isDynamic } from '../../src/web/isDynamic';
import { getTokens, isDynamic } from '../../src/web/isDynamic';

describe('getTokens', () => {
it('splits space, dash, underscore, colon', () => {
expect(getTokens('hi there-how_are:you')).toEqual([
'hi',
'there',
'how',
'are',
'you',
]);
});

it('splits camelCase without numbers', () => {
expect(getTokens('hiThere')).toEqual(['hi', 'there']);
});

it('does not split camelCase with numbers', () => {
expect(getTokens('input-3Di')).toEqual(['input', '3di']);
expect(getTokens('input-Di3')).toEqual(['input', 'di3']);
});
});

describe('isDynamic', () => {
it.each([
Expand All @@ -8,6 +29,7 @@ describe('isDynamic', () => {
'ggWlfB2BMlWvNeAo2F0uqw',
'gLFyf',
'haSfzA',
'input-bu32879fDi',
'intercom-123v9c3',
'StyledBox-sc-13pk1d4-0',
'StyledLayer-rmtehz-0',
Expand Down

0 comments on commit e882319

Please sign in to comment.