Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commonly used regex patterns #338

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
47 changes: 47 additions & 0 deletions src/utils/regexPatterns/regexPatterns.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Meta } from '@storybook/blocks';

<Meta title="Hooks / useMediaQuery" />

# 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
```
29 changes: 29 additions & 0 deletions src/utils/regexPatterns/regexPatterns.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
29 changes: 29 additions & 0 deletions src/utils/regexPatterns/regexPatterns.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading