Last active
October 23, 2022 20:44
-
-
Save voidcoefficient/2cb187092ac19794b2dfcefa65d76dc9 to your computer and use it in GitHub Desktop.
enterprise sum
This file contains hidden or 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
// @ts-ignore | |
class NumberValidator { | |
number: number | undefined; | |
log = new Logger('NumberValidator'); | |
constructor(n: any) { | |
this.log.log(`NumberValidator constructor called with ${n}`); | |
if (typeof n === 'string') { | |
this.log.error(`NumberValidator failed with string ${n}`); | |
throw Error('Parameter needs to be a number'); | |
} else if (typeof n === 'bigint') { | |
this.log.error(`NumberValidator failed with bigint ${n}`); | |
throw Error('Parameter needs to be a number'); | |
} else if (typeof n === 'object') { | |
this.log.error(`NumberValidator failed with object ${n}`); | |
throw Error('Parameter needs to be a number'); | |
} else if (typeof n === 'boolean') { | |
this.log.error(`NumberValidator failed with boolean ${n}`); | |
throw Error('Parameter needs to be a number'); | |
} else if (typeof n === 'undefined') { | |
this.log.error(`NumberValidator failed with undefined ${n}`); | |
throw Error('Parameter needs to be a number'); | |
} else if (typeof n === 'function') { | |
this.log.error(`NumberValidator failed with function ${n}`); | |
throw Error('Parameter needs to be a number'); | |
} else if (typeof n === 'symbol') { | |
this.log.error(`NumberValidator failed with symbol ${String(n)}`); | |
throw Error('Parameter needs to be a number'); | |
} else if (typeof n === 'number') { | |
this.log.log(`NumberValidator successfully constructor called with ${n}`); | |
this.number = n; | |
} | |
} | |
} | |
class SumProcessorValidator { | |
numbers: number[] = []; | |
log = new Logger('SumProcessorValidator'); | |
constructor(numbers: number[]) { | |
this.log.log(`Validating numbers from input: ${numbers.toString()}`); | |
for (let i = 0; i < numbers.length; i++) { | |
if (numbers[i] === undefined || numbers[i] === null) { | |
break; | |
} | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
this.numbers.push(numbers[i]!); | |
} | |
} | |
validate(result: number): boolean { | |
this.log.log(`Validating result: ${result}`); | |
return this.numbers.reduce((a, b) => a + b) === result; | |
} | |
} | |
class SumProcessor { | |
firstNumber: number | undefined; | |
secondNumber: number | undefined; | |
log = new Logger('SumProcessor'); | |
constructor(x: unknown, y: unknown) { | |
this.log.log('SumProcessor constructor called'); | |
this.log.log('x: ' + x); | |
this.firstNumber = new NumberValidator(x).number; | |
this.log.log('y: ' + y); | |
this.secondNumber = new NumberValidator(y).number; | |
} | |
sum() { | |
this.log.log('Computing sum'); | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
return this.firstNumber! + this.secondNumber!; | |
} | |
} | |
class Logger { | |
className: string | undefined; | |
constructor(className: string) { | |
this.className = className; | |
} | |
time() { | |
return new Date().toDateString(); | |
} | |
log(message: string) { | |
console.log(`${this.className} | ${this.time()} | ${message}`); | |
} | |
error(message: string) { | |
console.error(`${this.className} | ${this.time()} | ${message}`); | |
} | |
} | |
const add = (x: any, y: any): number => { | |
const log = new Logger('::add'); | |
const number1 = new NumberValidator(x); | |
const number2 = new NumberValidator(y); | |
const sumProcessor = new SumProcessor(number1.number, number2.number); | |
const sumProcessorValidator = new SumProcessorValidator([ | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
number1.number!, | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
number2.number!, | |
]); | |
const result = sumProcessor.sum(); | |
if (sumProcessorValidator.validate(result)) { | |
log.log('Sum is valid'); | |
return result; | |
} | |
log.error('Sum is invalid'); | |
throw Error('Result is not valid'); | |
}; | |
const sum = add(1, 2); | |
console.log(sum); |
πππ
this.log.log(/* */);
π₯΅π₯΅π₯΅
So clean ππ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LGTM ππ