From 36fc99781a1deff89da386b904e7925756559db4 Mon Sep 17 00:00:00 2001 From: Leroy Korterink Date: Fri, 3 May 2024 15:41:09 +0200 Subject: [PATCH] Add commonly used regex patterns --- src/index.ts | 1 + src/utils/regexPatterns/regexPatterns.mdx | 47 +++++++++++++++++++ src/utils/regexPatterns/regexPatterns.test.ts | 29 ++++++++++++ src/utils/regexPatterns/regexPatterns.ts | 29 ++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 src/utils/regexPatterns/regexPatterns.mdx create mode 100644 src/utils/regexPatterns/regexPatterns.test.ts create mode 100644 src/utils/regexPatterns/regexPatterns.ts diff --git a/src/index.ts b/src/index.ts index 815250e..bc0c7f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,4 +38,5 @@ export * from './utils/adjustFontSize/adjustFontSize.js'; export * from './utils/arrayRef/arrayRef.js'; export * from './utils/createTimeout/createTimeout.js'; export * from './utils/isRefObject/isRefObject.js'; +export * from './utils/regexPatterns/regexPatterns.js'; export * from './utils/unref/unref.js'; diff --git a/src/utils/regexPatterns/regexPatterns.mdx b/src/utils/regexPatterns/regexPatterns.mdx new file mode 100644 index 0000000..9d9aae3 --- /dev/null +++ b/src/utils/regexPatterns/regexPatterns.mdx @@ -0,0 +1,47 @@ +import { Meta } from '@storybook/blocks'; + + + +# Regex Patterns + +Regular expressions (regex) are sequences of characters that form a search pattern. They can be used +to check if a string contains the specified search pattern. This package contains commonly used +patterns that can be used to check strings. + +## Usage + +### PascalCase + +PascalCase is a naming convention in which the first letter of each word is capitalized. It is often +used in programming languages for class names and object constructors. + +```tsx +pascalCaseRegex.test('MyComponent'); // Returns true +pascalCaseRegex.test('MyComponent5'); // Returns true +pascalCaseRegex.test('MyC0mp0nent'); // Returns true +pascalCaseRegex.test('My00mponent'); // Returns true +``` + +### camelCase + +camelCase is another naming convention in which the first letter of each word is capitalized except +for the first word. It is often used in programming languages for variable and function names. + +```ts +camelCaseRegex.test('camelCaseString'); // Returns true +camelCaseRegex.test('helloW0rld'); // Returns true +camelCaseRegex.test('g00M0rning'); // Returns true +camelCaseRegex.test('happyC0ding'); // Returns true +``` + +### kebab-case + +kebab-case is a naming convention in which words are separated by hyphens (-). It is often used in +URL slugs and CSS class names. + +```ts +kebabCaseRegex.test('my-component'); // Returns true +kebabCaseRegex.test('my-component-5'); // Returns true +kebabCaseRegex.test('my-c0mp0nent'); // Returns true +kebabCaseRegex.test('c0ding1sfun'); // Returns true +``` diff --git a/src/utils/regexPatterns/regexPatterns.test.ts b/src/utils/regexPatterns/regexPatterns.test.ts new file mode 100644 index 0000000..f2ffc3f --- /dev/null +++ b/src/utils/regexPatterns/regexPatterns.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from 'vitest'; +import { camelCaseRegex, kebabCaseRegex, pascalCaseRegex } from './regexPatterns.js'; + +describe('pascalCaseRegex', () => { + it('should match string in the pascal case naming convention', () => { + expect(pascalCaseRegex.test('MyComponent')).toBeTruthy(); + expect(pascalCaseRegex.test('MyComponent5')).toBeTruthy(); + expect(pascalCaseRegex.test('MyC0mp0nent')).toBeTruthy(); + expect(pascalCaseRegex.test('My00mponent')).toBeTruthy(); + }); +}); + +describe('camelCaseRegex', () => { + it('should match string in the camel case naming convention', () => { + expect(camelCaseRegex.test('camelCaseString')).toBeTruthy(); + expect(camelCaseRegex.test('helloW0rld')).toBeTruthy(); + expect(camelCaseRegex.test('g00M0rning')).toBeTruthy(); + expect(camelCaseRegex.test('happyC0ding')).toBeTruthy(); + }); +}); + +describe('kebabCaseRegex', () => { + it('should match string in the kebab case naming convention', () => { + expect(kebabCaseRegex.test('my-component')).toBeTruthy(); + expect(kebabCaseRegex.test('my-component-5')).toBeTruthy(); + expect(kebabCaseRegex.test('my-c0mp0nent')).toBeTruthy(); + expect(kebabCaseRegex.test('c0ding1sfun')).toBeTruthy(); + }); +}); diff --git a/src/utils/regexPatterns/regexPatterns.ts b/src/utils/regexPatterns/regexPatterns.ts new file mode 100644 index 0000000..05a145c --- /dev/null +++ b/src/utils/regexPatterns/regexPatterns.ts @@ -0,0 +1,29 @@ +/** + * PascalCaseString + * MyComponent + * MyComponent5 + * MyC0mp0nent + * My00mponent + */ +// eslint-disable-next-line unicorn/no-unsafe-regex +export const pascalCaseRegex = /^(?:[A-Z][\da-z]+)+$/u; + +/** + * @example + * camelCaseString + * helloW0rld + * g00M0rning + * happyC0ding + */ +// eslint-disable-next-line unicorn/no-unsafe-regex +export const camelCaseRegex = /^[\da-z]+(?:[A-Z][\da-z]+)*$/u; + +/** + * @example + * my-component + * my-component-5 + * my-c0mp0nent + * c0ding1sfun + */ +// eslint-disable-next-line unicorn/no-unsafe-regex +export const kebabCaseRegex = /^[\da-z]+(?:-[\da-z]+)*$/u;