Clean w/asserts

This commit is contained in:
Pilot 2020-12-05 23:21:30 -05:00 committed by pilot
parent 45eed9fc9d
commit 5dff9ca114
1 changed files with 26 additions and 30 deletions

View File

@ -73,7 +73,7 @@ function parseData(input: string): Passport[] {
}
function filterInvalid(input: Passport[]): Passport[] {
return input.filter(p => p.valid);
return input.filter(p => p.valid());
}
function filterIncorrect(input: Passport[]): Passport[] {
return input.filter(p => p.correct);
@ -89,46 +89,42 @@ class Passport {
pid?: string; // (Passport ID)
cid?: string; // (Country ID)
get valid() {
return (this.byr && this.iyr && this.eyr && this.hgt && this.hcl &&
valid(): this is Required<Passport> {
return !!(this.byr && this.iyr && this.eyr && this.hgt && this.hcl &&
this.ecl && this.pid);
}
get correct() {
if (!this.valid) return false;
try {
assert.ok(this.valid());
let valid: Required<Passport> = this;
let byr = parseInt(this.byr!, 10);
if (byr < 1920 || byr > 2002) return false;
assert.ok(isInRange(parseInt(valid.byr, 10), 1920, 2002));
assert.ok(isInRange(parseInt(valid.iyr, 10), 2010, 2020));
assert.ok(isInRange(parseInt(valid.eyr, 10), 2020, 2030));
assert.match(valid.hcl, /^#[0-9a-f]{6}$/);
assert.match(valid.pid, /^\d{9}$/);
assert.ok([
'amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'
].includes(valid.ecl));
let iyr = parseInt(this.iyr!, 10);
if (iyr < 2010 || iyr > 2020) return false;
let eyr = parseInt(this.eyr!, 10);
if (eyr < 2020 || eyr > 2030) return false;
let height = this.hgt!.match(/(\d+)(cm|in)/);
if (height) {
if (height[2] === 'cm') {
let n = parseInt(height[1], 10);
if (n < 150 || n > 193) return false;
} else if (height[2] === 'in') {
let n = parseInt(height[1], 10);
if (n < 59 || n > 76) return false;
let height = valid.hgt.match(/(\d+)(cm|in)/);
if (height) {
if (height[2] === 'cm') {
assert.ok(isInRange(parseInt(height[1], 10), 150, 193));
} else if (height[2] === 'in') {
assert.ok(isInRange(parseInt(height[1], 10), 59, 76));
} else {
return false;
}
} else {
return false;
}
} else {
} catch (_ignore) {
return false;
}
let hair = this.hcl!.match(/^#[0-9a-f]{6}$/)
if (!hair) return false;
if (!['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'].includes(this.ecl!)) return false;
let pid = this.pid!.match(/^\d{9}$/);
if (!pid) return false;
return true;
}
}
const isInRange = (i: number, min: number, max: number): boolean => (i - min) * (i - max) <= 0;