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

feat: adding mask methods #7

Merged
merged 1 commit into from
Dec 7, 2023
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
40 changes: 40 additions & 0 deletions docs/vanilla.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,43 @@ import { getNavigatorCurrentLocation } from "utils-react";

getNavigatorCurrentLocation().then((location) => console.log(location));
```

## phoneMask

Mask a phone number.

```js
import { phoneMask } from "utils-react";

phoneMask("1234567890"); // (12) 3456-7890
```

## cpfMask

Mask a CPF number.

```js
import { cpfMask } from "utils-react";

cpfMask("12345678900"); // 123.456.789-00
```

## cnpjMask

Mask a CNPJ number.

```js
import { cnpjMask } from "utils-react";

cnpjMask("12345678900000"); // 12.345.678/9000-00
```

## cepMask

Mask a CEP number.

```js
import { cepMask } from "utils-react";

cepMask("12345678"); // 12345-678
```
20 changes: 20 additions & 0 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import {
generateRandomString,
generateRandomColor,
getNavigatorCurrentLocation,
phoneMask,
cpfMask,
cnpjMask,
cepMask,
} from "../index";

describe("all methods in the package", () => {
Expand Down Expand Up @@ -169,4 +173,20 @@ describe("all methods in the package", () => {
timestamp: 123,
});
});

it("phone mask", () => {
expect(phoneMask("11999999999")).toBe("(11) 99999-9999");
});

it("cpf mask", () => {
expect(cpfMask("99999999999")).toBe("999.999.999-99");
});

it("cnpj mask", () => {
expect(cnpjMask("99999999999999")).toBe("99.999.999/9999-99");
});

it("cep mask", () => {
expect(cepMask("99999999")).toBe("99999-999");
});
});
35 changes: 35 additions & 0 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
return str.split("").reverse().join("");
};

export const arrayEquals = (a: any[], b: any[]) => {

Check warning on line 36 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 36 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 36 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 36 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
return false;
}
Expand All @@ -47,14 +47,14 @@
return true;
};

export const arrayToObject = (array: any[], key: string) => {

Check warning on line 50 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 50 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
return array.reduce((obj, item) => {
obj[item[key]] = item;
return obj;
}, {});
};

export const removeArrayItemByValue = (array: any[], value: any) => {

Check warning on line 57 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 57 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 57 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 57 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
const index = array.indexOf(value);
if (index > -1) {
array.splice(index, 1);
Expand All @@ -63,7 +63,7 @@
return array;
};

export const removeArrayItemByIndex = (array: any[], index: number) => {

Check warning on line 66 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 66 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
if (index > -1) {
array.splice(index, 1);
}
Expand All @@ -71,7 +71,7 @@
return array;
};

export const objectToArray = (obj: any) => {

Check warning on line 74 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 74 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
return Object.keys(obj).map(key => obj[key]);
};

Expand Down Expand Up @@ -100,7 +100,7 @@
};

export const smartLog = (
value: any,

Check warning on line 103 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 103 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
currentFunction: Function,
label?: string,
) => {
Expand All @@ -112,11 +112,11 @@
);
};

export function isEmptyObject(obj: any) {

Check warning on line 115 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 115 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
return JSON.stringify(obj) === "{}";
}

export const shuffleArray = (array: any) => {

Check warning on line 119 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 119 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
Expand Down Expand Up @@ -195,3 +195,38 @@

return { coords, timestamp };
};

export const phoneMask = (value: string) => {
return value
.replace(/\D/g, "")
.replace(/(\d{2})(\d)/, "($1) $2")
.replace(/(\d{5})(\d)/, "$1-$2")
.replace(/(-\d{4})\d+?$/, "$1");
};

export const cpfMask = (value: string) => {
return value
.replace(/\D/g, "")
.replace(/(\d{3})(\d)/, "$1.$2")
.replace(/(\d{3})(\d)/, "$1.$2")
.replace(/(\d{3})(\d)/, "$1-$2")
.replace(/(-\d{2})\d+?$/, "$1");
};

export const cnpjMask = (value: string) => {
return value
.replace(/\D/g, "")
.replace(/(\d{2})(\d)/, "$1.$2")
.replace(/(\d{3})(\d)/, "$1.$2")
.replace(/(\d{3})(\d)/, "$1/$2")
.replace(/(\d{4})(\d)/, "$1-$2")
.replace(/(-\d{2})\d+?$/, "$1");
};

export const cepMask = (value: string) => {
if (value.length > 9) {
return value.slice(0, 9);
}

return value.replace(/\D/g, "").replace(/(\d{5})(\d)/, "$1-$2");
};