@nexora-libs/validation-libs 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/pre-commit +1 -0
- package/.prettierignore +8 -0
- package/config/webpack.common.config.js +24 -0
- package/config/webpack.dev.config.js +24 -0
- package/config/webpack.prod.config.js +24 -0
- package/dist/eslint.config.d.ts +2 -0
- package/dist/index.bundle.js +1 -0
- package/dist/src/lib/index.d.ts +3 -0
- package/dist/src/lib/isCNPJ/index.d.ts +1 -0
- package/dist/src/lib/isCNPJ/index.test.d.ts +1 -0
- package/dist/src/lib/isCPF/index.d.ts +1 -0
- package/dist/src/lib/isCPF/index.test.d.ts +1 -0
- package/dist/src/main.d.ts +1 -0
- package/dist/src/model/product.model.d.ts +6 -0
- package/eslint.config.js +15 -0
- package/eslint.config.ts +9 -0
- package/package.json +52 -0
- package/prettier.config.js +7 -0
- package/src/lib/index.ts +4 -0
- package/src/lib/isCNPJ/index.test.ts +25 -0
- package/src/lib/isCNPJ/index.ts +27 -0
- package/src/lib/isCPF/index.test.ts +22 -0
- package/src/lib/isCPF/index.ts +25 -0
- package/src/main.js +5 -0
- package/src/main.ts +9 -0
- package/src/model/product.model.ts +11 -0
- package/test.ts +9 -0
- package/tsconfig.common.json +16 -0
- package/tsconfig.dev.json +10 -0
- package/tsconfig.prod.json +10 -0
- package/vite.config.js +13 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx lint-staged
|
package/.prettierignore
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
const dist = path.resolve('./dist');
|
|
3
|
+
|
|
4
|
+
const config = {
|
|
5
|
+
entry: {
|
|
6
|
+
index: './src/lib/index.ts',
|
|
7
|
+
},
|
|
8
|
+
output: {
|
|
9
|
+
path: dist,
|
|
10
|
+
filename: '[name].bundle.js',
|
|
11
|
+
clean: true,
|
|
12
|
+
library: {
|
|
13
|
+
type: 'module',
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
resolve: {
|
|
17
|
+
extensions: ['.ts', '.js'],
|
|
18
|
+
},
|
|
19
|
+
experiments: {
|
|
20
|
+
outputModule: true
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default config
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import common from './webpack.common.config.js';
|
|
2
|
+
import {merge} from 'webpack-merge';
|
|
3
|
+
|
|
4
|
+
const devConfig = {
|
|
5
|
+
mode: 'development',
|
|
6
|
+
module: {
|
|
7
|
+
rules: [
|
|
8
|
+
{
|
|
9
|
+
test: /\.ts$/,
|
|
10
|
+
exclude: /node_modules/,
|
|
11
|
+
use: [
|
|
12
|
+
{
|
|
13
|
+
loader: 'ts-loader',
|
|
14
|
+
options: {
|
|
15
|
+
configFile: 'tsconfig.dev.json',
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default merge(common, devConfig);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import common from './webpack.common.config.js';
|
|
2
|
+
import {merge} from 'webpack-merge';
|
|
3
|
+
|
|
4
|
+
const devConfig = {
|
|
5
|
+
mode: 'production',
|
|
6
|
+
module: {
|
|
7
|
+
rules: [
|
|
8
|
+
{
|
|
9
|
+
test: /\.ts$/,
|
|
10
|
+
exclude: /node_modules/,
|
|
11
|
+
use: [
|
|
12
|
+
{
|
|
13
|
+
loader: 'ts-loader',
|
|
14
|
+
options: {
|
|
15
|
+
configFile: 'tsconfig.prod.json',
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default merge(common, devConfig);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var t={};function r(t){if(!t)return!1;const r=t.replace(/\D/g,"");if(14!==r.length)return!1;if(/^(\d)\1{13}$/.test(r))return!1;const e=t=>{let r=0,e=t.length-7;for(let n=0;n<t.length;n++)r+=Number(t.charAt(n))*e--,e<2&&(e=9);const n=r%11;return n<2?0:11-n},n=r.substring(0,12),o=e(n);return r===`${n}${o}${e(n+o)}`}function e(t){if(!t)return!1;const r=t.replace(/\D/g,"");if(11!==r.length)return!1;if(/^(\d)\1{10}$/.test(r))return!1;const e=(t,r)=>{let e=0;for(let n=0;n<t.length;n++)e+=Number(t.charAt(n))*r--;const n=10*e%11;return 10===n?0:n},n=r.substring(0,9),o=e(n,10);return r===`${n}${o}${e(n+o,11)}`}t.d=(r,e)=>{for(var n in e)t.o(e,n)&&!t.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:e[n]})},t.o=(t,r)=>Object.prototype.hasOwnProperty.call(t,r);export{r as isCNPJ,e as isCPF};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isCNPJ(value: string | null | undefined): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isCPF(value: string | null | undefined): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import js from "@eslint/js";
|
|
2
|
+
import globals from "globals";
|
|
3
|
+
import {defineConfig} from "eslint/config";
|
|
4
|
+
|
|
5
|
+
export default defineConfig([
|
|
6
|
+
{
|
|
7
|
+
files: ["**/*.{js,mjs,cjs}"],
|
|
8
|
+
plugins: {js},
|
|
9
|
+
extends: ["js/recommended"],
|
|
10
|
+
languageOptions: {globals: {...globals.browser, ...globals.node}}
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
ignores: ['node_modules', 'coverage', 'dist', 'eslint.config.js'],
|
|
14
|
+
},
|
|
15
|
+
]);
|
package/eslint.config.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import js from "@eslint/js";
|
|
2
|
+
import globals from "globals";
|
|
3
|
+
import tseslint from "typescript-eslint";
|
|
4
|
+
import { defineConfig } from "eslint/config";
|
|
5
|
+
|
|
6
|
+
export default defineConfig([
|
|
7
|
+
{ files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], plugins: { js }, extends: ["js/recommended"], languageOptions: { globals: {...globals.browser, ...globals.node} } },
|
|
8
|
+
tseslint.configs.recommended,
|
|
9
|
+
]);
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nexora-libs/validation-libs",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "src/main.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build-dev": "webpack --config config/webpack.dev.config.js",
|
|
9
|
+
"build-prod": "webpack --config config/webpack.prod.config.js",
|
|
10
|
+
"lint": "eslint",
|
|
11
|
+
"lint:fix": "eslint --fix",
|
|
12
|
+
"prettier": "prettier . --check",
|
|
13
|
+
"prettier:fix": "prettier . --write",
|
|
14
|
+
"prepare": "husky",
|
|
15
|
+
"test": "vitest --run",
|
|
16
|
+
"test:coverage": "vitest run --coverage",
|
|
17
|
+
"pre-commit": "npm run prettier && npm run lint && npm run test",
|
|
18
|
+
"v:patch": "npm version patch",
|
|
19
|
+
"v:minor": "npm version minor",
|
|
20
|
+
"v:major": "npm version major",
|
|
21
|
+
"v:dep": "npm publish --access public",
|
|
22
|
+
"v:patch:full": "npm run test && npm run build-prod && npm run v:patch",
|
|
23
|
+
"v:minor:full": "npm run test && npm run build-prod && npm run v:minor",
|
|
24
|
+
"v:major:full": "npm run test && npm run build-prod && npm run v:major"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"lint-staged": {
|
|
30
|
+
"**/*.js": "npm run pre-commit"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@eslint/js": "^10.0.1",
|
|
34
|
+
"@eslint/json": "^1.0.1",
|
|
35
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
36
|
+
"eslint": "^10.0.2",
|
|
37
|
+
"globals": "^17.4.0",
|
|
38
|
+
"husky": "^9.1.7",
|
|
39
|
+
"jiti": "^2.6.1",
|
|
40
|
+
"lint-staged": "^16.3.2",
|
|
41
|
+
"prettier": "3.8.1",
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"typescript-eslint": "^8.56.1",
|
|
44
|
+
"vitest": "^4.0.18",
|
|
45
|
+
"webpack": "^5.105.4",
|
|
46
|
+
"webpack-cli": "^6.0.1",
|
|
47
|
+
"webpack-merge": "^6.0.1"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"ts-loader": "^9.5.4"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/lib/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { isCNPJ } from './index'
|
|
3
|
+
|
|
4
|
+
describe('isCNPJ', () => {
|
|
5
|
+
it.each([null, undefined, '', ' '])(
|
|
6
|
+
'CNPJ should be informed: %s',
|
|
7
|
+
(cnpj: string | null | undefined) => {
|
|
8
|
+
expect(isCNPJ(cnpj)).toBe(false)
|
|
9
|
+
}
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
it.each(['120.591.292-49', '11111111111111'])(
|
|
13
|
+
'CNPJ format is invalid: %s',
|
|
14
|
+
(cnpj) => {
|
|
15
|
+
expect(isCNPJ(cnpj)).toBe(false)
|
|
16
|
+
}
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
it.each(['26.478.930/0001-49', '26478930000149'])(
|
|
20
|
+
'CNPJ is valid: %s',
|
|
21
|
+
(cnpj) => {
|
|
22
|
+
expect(isCNPJ(cnpj)).toBe(true)
|
|
23
|
+
}
|
|
24
|
+
)
|
|
25
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function isCNPJ(value: string | null | undefined): boolean {
|
|
2
|
+
if (!value) return false;
|
|
3
|
+
|
|
4
|
+
const normalized = value.replace(/\D/g, '');
|
|
5
|
+
|
|
6
|
+
if (normalized.length !== 14) return false;
|
|
7
|
+
if (/^(\d)\1{13}$/.test(normalized)) return false;
|
|
8
|
+
|
|
9
|
+
const calculateDigit = (base: string): number => {
|
|
10
|
+
let sum = 0;
|
|
11
|
+
let weight = base.length - 7;
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < base.length; i++) {
|
|
14
|
+
sum += Number(base.charAt(i)) * weight--;
|
|
15
|
+
if (weight < 2) weight = 9;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const remainder = sum % 11;
|
|
19
|
+
return remainder < 2 ? 0 : 11 - remainder;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const base = normalized.substring(0, 12);
|
|
23
|
+
const digit1 = calculateDigit(base);
|
|
24
|
+
const digit2 = calculateDigit(base + digit1);
|
|
25
|
+
|
|
26
|
+
return normalized === `${base}${digit1}${digit2}`;
|
|
27
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { isCPF } from './index'
|
|
3
|
+
|
|
4
|
+
describe('isCPF', () => {
|
|
5
|
+
it.each([null, undefined, ''])(
|
|
6
|
+
'CPF can not be null, undefined or empty',
|
|
7
|
+
(cpf: string | null | undefined) => {
|
|
8
|
+
expect(isCPF(cpf)).toBe(false)
|
|
9
|
+
}
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
it.each(['26.478.930/0001-49', '11111111111'])(
|
|
13
|
+
'CPF format is invalid: %s',
|
|
14
|
+
(cpf) => {
|
|
15
|
+
expect(isCPF(cpf)).toBe(false)
|
|
16
|
+
}
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
it.each(['120.591.292-49', '12059129249'])('CPF is valid: %s', (cpf) => {
|
|
20
|
+
expect(isCPF(cpf)).toBe(true)
|
|
21
|
+
})
|
|
22
|
+
})
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function isCPF(value: string | null | undefined): boolean {
|
|
2
|
+
if (!value) return false;
|
|
3
|
+
|
|
4
|
+
const normalized = value.replace(/\D/g, '');
|
|
5
|
+
|
|
6
|
+
if (normalized.length !== 11) return false;
|
|
7
|
+
if (/^(\d)\1{10}$/.test(normalized)) return false;
|
|
8
|
+
|
|
9
|
+
const calculateDigit = (base: string, factor: number): number => {
|
|
10
|
+
let sum = 0;
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < base.length; i++) {
|
|
13
|
+
sum += Number(base.charAt(i)) * factor--;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const remainder = (sum * 10) % 11;
|
|
17
|
+
return remainder === 10 ? 0 : remainder;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const base = normalized.substring(0, 9);
|
|
21
|
+
const digit1 = calculateDigit(base, 10);
|
|
22
|
+
const digit2 = calculateDigit(base + digit1, 11);
|
|
23
|
+
|
|
24
|
+
return normalized === `${base}${digit1}${digit2}`;
|
|
25
|
+
}
|
package/src/main.js
ADDED
package/src/main.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { isCPF, isCNPJ } from './lib'
|
|
2
|
+
import { Product } from "./model/product.model";
|
|
3
|
+
|
|
4
|
+
const p1 = new Product("Meia", "Algodão", 39);
|
|
5
|
+
console.log(JSON.stringify(p1));
|
|
6
|
+
|
|
7
|
+
console.log('isCPF: \'120.591.292-49\'', isCPF('120.591.292-49'));
|
|
8
|
+
console.log('isCNPJ: \'120.591.292-49\'', isCNPJ('120.591.292-49'));
|
|
9
|
+
|
package/test.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { isCPF, isCNPJ } from './dist/index.bundle.js'
|
|
2
|
+
// import { Product } from "./model/product.model";
|
|
3
|
+
//
|
|
4
|
+
// const p1 = new Product("Meia", "Algodão", 39);
|
|
5
|
+
// console.log(JSON.stringify(p1));
|
|
6
|
+
|
|
7
|
+
console.log('isCPF: \'120.591.292-49\'', isCPF('120.591.292-49'));
|
|
8
|
+
console.log('isCNPJ: \'120.591.292-49\'', isCNPJ('120.591.292-49'));
|
|
9
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"include": ["**/*.ts"],
|
|
3
|
+
"exclude": ["node_modules", "test.ts"],
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"outDir": "./dist/",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"forceConsistentCasingInFileNames": true,
|
|
8
|
+
"strict": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"allowJs": true,
|
|
12
|
+
"noImplicitAny": true,
|
|
13
|
+
"explainFiles": true,
|
|
14
|
+
"declaration": true
|
|
15
|
+
}
|
|
16
|
+
}
|
package/vite.config.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {defineConfig} from "vitest/config";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
include: ['src/**/*.test.ts'],
|
|
6
|
+
coverage: {
|
|
7
|
+
provider: 'v8',
|
|
8
|
+
include: ['src/lib/**/*.ts'],
|
|
9
|
+
exclude: ['src/**/*.test.ts', 'src/lib/index.ts', 'src/main.ts'],
|
|
10
|
+
reporter: ['text', 'html'],
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
})
|