-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingredients.ts
More file actions
150 lines (108 loc) · 4.56 KB
/
ingredients.ts
File metadata and controls
150 lines (108 loc) · 4.56 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import RatioViewPlugin from "main";
const GRAMS_REGEX = /\d+(?= *grams| *g[^a-zA-Z]*)/;
export class Ingredients {
plugin: RatioViewPlugin;
ingredientText: string;
ingredientTextWithRatios: string;
constructor(plugin: RatioViewPlugin, ingredientText: string){
this.plugin = plugin;
this.ingredientText = ingredientText;
this.ingredientTextWithRatios = this.getTextWithRatiosOfIngredients(ingredientText);
}
getTextWithRatios(): string {
return this.ingredientTextWithRatios;
}
getTextWithRatiosOfIngredients(ingredientText: string): string {
let individualLines = this.getIndividualLines(ingredientText);
let baseAmount = this.getBaseAmountForIngredients(individualLines);
let textWithRatios = individualLines.map((line) => {
let ratioAmount = this.getRatioAmountOfLine(line, baseAmount);
let ratioText = this.formatTextWithRatio(ratioAmount);
return line.concat(ratioText);
}).join('\n');
return textWithRatios;
}
getIndividualLines(text: string): Array<string> {
let newlineRegex = /\r?\n/;
return text.split(newlineRegex);
}
getBaseAmountForIngredients(ingredients: Array<string>): number {
let baseAmount = 0;
ingredients.forEach((ingredient) => {
let gramAmount = this.getGramAmountOfLine(ingredient);
if (gramAmount && this.isBaseIngredient(ingredient)) {
baseAmount = gramAmount;
}
});
return baseAmount;
}
getGramAmountOfLine(line: string): number {
let cleanedLine = line.replace(/[^a-zA-Z0-9 ]/g, '');
let firstGramAmountInString = cleanedLine.match(GRAMS_REGEX);
let gramAmount = Number(firstGramAmountInString);
if (!gramAmount) {
let gramsOfSpecialIngredient = this.getWeightPerIngredient(line);
gramAmount = gramsOfSpecialIngredient;
}
return gramAmount || 0;
}
getWeightPerIngredient(line: string): number {
let knownIngredient: string = this.getKnownIngredientInLine(line);
if (knownIngredient) {
let amountOfKnownIngredient: number = this.getAmountOfKnownIngredient(line, knownIngredient);
let weightPerIngredient: number = this.calculateWeightOfIngredient(knownIngredient, amountOfKnownIngredient);
return weightPerIngredient;
}
return 0;
}
getKnownIngredientInLine(line: string): string {
let ingredientWeightsKnown = this.plugin.settings.ingredientsToGrams;
let ingredientsKnown = Object.keys(ingredientWeightsKnown);
let knownIngredient = ''
ingredientsKnown.forEach((key) => {
if (line.contains(key)) {
knownIngredient = key;
}
})
return knownIngredient;
}
getAmountOfKnownIngredient(line: string, knownIngredient: string): number {
let specialAmountRegex = new RegExp(/\d+/);
let specialAmountInString = line.match(specialAmountRegex);
let amountOfKnownIngredient = Number(specialAmountInString);
return amountOfKnownIngredient;
}
calculateWeightOfIngredient(knownIngredient: string, amountOfKnownIngredient: number): number {
let weightOfKnownIngredient = this.plugin.settings.ingredientsToGrams[knownIngredient];
if (weightOfKnownIngredient) {
let weight = amountOfKnownIngredient * weightOfKnownIngredient;
return weight;
}
return 0;
}
isBaseIngredient(ingredient: string): boolean {
var identifier = this.plugin.settings.baseAmountIdentifier;
let baseAmountRegex = new RegExp(identifier, "g");
let isBase = baseAmountRegex.test(ingredient);
return isBase;
}
getRatioAmountOfLine(line: string, baseAmountInGrams: number): number {
let gramAmountOfLine = this.getGramAmountOfLine(line);
if (gramAmountOfLine && baseAmountInGrams) {
let ratioAmount = (gramAmountOfLine / baseAmountInGrams) * 100;
let roundToXxPlace = this.plugin.settings.roundToPlace;
return this.roundToPlace(ratioAmount, roundToXxPlace);
}
return 0;
}
roundToPlace(amount: number, place: number): number {
return Number(amount.toFixed(place));
}
formatTextWithRatio(ratioAmount: number) {
let ratioText = '';
if (ratioAmount) {
ratioText = ratioText.concat(' ', ratioAmount.toString(), '%');
}
return ratioText;
}
}