Skip to content

Commit

Permalink
chore: add prettier script
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jul 12, 2024
1 parent 44f0e70 commit 254dc00
Show file tree
Hide file tree
Showing 46 changed files with 1,765 additions and 1,309 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ webpack.config.js
jestTestFile.js
BackupCopia
.eslintrc.js
jest.config.js
24 changes: 13 additions & 11 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

title: ""
labels: ""
assignees: ""
---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
Expand All @@ -24,15 +24,17 @@ A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
7 changes: 3 additions & 4 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

title: ""
labels: ""
assignees: ""
---

**Is your feature request related to a problem? Please describe.**
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/typescript-pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: Typescript Pull Request Check

on:
push:
branches: [ main ]
branches: [main]

pull_request:
branches: [ main ]
branches: [main]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand All @@ -15,13 +15,13 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 20
registry-url: https://registry.npmjs.org/

- name: Test
run: |
- name: Test
run: |
yarn install
yarn pr-check
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"code-runner.executorMap": {
"typescript": "npx ts-node",
},
"typescript": "npx ts-node"
}
}
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ Feel free to find bugs and report them to me. Your feedback is highly appreciate
## CDNs

jsDelivr:

```bash
https://cdn.jsdelivr.net/npm/multiform-validator@2.1.3/dist/bundle.min.js
```

```html
<script src="https://cdn.jsdelivr.net/npm/multiform-validator@2.1.3/dist/bundle.min.js"></script>
```

unpkg:

```bash
https://unpkg.com/multiform-validator@2.1.3/dist/bundle.js
```

```html
<script src="https://unpkg.com/multiform-validator@2.1.3/dist/bundle.js"></script>
```
Expand All @@ -36,13 +40,11 @@ https://unpkg.com/multiform-validator@2.1.3/dist/bundle.js
```html
<script src="https://cdn.jsdelivr.net/npm/multiform-validator@2.1.3/dist/bundle.min.js"></script>
<script>
const emailResult = isEmail('123456');
const cpfResult = cpfIsValid('123456');
const emailResult = isEmail("123456");
const cpfResult = cpfIsValid("123456");
console.log(emailResult); // returns false
console.log(cpfResult.isValid); // returns false
</script>
```

Expand Down Expand Up @@ -100,6 +102,7 @@ This package contains various modules for validating different types of data. Be
- **validateUSPhoneNumber**: US phone number validation.

### You can use it in React Native, Angular, any javascript framework or any javascript or typescript code.

#### Example using Reactjs:

![Example using Reactjs](https://raw.githubusercontent.com/Multiform-Validator/docs/main/images/exampleWithReactjs.png)
Expand All @@ -109,19 +112,17 @@ This package contains various modules for validating different types of data. Be
### For better information, read the documentation

```javascript

const validator = require('multiform-validator');
const validator = require("multiform-validator");
// or
import validator from 'multiform-validator';
import validator from "multiform-validator";

or
or;

// Attention, FUNCTION_NAME is not a valid function name! It is just an example of how to import the functions.

const { FUNCTION_NAME } = require('multiform-validator');
const { FUNCTION_NAME } = require("multiform-validator");
// or
import { FUNCTION_NAME } from 'multiform-validator';

import { FUNCTION_NAME } from "multiform-validator";
```

```javascript
Expand All @@ -144,32 +145,36 @@ validator.FUNCTION_NAME.errorMsg = 'ErrorMsg' // You can customize errors
```

## Documentation

### https://multiformvalidator.netlify.app

```javascript

const { validateEmail } = require('multiform-validator');
const { validateEmail } = require("multiform-validator");
// or
import { validateEmail } from 'multiform-validator';
import { validateEmail } from "multiform-validator";

// It has many more validations, you can read the documentation or
// do several tests to learn how to use the library in the best possible way.

// Two last parameters are optional
console.log(validateEmail('email@email.com', { maxLength: 30, country: 'br' }).isValid); // returns false
console.log(validateEmail('email@email.com.br', { maxLength: 30, country: 'br' }).isValid); // returns true
console.log(validateEmail('email@email.com', { maxLength: 30 }).isValid); // returns true
console.log(
validateEmail("email@email.com", { maxLength: 30, country: "br" }).isValid,
); // returns false
console.log(
validateEmail("email@email.com.br", { maxLength: 30, country: "br" }).isValid,
); // returns true
console.log(validateEmail("email@email.com", { maxLength: 30 }).isValid); // returns true

// All examples return default messages, below is an example setting your own messages

// If you want to use a default parameter, use null.

const validationResult = validateEmail('email@email.com', { maxLength: 30 });
const validationResult = validateEmail("email@email.com", { maxLength: 30 });

if (validationResult.isValid) {
console.log('0 errors');
console.log("0 errors");
} else {
console.log(validationResult.errorMsg); // returns the error
console.log(validationResult.errorMsg); // returns the error
}

// or
Expand All @@ -180,11 +185,10 @@ const validationResult = validateEmail("1email@email.com", {
});

if (validationResult.isValid) {
console.log('0 errors');
console.log("0 errors");
} else {
console.log(validationResult.errorMsg); // Return 'This is an invalid email with my own errors'
console.log(validationResult.errorMsg); // Return 'This is an invalid email with my own errors'
}

```

Feel free to explore the various functions and experiment with different inputs to understand their behavior. If you encounter any issues or have suggestions, don't hesitate to reach out to me. Your feedback is valuable and helps improve the package. Happy coding!
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
preset: "ts-jest",
testEnvironment: "node",
};
Empty file removed jestTestFile.js
Empty file.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"build:browser": "webpack",
"build": "yarn build:types && yarn build:src && yarn build:browser",
"lint": "eslint . --ext .ts",
"pr-check": "yarn lint && yarn build && yarn test"
"prettier": "prettier --write .",
"pr-check": "yarn prettier && yarn lint && yarn build && yarn test"
},
"repository": {
"type": "git",
Expand Down
127 changes: 67 additions & 60 deletions tests/src/cnpjValidator.test.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,70 @@
import cnpjIsValid from '../../src/cnpjValidator';

describe('cnpjIsValid function', () => {
test('should validate a valid CNPJ', () => {
const result = cnpjIsValid('72.501.263/0001-40');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test('should invalidate an invalid CNPJ', () => {
const result = cnpjIsValid('12.345.678/0001-91');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CNPJ is not valid');
});

test('should invalidate a CNPJ with incorrect length', () => {
const result = cnpjIsValid('1234567890123');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CNPJ must have 14 numerical digits');
});

test('should invalidate a CNPJ with non-digit characters', () => {
const result = cnpjIsValid('72.501.263/0001-4A');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CNPJ is not valid');
});

test('should invalidate an empty CNPJ', () => {
const result = cnpjIsValid('');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CNPJ invalid');
});

test('should throw an error if input is not a string', () => {
expect(() => {
cnpjIsValid(12345678901234 as any);
}).toThrow('The input should be a string.');
});

test('should throw an error if errorMsg is not an array', () => {
expect(() => {
cnpjIsValid('72.501.263/0001-40', 'error message' as any);
}).toThrow('Must be an Array');
});

test('should throw an error if errorMsg contains non-string values', () => {
expect(() => {
cnpjIsValid('72.501.263/0001-40', [123, 'error message'] as any);
}).toThrow('All values within the array must be strings or null/undefined.');
});

test('should return custom error messages', () => {
const result = cnpjIsValid('12.345.678/0001-91', ['Custom invalid message', 'Custom length message', 'Custom not valid message', 'Custom unknown error message']);
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('Custom not valid message');
});

test('should return false when all digits are repeated', () => {
const result = cnpjIsValid('11.111.111/1111-11');
import cnpjIsValid from "../../src/cnpjValidator";

describe("cnpjIsValid function", () => {
test("should validate a valid CNPJ", () => {
const result = cnpjIsValid("72.501.263/0001-40");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should invalidate an invalid CNPJ", () => {
const result = cnpjIsValid("12.345.678/0001-91");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should invalidate a CNPJ with incorrect length", () => {
const result = cnpjIsValid("1234567890123");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ must have 14 numerical digits");
});

test("should invalidate a CNPJ with non-digit characters", () => {
const result = cnpjIsValid("72.501.263/0001-4A");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should invalidate an empty CNPJ", () => {
const result = cnpjIsValid("");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ invalid");
});

test("should throw an error if input is not a string", () => {
expect(() => {
cnpjIsValid(12345678901234 as any);
}).toThrow("The input should be a string.");
});

test("should throw an error if errorMsg is not an array", () => {
expect(() => {
cnpjIsValid("72.501.263/0001-40", "error message" as any);
}).toThrow("Must be an Array");
});

test("should throw an error if errorMsg contains non-string values", () => {
expect(() => {
cnpjIsValid("72.501.263/0001-40", [123, "error message"] as any);
}).toThrow(
"All values within the array must be strings or null/undefined.",
);
});

test("should return custom error messages", () => {
const result = cnpjIsValid("12.345.678/0001-91", [
"Custom invalid message",
"Custom length message",
"Custom not valid message",
"Custom unknown error message",
]);
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("Custom not valid message");
});

test("should return false when all digits are repeated", () => {
const result = cnpjIsValid("11.111.111/1111-11");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CNPJ is not valid');
expect(result.errorMsg).toBe("CNPJ is not valid");
});
});
Loading

0 comments on commit 254dc00

Please sign in to comment.