Skip to content

Commit

Permalink
Merge pull request #7 from wesleyara/develop
Browse files Browse the repository at this point in the history
feat: adding mask methods
  • Loading branch information
wesleyara authored Dec 7, 2023
2 parents f070f6b + ab02bea commit 01e2c92
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
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 @@ -195,3 +195,38 @@ export const getNavigatorCurrentLocation = async ({

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");
};

0 comments on commit 01e2c92

Please sign in to comment.