-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypescript-example-1.ts
47 lines (41 loc) · 1.41 KB
/
typescript-example-1.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @author Gadzhiev Islam
*
* How can we refactor it and make simpler to read and understand, keeping immutability and all the logic inside ?
* 1. Rules must be provided with comments somehow
* 2. When we add something it always is about new object
* 3. If rules are not marked with ids - its error
* 4. Somehow rules must be marked with ids
*/
interface IBusinessRules {
rules: IRuleDescription[];
}
interface IRuleDescription {
rule: string;
comment: string;
}
class BusinessRules implements IBusinessRules {
public rules: IRuleDescription[] = [{ rule: '', comment: '' }];
constructor (rules: IRuleDescription[]) {
this.rules = rules;
}
addRule ({ rule , comment }: IRuleDescription) {
const updatedRules = new BusinessRules([...this.rules, { rule, comment }]);
updatedRules.rules.map(({ rule }) => this.ruleIdIsPresent(rule))
return updatedRules;
}
ruleIdIsPresent (rule: string) {
const ruleId = rule.slice(0, 2);
if (
!ruleId.includes('#') &&
!!Number(ruleId.at(0))
) {
throw new Error(`Rule "${rule}" doesn't have an id in the beginning`);
}
return true;
}
}
const rules = new BusinessRules([{ rule: '#1 X buys Y', comment: '' }])
.addRule({ rule: '#2 taxes are 13%', comment: '' })
.addRule({ rule: '#3 and more…', comment: '' });
console.log(rules);