peru-doc-validators
Version:
Validaciones para los documentos de Identidad que se usan en el Perú
1 lines • 3.13 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../src/models/dni.model.ts"],"sourcesContent":["import { ResultValid } from './validators.model';\n\nexport class Dni {\n\n status: ResultValid;\n errors: ResultValid[] = [];\n\n constructor(private number: string, private digit: string) {\n this.status = {\n code: -1,\n message: 'no validado'\n }\n this.validate();\n }\n\n validate() {\n this.validateDigits();\n }\n\n get isValid() {\n return this.status.code === 0;\n }\n \n\n validateDigits() {\n const multiples = [3, 2, 7, 6, 5, 4, 3, 2];\n const dcontrols = {\n numbers: [6, 7, 8, 9, 0, 1, 1, 2, 3, 4, 5],\n letters: [\"K\", \"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\"],\n };\n const numdni = this.number.substring(0, this.number.length - 1).split(\"\").map( digit => +digit);\n const dcontrol = this.digit;\n const dsum = numdni.reduce((acc, digit, index) => {\n acc += digit * multiples[index];\n return acc;\n }, 0);\n const key = 11 - (dsum % 11);\n const index = key === 11 ? 0 : key;\n if (/^\\d+$/.test(this.number)) {\n const valid = dcontrols.numbers[index] === parseInt(dcontrol, 10);\n this.addStatus(valid);\n return;\n }\n const valid = dcontrols.letters[index] === dcontrol;\n this.addStatus(valid);\n }\n\n\n private addStatus(valid: boolean) {\n if (valid) {\n this.setValidOk();\n } else {\n this.setValidError('Formato invalido')\n }\n }\n\n\n private setValidOk() {\n this.status = {\n code: 0,\n message: 'Documento Valido'\n }\n }\n\n private setValidError(message: string) {\n this.status = {\n code: 1,\n message\n }\n }\n\n\n}"],"names":["Dni","constructor","number","digit","this","status","code","message","validate","validateDigits","isValid","multiples","dcontrols","numdni","substring","length","split","map","dcontrol","key","reduce","acc","index","test","valid","parseInt","addStatus","setValidOk","setValidError"],"mappings":"MAEaA,EAKTC,YAAoBC,EAAwBC,GAAxBC,YAAAF,EAAwBE,WAAAD,EAF5CC,YAAwB,GAGpBA,KAAKC,OAAS,CACVC,MAAO,EACPC,QAAS,eAEbH,KAAKI,WAGTA,WACIJ,KAAKK,iBAGTC,cACI,OAA4B,SAAhBL,OAAOC,KAIvBG,iBACI,MAAME,EAAY,CAAC,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,GAClCC,EACG,CAAC,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,GAGlCC,EAAST,KAAKF,OAAOY,UAAU,EAAGV,KAAKF,OAAOa,OAAS,GAAGC,MAAM,IAAIC,IAAKd,IAAUA,GACnFe,EAAWd,KAAKD,MAKhBgB,EAAM,GAJCN,EAAOO,OAAO,CAACC,EAAKlB,EAAOmB,IACpCD,EAAOlB,EAAQQ,EAAUW,GAEtB,GACkB,GACnBA,EAAgB,KAARH,EAAa,EAAIA,EAC/B,GAAI,QAAQI,KAAKnB,KAAKF,QAAtB,CACI,MAAMsB,EAAQZ,EAAkBU,KAAWG,SAASP,EAAU,IAC9Dd,KAAKsB,UAAUF,QAInBpB,KAAKsB,UAhBI,CAAC,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,KAe5BJ,KAAWJ,GAKvCQ,UAAUF,GACVA,EACApB,KAAKuB,aAELvB,KAAKwB,cAAc,oBAKnBD,aACJvB,KAAKC,OAAS,CACVC,KAAM,EACNC,QAAS,oBAITqB,cAAcrB,GAClBH,KAAKC,OAAS,CACVC,KAAM,EACNC,QAAAA"}