@ronaldarndt/nationalid
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,1 +0,7 @@ | ||
export {}; | ||
import cpf from '@/countries/brazil/cpf'; | ||
import ssn from '@/countries/us/ssn'; | ||
import brazil from '@/countries/brazil'; | ||
import us from '@/countries/us'; | ||
import { Document, DocumentCode } from './types/general'; | ||
declare function getDocumentFromCode(code: DocumentCode): Document; | ||
export { brazil, cpf, getDocumentFromCode, ssn, us }; |
@@ -0,2 +1,125 @@ | ||
// src/countries/brazil/cpf.ts | ||
var mask = [ | ||
/\d/, | ||
/\d/, | ||
/\d/, | ||
".", | ||
/\d/, | ||
/\d/, | ||
/\d/, | ||
".", | ||
/\d/, | ||
/\d/, | ||
/\d/, | ||
"-", | ||
/\d/, | ||
/\d/ | ||
]; | ||
var cpf = { | ||
code: "CPF", | ||
digits: 11, | ||
mask, | ||
name: "Cadastro de Pessoas F\xEDsicas", | ||
format: (value) => { | ||
return value.replaceAll(/[^\d]/g, "").replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1.$2.$3-$4"); | ||
}, | ||
formatPartial: (value) => { | ||
const normalized = value.replaceAll(/[^\d]/g, ""); | ||
let result = [...normalized]; | ||
if (result.length >= 3) | ||
result = result.toSpliced(3, 0, "."); | ||
if (result.length >= 7) | ||
result = result.toSpliced(7, 0, "."); | ||
if (result.length >= 11) | ||
result = result.toSpliced(11, 0, "-"); | ||
return result.join(""); | ||
}, | ||
validate: (value) => { | ||
const normalized = value.replaceAll(/[^\d]/g, ""); | ||
if (normalized.length !== 11) | ||
return false; | ||
const digits = normalized.split("").map(Number); | ||
if (digits.every((digit) => digit === digits[0])) | ||
return false; | ||
const firstNineDigits = digits.slice(0, 9); | ||
const firstDigitAcc = firstNineDigits.reduce((acc, digit, index) => acc + digit * (10 - index), 0); | ||
const firstDigit = firstDigitAcc * 10 % 11; | ||
const firstDigitFinal = firstDigit === 10 ? 0 : firstDigit; | ||
const secondDigitAcc = [...firstNineDigits, firstDigitFinal].reduce((acc, digit, index) => acc + digit * (11 - index), 0); | ||
const secondDigit = secondDigitAcc * 10 % 11; | ||
const secondDigitFinal = secondDigit === 10 ? 0 : secondDigit; | ||
return firstDigitFinal === digits[9] && secondDigitFinal === digits[10]; | ||
} | ||
}; | ||
var cpf_default = cpf; | ||
// src/countries/us/ssn.ts | ||
var ssn = { | ||
code: "SSN", | ||
digits: 9, | ||
mask: [/\d/, /\d/, /\d/, "-", /\d/, /\d/, "-", /\d/, /\d/, /\d/], | ||
name: "Social Security Number", | ||
format: (value) => { | ||
return value.replaceAll(/[^\d]/g, "").replace(/(\d{3})(\d{2})(\d{4})/, "$1-$2-$3"); | ||
}, | ||
formatPartial: (value) => { | ||
const normalized = value.replaceAll(/[^\d]/g, ""); | ||
let result = [...normalized]; | ||
if (result.length >= 3) | ||
result = result.toSpliced(3, 0, "-"); | ||
if (result.length >= 6) | ||
result = result.toSpliced(6, 0, "-"); | ||
return result.join(""); | ||
}, | ||
validate: (value) => { | ||
const normalized = value.replaceAll(/[^\d]/g, ""); | ||
if (normalized.length !== 9) | ||
return false; | ||
const [firstPart, secondPart, thirdPart] = [ | ||
normalized.slice(0, 3), | ||
normalized.slice(3, 5), | ||
normalized.slice(5, 9) | ||
]; | ||
if (firstPart === "000" || firstPart === "666" || firstPart.startsWith("9")) | ||
return false; | ||
if (secondPart === "00") | ||
return false; | ||
if (thirdPart === "0000") | ||
return false; | ||
return true; | ||
} | ||
}; | ||
var ssn_default = ssn; | ||
// src/countries/brazil/index.ts | ||
var brazil = { | ||
code: "BR", | ||
name: "Brazil", | ||
documents: { | ||
CPF: cpf_default | ||
} | ||
}; | ||
var brazil_default = brazil; | ||
// src/countries/us/index.ts | ||
var us = { | ||
code: "US", | ||
name: "United States", | ||
documents: { | ||
SSN: ssn_default | ||
} | ||
}; | ||
var us_default = us; | ||
// src/index.ts | ||
console.log("Hello via Bun!"); | ||
var getDocumentFromCode = function(code) { | ||
return allDocuments[code]; | ||
}; | ||
var allDocuments = [brazil_default, us_default].map((x) => x.documents).reduce((acc, x) => ({ ...acc, ...x }), {}); | ||
export { | ||
us_default as us, | ||
ssn_default as ssn, | ||
getDocumentFromCode, | ||
cpf_default as cpf, | ||
brazil_default as brazil | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"type": "module", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Format and validate national identification numbers", | ||
@@ -16,2 +16,3 @@ "scripts": { | ||
"prettier": "^3.2.5", | ||
"prettier-plugin-organize-imports": "^3.2.4", | ||
"rimraf": "^5.0.5", | ||
@@ -27,2 +28,3 @@ "typescript": "^5.4.5" | ||
}, | ||
"license": "MIT", | ||
"keywords": [ | ||
@@ -29,0 +31,0 @@ "national", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
4924
0
128
0
5