diff --git a/src/br/cpf.spec.ts b/src/br/cpf.spec.ts index a99f800f..e6b3c758 100644 --- a/src/br/cpf.spec.ts +++ b/src/br/cpf.spec.ts @@ -8,6 +8,12 @@ describe('br/cpf', () => { expect(result).toEqual('231.002.999-00'); }); + it('format: 216.957.050-09', () => { + const result = format('216.957.050-09'); + + expect(result).toEqual('216.957.050-09'); + }); + it('validate:390.533.447-05', () => { const result = validate('390.533.447-05'); diff --git a/src/br/cpf.ts b/src/br/cpf.ts index 51495f0d..48898e04 100644 --- a/src/br/cpf.ts +++ b/src/br/cpf.ts @@ -47,7 +47,11 @@ const impl: Validator = { format(input: string): string { const [value] = clean(input); - const [a, b, c, d] = strings.splitAt(value, 3, 6, input.length - 2); + if (value.length !== 11) { + throw new exceptions.InvalidLength('CPF must have 11 digits.'); + } + + const [a, b, c, d] = strings.splitAt(value, 3, 6, 9, 11); return `${a}.${b}.${c}-${d}`; },