@devix-tecnologia/value-objects 2.1.4

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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -0
  3. package/dist/cjs/cpf_cnpj/index.js +152 -0
  4. package/dist/cjs/imagem_base64/image-formats.js +90 -0
  5. package/dist/cjs/imagem_base64/index.js +132 -0
  6. package/dist/cjs/index.js +9 -0
  7. package/dist/cjs/telefone/country_codes.js +1146 -0
  8. package/dist/cjs/telefone/ddd.js +73 -0
  9. package/dist/cjs/telefone/index.js +127 -0
  10. package/dist/cjs/types/base_class.js +2 -0
  11. package/dist/cjs/types/cpf_cnpj/index.d.ts +25 -0
  12. package/dist/cjs/types/cpf_cnpj/index.d.ts.map +1 -0
  13. package/dist/cjs/types/imagem_base64/image-formats.d.ts +14 -0
  14. package/dist/cjs/types/imagem_base64/image-formats.d.ts.map +1 -0
  15. package/dist/cjs/types/imagem_base64/index.d.ts +32 -0
  16. package/dist/cjs/types/imagem_base64/index.d.ts.map +1 -0
  17. package/dist/cjs/types/index.d.ts +5 -0
  18. package/dist/cjs/types/index.d.ts.map +1 -0
  19. package/dist/cjs/types/telefone/country_codes.d.ts +7 -0
  20. package/dist/cjs/types/telefone/country_codes.d.ts.map +1 -0
  21. package/dist/cjs/types/telefone/ddd.d.ts +3 -0
  22. package/dist/cjs/types/telefone/ddd.d.ts.map +1 -0
  23. package/dist/cjs/types/telefone/index.d.ts +38 -0
  24. package/dist/cjs/types/telefone/index.d.ts.map +1 -0
  25. package/dist/cjs/types/types/base_class.d.ts +26 -0
  26. package/dist/cjs/types/types/base_class.d.ts.map +1 -0
  27. package/dist/esm/cpf_cnpj/index.js +151 -0
  28. package/dist/esm/imagem_base64/image-formats.js +86 -0
  29. package/dist/esm/imagem_base64/index.js +129 -0
  30. package/dist/esm/index.mjs +4 -0
  31. package/dist/esm/package.json +1 -0
  32. package/dist/esm/telefone/country_codes.js +1143 -0
  33. package/dist/esm/telefone/ddd.js +70 -0
  34. package/dist/esm/telefone/index.js +124 -0
  35. package/dist/esm/types/base_class.js +1 -0
  36. package/dist/esm/types/cpf_cnpj/index.d.ts +25 -0
  37. package/dist/esm/types/cpf_cnpj/index.d.ts.map +1 -0
  38. package/dist/esm/types/imagem_base64/image-formats.d.ts +14 -0
  39. package/dist/esm/types/imagem_base64/image-formats.d.ts.map +1 -0
  40. package/dist/esm/types/imagem_base64/index.d.ts +32 -0
  41. package/dist/esm/types/imagem_base64/index.d.ts.map +1 -0
  42. package/dist/esm/types/index.d.ts +5 -0
  43. package/dist/esm/types/index.d.ts.map +1 -0
  44. package/dist/esm/types/telefone/country_codes.d.ts +7 -0
  45. package/dist/esm/types/telefone/country_codes.d.ts.map +1 -0
  46. package/dist/esm/types/telefone/ddd.d.ts +3 -0
  47. package/dist/esm/types/telefone/ddd.d.ts.map +1 -0
  48. package/dist/esm/types/telefone/index.d.ts +38 -0
  49. package/dist/esm/types/telefone/index.d.ts.map +1 -0
  50. package/dist/esm/types/types/base_class.d.ts +26 -0
  51. package/dist/esm/types/types/base_class.d.ts.map +1 -0
  52. package/package.json +71 -0
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DDD = void 0;
4
+ const DDD = {
5
+ '11': 'SP',
6
+ '12': 'SP',
7
+ '13': 'SP',
8
+ '14': 'SP',
9
+ '15': 'SP',
10
+ '16': 'SP',
11
+ '17': 'SP',
12
+ '18': 'SP',
13
+ '19': 'SP',
14
+ '21': 'RJ',
15
+ '22': 'RJ',
16
+ '24': 'RJ',
17
+ '27': 'ES',
18
+ '28': 'ES',
19
+ '31': 'MG',
20
+ '32': 'MG',
21
+ '33': 'MG',
22
+ '34': 'MG',
23
+ '35': 'MG',
24
+ '37': 'MG',
25
+ '38': 'MG',
26
+ '41': 'PR',
27
+ '42': 'PR',
28
+ '43': 'PR',
29
+ '44': 'PR',
30
+ '45': 'PR',
31
+ '46': 'PR',
32
+ '47': 'SC',
33
+ '48': 'SC',
34
+ '49': 'SC',
35
+ '51': 'RS',
36
+ '53': 'RS',
37
+ '54': 'RS',
38
+ '55': 'RS',
39
+ '61': 'DF',
40
+ '62': 'GO',
41
+ '63': 'TO',
42
+ '64': 'GO',
43
+ '65': 'MT',
44
+ '66': 'MT',
45
+ '67': 'MS',
46
+ '68': 'AC',
47
+ '69': 'RO',
48
+ '71': 'BA',
49
+ '73': 'BA',
50
+ '74': 'BA',
51
+ '75': 'BA',
52
+ '77': 'BA',
53
+ '79': 'SE',
54
+ '81': 'PE',
55
+ '82': 'AL',
56
+ '83': 'PB',
57
+ '84': 'RN',
58
+ '85': 'CE',
59
+ '86': 'PI',
60
+ '87': 'PE',
61
+ '88': 'CE',
62
+ '89': 'PI',
63
+ '91': 'PA',
64
+ '92': 'AM',
65
+ '93': 'PA',
66
+ '94': 'PA',
67
+ '95': 'RR',
68
+ '96': 'AP',
69
+ '97': 'AM',
70
+ '98': 'MA',
71
+ '99': 'MA',
72
+ };
73
+ exports.DDD = DDD;
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Telefone = void 0;
4
+ const country_codes_js_1 = require("./country_codes.js");
5
+ const ddd_js_1 = require("./ddd.js");
6
+ /**
7
+ * Cria objeto de valor para número de telefone.
8
+ * Valida DDD e código do país.
9
+ * É possível cadastrar usando formato string para números Brasileiros.
10
+ * Aceita formatos como: 27 97645-5555 ou 55 27 97645-5555.
11
+ * Desconsidera espaços e qq caracter não numérico
12
+ */
13
+ class Telefone {
14
+ constructor(phoneNum) {
15
+ this._phoneNum = '';
16
+ this._areaCode = '';
17
+ this._countryCode = '';
18
+ this._isValid = false;
19
+ if (typeof phoneNum === 'string') {
20
+ this.parseFromString(phoneNum);
21
+ }
22
+ else {
23
+ this.parseFromObject(phoneNum);
24
+ }
25
+ }
26
+ parseFromString(phoneStr) {
27
+ let tel = phoneStr.replace(/[^\d]+/g, '');
28
+ if ((tel.length === 12 || tel.length === 11) && tel[0] === '0') {
29
+ tel = tel.slice(1);
30
+ }
31
+ if (tel.length === 10 || tel.length === 11) {
32
+ this._countryCode = '55';
33
+ this._areaCode = tel.slice(0, 2);
34
+ this._phoneNum = tel.slice(2);
35
+ this._isValid = this.isDDDValid();
36
+ }
37
+ else if (tel.length === 12 || tel.length === 13) {
38
+ this._countryCode = tel.slice(0, 2);
39
+ this._areaCode = tel.slice(2, 4);
40
+ this._phoneNum = tel.slice(4);
41
+ this._isValid = this.isDDDValid() && this._countryCode === '55';
42
+ }
43
+ else {
44
+ throw new Error('Could not parse phone number from string. Try creating the record using an object { countryCode: string; areaCode: string; phoneNum: string }');
45
+ }
46
+ }
47
+ parseFromObject(phone) {
48
+ this._countryCode = phone.countryCode.replace(/[^\d]+/g, '');
49
+ this._areaCode = phone.areaCode.replace(/[^\d]+/g, '');
50
+ this._phoneNum = phone.phoneNum.replace(/[^\d]+/g, '');
51
+ if (this._countryCode === '55') {
52
+ this._isValid = this.isDDDValid() && this.isCountryCodeValid();
53
+ }
54
+ else {
55
+ this._isValid = this.isCountryCodeValid();
56
+ }
57
+ }
58
+ get type() {
59
+ return 'PHONE';
60
+ }
61
+ get version() {
62
+ return Telefone.VERSION;
63
+ }
64
+ get validation() {
65
+ if (this._countryCode === '55') {
66
+ return 'Validação de DDD brasileiro e código de país';
67
+ }
68
+ return 'Validação de código de país';
69
+ }
70
+ get formatted() {
71
+ this.isValid({ raiseException: true });
72
+ const num = this._phoneNum.split('');
73
+ const lastDigits = num.splice(-4, 4);
74
+ return `+${this._countryCode} ${this._areaCode} ${num.join('')}-${lastDigits.join('')}`;
75
+ }
76
+ get onlyNumbers() {
77
+ this.isValid({ raiseException: true });
78
+ return `${this._countryCode}${this._areaCode}${this._phoneNum}`;
79
+ }
80
+ get country() {
81
+ return country_codes_js_1.countryCodes[this._countryCode].code;
82
+ }
83
+ get estate() {
84
+ if (this._countryCode === '55')
85
+ return ddd_js_1.DDD[this._areaCode];
86
+ return null;
87
+ }
88
+ toJSON() {
89
+ return {
90
+ type: this.type,
91
+ value: this.onlyNumbers,
92
+ formatted: this.formatted,
93
+ isValid: this._isValid,
94
+ version: this.version,
95
+ country: this.country,
96
+ estate: this.estate,
97
+ };
98
+ }
99
+ toString() {
100
+ return this.formatted;
101
+ }
102
+ isValid(config = { raiseException: false }) {
103
+ if (config.raiseException && !this._isValid) {
104
+ throw new Error('Invalid phone number');
105
+ }
106
+ return this._isValid;
107
+ }
108
+ equals(other) {
109
+ if (!this._isValid)
110
+ return false;
111
+ try {
112
+ const otherPhone = other instanceof Telefone ? other : new Telefone(other);
113
+ return this.onlyNumbers === otherPhone.onlyNumbers;
114
+ }
115
+ catch (_a) {
116
+ return false;
117
+ }
118
+ }
119
+ isDDDValid() {
120
+ return ddd_js_1.DDD[this._areaCode] !== undefined;
121
+ }
122
+ isCountryCodeValid() {
123
+ return country_codes_js_1.countryCodes[this._countryCode] !== undefined;
124
+ }
125
+ }
126
+ exports.Telefone = Telefone;
127
+ Telefone.VERSION = '1.0.0';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,25 @@
1
+ import { IBaseClassNumbers, ValidConfig } from '../types/base_class.js';
2
+ declare class CpfCnpj implements IBaseClassNumbers {
3
+ private _docId;
4
+ private _docType;
5
+ private _isValid;
6
+ private static readonly VERSION;
7
+ constructor(numDoc: string | number);
8
+ get type(): string;
9
+ get version(): string;
10
+ get validation(): string;
11
+ get formatted(): string;
12
+ get onlyNumbers(): string;
13
+ toJSON(): object;
14
+ toString(): string;
15
+ isValid(config?: ValidConfig): boolean;
16
+ equals(other: CpfCnpj | unknown): boolean;
17
+ static newCpf(): CpfCnpj;
18
+ static newCnpj(): CpfCnpj;
19
+ private isCpfValid;
20
+ private isCnpjValid;
21
+ private static CpfVerifierDigits;
22
+ private static CnpjVerifierDigits;
23
+ }
24
+ export { CpfCnpj };
25
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/cpf_cnpj/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AAIvE,cAAM,OAAQ,YAAW,iBAAiB;IACxC,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAU;gBAE7B,MAAM,EAAE,MAAM,GAAG,MAAM;IAcnC,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,UAAU,IAAI,MAAM,CAIvB;IAED,IAAI,SAAS,IAAI,MAAM,CAatB;IAED,IAAI,WAAW,IAAI,MAAM,CAGxB;IAED,MAAM,IAAI,MAAM;IAUhB,QAAQ,IAAI,MAAM;IAIlB,OAAO,CAAC,MAAM,GAAE,WAAuC,GAAG,OAAO;IAOjE,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO;IAYzC,MAAM,CAAC,MAAM,IAAI,OAAO;IAUxB,MAAM,CAAC,OAAO,IAAI,OAAO;IAUzB,OAAO,CAAC,UAAU;IAuBlB,OAAO,CAAC,WAAW;IAiBnB,OAAO,CAAC,MAAM,CAAC,iBAAiB;IAUhC,OAAO,CAAC,MAAM,CAAC,kBAAkB;CAiBlC;AAED,OAAO,EAAE,OAAO,EAAE,CAAA"}
@@ -0,0 +1,14 @@
1
+ interface ImageFormatInfo {
2
+ name: string;
3
+ extension: string;
4
+ mimeType: string;
5
+ base64Prefix: string[];
6
+ }
7
+ declare const imageFormats: Record<string, ImageFormatInfo>;
8
+ type ImageFormat = keyof typeof imageFormats | 'unknown';
9
+ /**
10
+ * Detecta o formato da imagem pelo seu prefixo base64
11
+ */
12
+ declare function findFormatByPrefix(base64Str: string): ImageFormat;
13
+ export { imageFormats, type ImageFormat, type ImageFormatInfo, findFormatByPrefix, };
14
+ //# sourceMappingURL=image-formats.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"image-formats.d.ts","sourceRoot":"","sources":["../../../../src/imagem_base64/image-formats.ts"],"names":[],"mappings":"AAAA,UAAU,eAAe;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,MAAM,CAAA;IAChB,YAAY,EAAE,MAAM,EAAE,CAAA;CACvB;AAED,QAAA,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAyExC,CAAA;AAEV,KAAK,WAAW,GAAG,MAAM,OAAO,YAAY,GAAG,SAAS,CAAA;AAExD;;GAEG;AACH,iBAAS,kBAAkB,CAAC,SAAS,EAAE,MAAM,GAAG,WAAW,CAO1D;AAED,OAAO,EACL,YAAY,EACZ,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,kBAAkB,GACnB,CAAA"}
@@ -0,0 +1,32 @@
1
+ import { IBaseClassText, ValidConfig } from '../types/base_class.js';
2
+ import { type ImageFormat, type ImageFormatInfo } from './image-formats.js';
3
+ /**
4
+ * Cria objeto de valor para imagens em base64.
5
+ * Valida se a string fornecida é uma imagem base64 válida.
6
+ * Detecta o formato da imagem.
7
+ * Aceita tanto strings base64 puras quanto data URIs.
8
+ */
9
+ declare class ImagemBase64 implements IBaseClassText {
10
+ private _content;
11
+ private _format;
12
+ private _isValid;
13
+ private _isDataUri;
14
+ private static readonly VERSION;
15
+ constructor(content: string);
16
+ get type(): string;
17
+ get version(): string;
18
+ get validation(): string;
19
+ get formatted(): string;
20
+ get format(): ImageFormat;
21
+ get content(): string;
22
+ get formatInfo(): ImageFormatInfo;
23
+ private get DataUri();
24
+ toJSON(): object;
25
+ toString(): string;
26
+ isValid(config?: ValidConfig): boolean;
27
+ equals(other: ImagemBase64 | unknown): boolean;
28
+ private validate;
29
+ private isBase64String;
30
+ }
31
+ export { ImagemBase64 };
32
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/imagem_base64/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AACpE,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,eAAe,EAErB,MAAM,oBAAoB,CAAA;AAE3B;;;;;GAKG;AACH,cAAM,YAAa,YAAW,cAAc;IAC1C,OAAO,CAAC,QAAQ,CAAa;IAC7B,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,UAAU,CAAiB;IACnC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAU;gBAE7B,OAAO,EAAE,MAAM;IAK3B,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED,IAAI,MAAM,IAAI,WAAW,CAGxB;IAED,IAAI,OAAO,IAAI,MAAM,CAGpB;IAED,IAAI,UAAU,IAAI,eAAe,CAGhC;IAED,OAAO,KAAK,OAAO,GAMlB;IAED,MAAM,IAAI,MAAM;IAoBhB,QAAQ,IAAI,MAAM;IAKlB,OAAO,CAAC,MAAM,GAAE,WAAuC,GAAG,OAAO;IAOjE,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,GAAG,OAAO;IAgB9C,OAAO,CAAC,QAAQ;IA8BhB,OAAO,CAAC,cAAc;CAUvB;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
@@ -0,0 +1,5 @@
1
+ import { Telefone } from './telefone/index.js';
2
+ import { CpfCnpj } from './cpf_cnpj/index.js';
3
+ import { ImagemBase64 } from './imagem_base64/index.js';
4
+ export { Telefone, CpfCnpj, ImagemBase64 };
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAEvD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,YAAY,EAAE,CAAA"}
@@ -0,0 +1,7 @@
1
+ declare const countryCodes: Record<string, {
2
+ name: string;
3
+ code: string;
4
+ dial_code: string;
5
+ }>;
6
+ export { countryCodes };
7
+ //# sourceMappingURL=country_codes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"country_codes.d.ts","sourceRoot":"","sources":["../../../../src/telefone/country_codes.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,YAAY,EAAE,MAAM,CACxB,MAAM,EACN;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAsnClD,CAAA;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
@@ -0,0 +1,3 @@
1
+ declare const DDD: Record<string, string>;
2
+ export { DDD };
3
+ //# sourceMappingURL=ddd.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ddd.d.ts","sourceRoot":"","sources":["../../../../src/telefone/ddd.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAoE/B,CAAA;AACD,OAAO,EAAE,GAAG,EAAE,CAAA"}
@@ -0,0 +1,38 @@
1
+ import { IBaseClassNumbers, ValidConfig } from '../types/base_class.js';
2
+ interface PhoneInput {
3
+ countryCode: string;
4
+ areaCode: string;
5
+ phoneNum: string;
6
+ }
7
+ /**
8
+ * Cria objeto de valor para número de telefone.
9
+ * Valida DDD e código do país.
10
+ * É possível cadastrar usando formato string para números Brasileiros.
11
+ * Aceita formatos como: 27 97645-5555 ou 55 27 97645-5555.
12
+ * Desconsidera espaços e qq caracter não numérico
13
+ */
14
+ declare class Telefone implements IBaseClassNumbers {
15
+ private _phoneNum;
16
+ private _areaCode;
17
+ private _countryCode;
18
+ private _isValid;
19
+ private static readonly VERSION;
20
+ constructor(phoneNum: string | PhoneInput);
21
+ private parseFromString;
22
+ private parseFromObject;
23
+ get type(): string;
24
+ get version(): string;
25
+ get validation(): string;
26
+ get formatted(): string;
27
+ get onlyNumbers(): string;
28
+ get country(): string;
29
+ get estate(): string | null;
30
+ toJSON(): object;
31
+ toString(): string;
32
+ isValid(config?: ValidConfig): boolean;
33
+ equals(other: Telefone | unknown): boolean;
34
+ private isDDDValid;
35
+ private isCountryCodeValid;
36
+ }
37
+ export { Telefone, type PhoneInput };
38
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/telefone/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AAEvE,UAAU,UAAU;IAClB,WAAW,EAAE,MAAM,CAAA;IACnB,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED;;;;;;GAMG;AACH,cAAM,QAAS,YAAW,iBAAiB;IACzC,OAAO,CAAC,SAAS,CAAa;IAC9B,OAAO,CAAC,SAAS,CAAa;IAC9B,OAAO,CAAC,YAAY,CAAa;IACjC,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAU;gBAE7B,QAAQ,EAAE,MAAM,GAAG,UAAU;IAQzC,OAAO,CAAC,eAAe;IAwBvB,OAAO,CAAC,eAAe;IAYvB,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,UAAU,IAAI,MAAM,CAKvB;IAED,IAAI,SAAS,IAAI,MAAM,CAKtB;IAED,IAAI,WAAW,IAAI,MAAM,CAGxB;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,MAAM,IAAI,MAAM,GAAG,IAAI,CAG1B;IAED,MAAM,IAAI,MAAM;IAYhB,QAAQ,IAAI,MAAM;IAIlB,OAAO,CAAC,MAAM,GAAE,WAAuC,GAAG,OAAO;IAOjE,MAAM,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO,GAAG,OAAO;IAY1C,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,kBAAkB;CAG3B;AAED,OAAO,EAAE,QAAQ,EAAE,KAAK,UAAU,EAAE,CAAA"}
@@ -0,0 +1,26 @@
1
+ interface ValidConfig {
2
+ raiseException: boolean;
3
+ }
4
+ interface IMetadata {
5
+ get type(): string;
6
+ get version(): string;
7
+ get validation(): string;
8
+ }
9
+ interface IBaseClass<T> extends IMetadata {
10
+ get formatted(): string;
11
+ isValid(config: ValidConfig): boolean;
12
+ equals(other: T | unknown): boolean;
13
+ toJSON(): object;
14
+ toString(): string;
15
+ }
16
+ interface IBaseClassNumbers extends IBaseClass<IBaseClassNumbers> {
17
+ get onlyNumbers(): string;
18
+ }
19
+ interface IBaseClassText extends IBaseClass<IBaseClassText> {
20
+ }
21
+ interface IBase64<T = unknown> extends IBaseClass<T> {
22
+ get onlyContent(): string;
23
+ get dataUri(): string;
24
+ }
25
+ export type { IBaseClassNumbers, IBaseClassText, IBase64, ValidConfig };
26
+ //# sourceMappingURL=base_class.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base_class.d.ts","sourceRoot":"","sources":["../../../../src/types/base_class.ts"],"names":[],"mappings":"AAAA,UAAU,WAAW;IACnB,cAAc,EAAE,OAAO,CAAA;CACxB;AAED,UAAU,SAAS;IACjB,IAAI,IAAI,IAAI,MAAM,CAAA;IAClB,IAAI,OAAO,IAAI,MAAM,CAAA;IACrB,IAAI,UAAU,IAAI,MAAM,CAAA;CACzB;AAED,UAAU,UAAU,CAAC,CAAC,CAAE,SAAQ,SAAS;IACvC,IAAI,SAAS,IAAI,MAAM,CAAA;IACvB,OAAO,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAA;IACrC,MAAM,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO,GAAG,OAAO,CAAA;IACnC,MAAM,IAAI,MAAM,CAAA;IAChB,QAAQ,IAAI,MAAM,CAAA;CACnB;AAED,UAAU,iBAAkB,SAAQ,UAAU,CAAC,iBAAiB,CAAC;IAC/D,IAAI,WAAW,IAAI,MAAM,CAAA;CAC1B;AAED,UAAU,cAAe,SAAQ,UAAU,CAAC,cAAc,CAAC;CAAG;AAE9D,UAAU,OAAO,CAAC,CAAC,GAAG,OAAO,CAAE,SAAQ,UAAU,CAAC,CAAC,CAAC;IAClD,IAAI,WAAW,IAAI,MAAM,CAAA;IACzB,IAAI,OAAO,IAAI,MAAM,CAAA;CACtB;AAED,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,EAAE,CAAA"}
@@ -0,0 +1,151 @@
1
+ class CpfCnpj {
2
+ _docId;
3
+ _docType;
4
+ _isValid = false;
5
+ static VERSION = '1.0.0';
6
+ constructor(numDoc) {
7
+ const doc = String(numDoc).replace(/[^\d]+/g, '');
8
+ this._docId = doc;
9
+ if (doc.length === 11 && this.isCpfValid()) {
10
+ this._docType = 'CPF';
11
+ this._isValid = true;
12
+ }
13
+ else if (doc.length === 14 && this.isCnpjValid()) {
14
+ this._docType = 'CNPJ';
15
+ this._isValid = true;
16
+ }
17
+ else {
18
+ this._docType = 'INVALID';
19
+ }
20
+ }
21
+ get type() {
22
+ return this._docType;
23
+ }
24
+ get version() {
25
+ return CpfCnpj.VERSION;
26
+ }
27
+ get validation() {
28
+ return this._docType === 'CPF'
29
+ ? 'Módulo 11 para CPF'
30
+ : 'Módulo 11 para CNPJ';
31
+ }
32
+ get formatted() {
33
+ this.isValid({ raiseException: true });
34
+ if (this._docType === 'CPF') {
35
+ return this._docId.replace(/^(\d{3})(\d{3})(\d{3})(\d{2})$/, '$1.$2.$3-$4');
36
+ }
37
+ return this._docId.replace(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/, '$1.$2.$3/$4-$5');
38
+ }
39
+ get onlyNumbers() {
40
+ this.isValid({ raiseException: true });
41
+ return this._docId;
42
+ }
43
+ toJSON() {
44
+ return {
45
+ type: this.type,
46
+ value: this.onlyNumbers,
47
+ formatted: this.formatted,
48
+ isValid: this._isValid,
49
+ version: this.version,
50
+ };
51
+ }
52
+ toString() {
53
+ return this.formatted;
54
+ }
55
+ isValid(config = { raiseException: false }) {
56
+ if (config.raiseException && !this._isValid) {
57
+ throw new Error(`Invalid ${this._docType.toLowerCase()}`);
58
+ }
59
+ return this._isValid;
60
+ }
61
+ equals(other) {
62
+ if (!this._isValid)
63
+ return false;
64
+ try {
65
+ const otherDoc = other instanceof CpfCnpj ? other : new CpfCnpj(other);
66
+ return this.onlyNumbers === otherDoc.onlyNumbers;
67
+ }
68
+ catch {
69
+ return false;
70
+ }
71
+ }
72
+ static newCpf() {
73
+ let doc_id = '';
74
+ for (let i = 0; i < 9; i += 1) {
75
+ doc_id += Math.floor(Math.random() * 9);
76
+ }
77
+ doc_id += this.CpfVerifierDigits(doc_id);
78
+ doc_id += this.CpfVerifierDigits(doc_id);
79
+ return new CpfCnpj(doc_id);
80
+ }
81
+ static newCnpj() {
82
+ let doc_id = '';
83
+ for (let i = 0; i < 12; i += 1) {
84
+ doc_id += Math.floor(Math.random() * 9);
85
+ }
86
+ doc_id += this.CnpjVerifierDigits(doc_id);
87
+ doc_id += this.CnpjVerifierDigits(doc_id);
88
+ return new CpfCnpj(doc_id);
89
+ }
90
+ isCpfValid() {
91
+ const blocklist = [
92
+ '00000000000',
93
+ '11111111111',
94
+ '22222222222',
95
+ '33333333333',
96
+ '44444444444',
97
+ '55555555555',
98
+ '66666666666',
99
+ '77777777777',
100
+ '88888888888',
101
+ '99999999999',
102
+ '12345678909',
103
+ ];
104
+ if (blocklist.includes(this._docId))
105
+ return false;
106
+ let numbers = this._docId.slice(0, 9);
107
+ numbers += CpfCnpj.CpfVerifierDigits(numbers);
108
+ numbers += CpfCnpj.CpfVerifierDigits(numbers);
109
+ return numbers.slice(-2) === this._docId.slice(-2);
110
+ }
111
+ isCnpjValid() {
112
+ const blocklist = [
113
+ '00000000000000',
114
+ '11111111111111',
115
+ '22222222222222',
116
+ '33333333333333',
117
+ '44444444444444',
118
+ '55555555555555',
119
+ '66666666666666',
120
+ '77777777777777',
121
+ '88888888888888',
122
+ '99999999999999',
123
+ ];
124
+ if (blocklist.includes(this._docId))
125
+ return false;
126
+ return true;
127
+ }
128
+ static CpfVerifierDigits(doc_ident) {
129
+ const numbers = doc_ident.split('').map((number) => parseInt(number, 10));
130
+ const modulus = numbers.length + 1;
131
+ const multiplied = numbers.map((number, index) => number * (modulus - index));
132
+ const mod = multiplied.reduce((buffer, number) => buffer + number) % 11;
133
+ return mod < 2 ? 0 : 11 - mod;
134
+ }
135
+ static CnpjVerifierDigits(doc_ident) {
136
+ let index = 2;
137
+ const reverse = doc_ident
138
+ .split('')
139
+ .reduce((buffer, number) => {
140
+ return [parseInt(number, 10)].concat(buffer);
141
+ }, []);
142
+ const sum = reverse.reduce((buffer, number) => {
143
+ buffer += number * index;
144
+ index = index === 9 ? 2 : index + 1;
145
+ return buffer;
146
+ }, 0);
147
+ const mod = sum % 11;
148
+ return mod < 2 ? 0 : 11 - mod;
149
+ }
150
+ }
151
+ export { CpfCnpj };
@@ -0,0 +1,86 @@
1
+ const imageFormats = {
2
+ JPEG: {
3
+ name: 'JPEG Image',
4
+ extension: 'jpg',
5
+ mimeType: 'image/jpeg',
6
+ base64Prefix: ['/9j/'],
7
+ },
8
+ PNG: {
9
+ name: 'Portable Network Graphics',
10
+ extension: 'png',
11
+ mimeType: 'image/png',
12
+ base64Prefix: ['iVBORw0KGgo'],
13
+ },
14
+ GIF: {
15
+ name: 'Graphics Interchange Format',
16
+ extension: 'gif',
17
+ mimeType: 'image/gif',
18
+ base64Prefix: ['R0lGODlh'],
19
+ },
20
+ WEBP: {
21
+ name: 'WebP Image',
22
+ extension: 'webp',
23
+ mimeType: 'image/webp',
24
+ base64Prefix: ['UklGRg=='],
25
+ },
26
+ BMP: {
27
+ name: 'Bitmap Image File',
28
+ extension: 'bmp',
29
+ mimeType: 'image/bmp',
30
+ base64Prefix: ['Qk'],
31
+ },
32
+ TIFF: {
33
+ name: 'Tagged Image File Format',
34
+ extension: 'tiff',
35
+ mimeType: 'image/tiff',
36
+ base64Prefix: ['SUkqAA==', 'TU0AKg=='],
37
+ },
38
+ ICO: {
39
+ name: 'Windows Icon',
40
+ extension: 'ico',
41
+ mimeType: 'image/x-icon',
42
+ base64Prefix: ['AAABAAEAICAQAAEABADoAgAAFg=='],
43
+ },
44
+ SVG: {
45
+ name: 'Scalable Vector Graphics',
46
+ extension: 'svg',
47
+ mimeType: 'image/svg+xml',
48
+ base64Prefix: ['PHN2Zw=='],
49
+ },
50
+ HEIC: {
51
+ name: 'High Efficiency Image Format',
52
+ extension: 'heic',
53
+ mimeType: 'image/heic',
54
+ base64Prefix: ['AAAAGGZ0eXA='],
55
+ },
56
+ AVIF: {
57
+ name: 'AV1 Image File Format',
58
+ extension: 'avif',
59
+ mimeType: 'image/avif',
60
+ base64Prefix: ['AAAAGGFhdmM='],
61
+ },
62
+ PSD: {
63
+ name: 'Adobe Photoshop Document',
64
+ extension: 'psd',
65
+ mimeType: 'image/vnd.adobe.photoshop',
66
+ base64Prefix: ['OEJQUw=='],
67
+ },
68
+ unknown: {
69
+ name: 'Unknown Format',
70
+ extension: '',
71
+ mimeType: 'application/octet-stream',
72
+ base64Prefix: [],
73
+ },
74
+ };
75
+ /**
76
+ * Detecta o formato da imagem pelo seu prefixo base64
77
+ */
78
+ function findFormatByPrefix(base64Str) {
79
+ for (const [format, info] of Object.entries(imageFormats)) {
80
+ if (info.base64Prefix.some((prefix) => base64Str.startsWith(prefix))) {
81
+ return format;
82
+ }
83
+ }
84
+ return 'unknown';
85
+ }
86
+ export { imageFormats, findFormatByPrefix, };