Skip to content

Commit

Permalink
Added the option for setting a default target currency. (oliverschwen…
Browse files Browse the repository at this point in the history
…dener#872)

You can now simply write "6 usd" and have it convert into a default currency of your choosing (the default is EUR).
  • Loading branch information
peterphmikkelsen authored Mar 18, 2022
1 parent 34c491b commit 3520748
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 41 deletions.
4 changes: 4 additions & 0 deletions src/common/config/currency-converter-options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { CurrencyCode } from "../../main/plugins/currency-converter-plugin/currency-code";

export interface CurrencyConverterOptions {
precision: number;
defaultTarget: CurrencyCode;
isEnabled: boolean;
}

export const defaultCurrencyConverterOptions: CurrencyConverterOptions = {
isEnabled: false,
precision: 2,
defaultTarget: CurrencyCode.EUR
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,39 @@ export class CurrencyConverterPlugin implements ExecutionPlugin {
public isValidUserInput(userInput: string, fallback?: boolean | undefined): boolean {
const keywords = ["in", "to"];
const words = userInput.trim().split(" ");
if (words.length === 3) {
try {
return (
!isNaN(this.getNumber(words[0])) &&
this.isCurrencyCode(words[1]) &&
this.isCurrencyCode(words[2])
);
} catch (err) {
return false;
switch (words.length) {
case 2: {
try {
return (!isNaN(this.getNumber(words[0])) && this.isCurrencyCode(words[1]));
} catch (err) {
return false;
}
}
} else if (words.length === 4) {
try {
return (
keywords.includes(words[2].toLowerCase()) &&
!isNaN(this.getNumber(words[0])) &&
this.isCurrencyCode(words[1]) &&
this.isCurrencyCode(words[3])
);
} catch (err) {
return false;
case 3: {
try {
return (
!isNaN(this.getNumber(words[0])) &&
this.isCurrencyCode(words[1]) &&
this.isCurrencyCode(words[2])
);
} catch (err) {
return false;
}
}
} else {
return false;
case 4: {
try {
return (
keywords.includes(words[2].toLowerCase()) &&
!isNaN(this.getNumber(words[0])) &&
this.isCurrencyCode(words[1]) &&
this.isCurrencyCode(words[3])
);
} catch (err) {
return false;
}
}
default:
return false;
}
}

Expand Down Expand Up @@ -115,26 +125,38 @@ export class CurrencyConverterPlugin implements ExecutionPlugin {

private buildCurrencyConversion(userInput: string): CurrencyConversion {
const words = userInput.trim().split(" ");
if (words.length === 3) {
return {
base:
Object.values(CurrencyCode).find((c: CurrencyCode) => c.toLowerCase() === words[1].toLowerCase()) ||
CurrencyCode.EUR,
target:
Object.values(CurrencyCode).find((c: CurrencyCode) => c.toLowerCase() === words[2].toLowerCase()) ||
CurrencyCode.USD,
value: this.getNumber(words[0]),
};
} else{
return {
base:
Object.values(CurrencyCode).find((c: CurrencyCode) => c.toLowerCase() === words[1].toLowerCase()) ||
CurrencyCode.EUR,
target:
Object.values(CurrencyCode).find((c: CurrencyCode) => c.toLowerCase() === words[3].toLowerCase()) ||
CurrencyCode.USD,
value: this.getNumber(words[0]),
};
switch (words.length) {
case 2: {
return {
base:
Object.values(CurrencyCode).find((c: CurrencyCode) => c.toLowerCase() === words[1].toLowerCase()) ||
CurrencyCode.EUR,
target: this.config.defaultTarget,
value: this.getNumber(words[0]),
};
}
case 3: {
return {
base:
Object.values(CurrencyCode).find((c: CurrencyCode) => c.toLowerCase() === words[1].toLowerCase()) ||
CurrencyCode.EUR,
target:
Object.values(CurrencyCode).find((c: CurrencyCode) => c.toLowerCase() === words[2].toLowerCase()) ||
CurrencyCode.USD,
value: this.getNumber(words[0]),
};
}
default: {
return {
base:
Object.values(CurrencyCode).find((c: CurrencyCode) => c.toLowerCase() === words[1].toLowerCase()) ||
CurrencyCode.EUR,
target:
Object.values(CurrencyCode).find((c: CurrencyCode) => c.toLowerCase() === words[3].toLowerCase()) ||
CurrencyCode.USD,
value: this.getNumber(words[0]),
};
}
}
}
}
17 changes: 17 additions & 0 deletions src/renderer/settings/currency-converter-settings-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { PluginSettings } from "./plugin-settings";
import { TranslationSet } from "../../common/translation/translation-set";
import { UserConfirmationDialogParams, UserConfirmationDialogType } from "./modals/user-confirmation-dialog-params";
import { deepCopy } from "../../common/helpers/object-helpers";
import { CurrencyCode } from "../../main/plugins/currency-converter-plugin/currency-code";

export const currencyConverterSettingsComponent = Vue.extend({
data() {
return {
settingName: PluginSettings.CurrencyConverter,
currencyCodes: Object.values(CurrencyCode),
visible: false,
};
},
Expand Down Expand Up @@ -85,6 +87,21 @@ export const currencyConverterSettingsComponent = Vue.extend({
</div>
</div>
<div class="settings__option">
<div class="settings__option-name">Default Currency Target</div>
<div class="settings__option-content">
<div class="field is-grouped is-grouped-right">
<div class="control">
<div class="select">
<select v-model="config.currencyConverterOptions.defaultTarget" @change="updateConfig">
<option v-for="currencyCode in currencyCodes">{{ currencyCode }}</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 3520748

Please sign in to comment.