Created
November 7, 2018 13:47
-
-
Save handersonbf/ef8b296cc4f7d4fd14c64587d6323cf7 to your computer and use it in GitHub Desktop.
CertidaoField.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Field } from "./Field"; | |
export class CertidaoField { | |
private readonly field: Field; | |
constructor(config: { required?: boolean; disabled?: boolean; valueCommitted?: (() => void) } = {}) { | |
this.field = new Field({ typicalLength: 32, ...config, validateNotEmpty: this.validateNotEmpty }); | |
} | |
get text() { | |
return this.field.text; | |
} | |
set text(value) { | |
this.field.text = value; | |
} | |
get setText() { | |
return this.field.setText; | |
} | |
get readOnly() { | |
return this.field.readOnly; | |
} | |
set readOnly(value) { | |
this.field.readOnly = value; | |
} | |
get disabled() { | |
return this.field.disabled; | |
} | |
set disabled(value) { | |
this.field.disabled = value; | |
} | |
get required() { | |
return this.field.required; | |
} | |
set required(value) { | |
this.field.required = value; | |
} | |
get tabFocusDisabled() { | |
return this.field.tabFocusDisabled; | |
} | |
get errorMessage() { | |
return this.field.errorMessage; | |
} | |
set errorMessage(value) { | |
this.field.errorMessage = value; | |
} | |
get isValid() { | |
return !this.errorMessage; | |
} | |
get isFocusRequested() { | |
return this.field.isFocusRequested; | |
} | |
set isFocusRequested(value) { | |
this.field.isFocusRequested = value; | |
} | |
get typicalLength() { | |
return this.field.typicalLength; | |
} | |
get placeholder() { | |
return this.field.placeholder; | |
} | |
get textAlign() { | |
return this.field.textAlign; | |
} | |
get value() { | |
// 1167370155201045047370382804745 | |
// 116737.01.55.2010.4.50473.703.828047-45 | |
// 116737 01 55 2010 4 50473 703 828047 45 | |
const match = this.text.match(/^(\d{31})$/); | |
let decimoQuintoDigitoValido = false; | |
let ehUmaCertidaoValida = false; | |
if (match) { | |
const numeroDaCertidaoDeObito = match[0]; | |
decimoQuintoDigitoValido = "%w[4, 7]".includes(numeroDaCertidaoDeObito.toString().substr(14, 1)); | |
ehUmaCertidaoValida = new CheckDVCertidaoObito(numeroDaCertidaoDeObito).certidaoValida; | |
ehUmaCertidaoValida = true; | |
} | |
return match && decimoQuintoDigitoValido && ehUmaCertidaoValida ? match[0] : undefined; | |
} | |
set value(value) { | |
this.text = value ? value : ""; | |
} | |
get jsonValue() { | |
return this.value || null; | |
} | |
set jsonValue(value) { | |
this.value = value || undefined; | |
} | |
get updated() { | |
return this.field.updated; | |
} | |
set updated(value) { | |
this.field.updated = value; | |
} | |
get focus() { | |
return this.field.focus; | |
} | |
get commitValueChanged() { | |
return this.field.commitValueChange; | |
} | |
get validate() { | |
return this.field.validate; | |
} | |
private readonly validateNotEmpty = () => { | |
if (!this.jsonValue) { | |
this.errorMessage = "Valor inválido"; | |
} | |
} | |
} | |
class CheckDVCertidaoObito { | |
constructor(private readonly str: string) { } | |
get certidaoValida() { | |
// tslint:disable-next-line:no-console | |
console.log(this.str); | |
const dv1 = this.str.substr(30, 1); | |
const dv2 = Number(); | |
// this.str.split("").reverse().forEach(digito => { | |
// const dv2 = Number(digito); | |
// // tslint:disable-next-line:no-console | |
// console.log(digito); | |
// }); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment