Skip to content

Instantly share code, notes, and snippets.

@marlalain
Last active October 23, 2022 20:44
Show Gist options
  • Save marlalain/2cb187092ac19794b2dfcefa65d76dc9 to your computer and use it in GitHub Desktop.
Save marlalain/2cb187092ac19794b2dfcefa65d76dc9 to your computer and use it in GitHub Desktop.
enterprise sum
// @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);
@mmattbtw
Copy link

mmattbtw commented Sep 7, 2022

LGTM πŸ‘πŸ‘

@theianm8
Copy link

theianm8 commented Sep 7, 2022

😭😭😭

@Samathingamajig
Copy link

this.log.log(/* */);

πŸ₯΅πŸ₯΅πŸ₯΅

@AnibalDBXD
Copy link

So clean πŸ‘πŸ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment