Skip to content

Commit

Permalink
Merge pull request #13 from jalibu/feat/configureDecimals
Browse files Browse the repository at this point in the history
Feat/configure decimals
  • Loading branch information
jalibu authored Apr 10, 2021
2 parents b8c205f + 7412800 commit 2526c36
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 29 deletions.
2 changes: 1 addition & 1 deletion MMM-Jast.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ To use this module, add it to the modules array in the `MagicMirror/config/confi
showChangeValue: false,
showChangeValueCurrency: false,
showDepotGrowth: false,
numberDecimalsValues: 2,
numberDecimalsPercentages: 1,
stocks: [
{ name: "BASF", symbol: "BAS.DE", quantity: 10 },
{ name: "SAP", symbol: "SAP.DE", quantity: 15 },
Expand All @@ -54,6 +56,8 @@ To use this module, add it to the modules array in the `MagicMirror/config/confi
| stocks | (Array<Stock>) Array of stocks to be displayed | Sample set |
| scroll | (String) Animation direction for ticker. Values: none, vertical or horizontal | "vertical" |
| maxWidth | (String) CSS style to limit ticker width for vertical styles | "100%" |
| numberDecimalsValues | (Number) Number of decimals for stock values | 2 |
| numberDecimalsPercentages | (Number) Number of decimals for percentages | 1 |
| showCurrency | (Boolean) Show stocks currency | true |
| showChangePercent | (Boolean) Show stocks change against last close in percent | true |
| showChangeValue | (Boolean) Show stocks change against last close in absolute value | false |
Expand All @@ -63,6 +67,9 @@ To use this module, add it to the modules array in the `MagicMirror/config/confi
### Stock Object
| Field | Description | Example |
| -------- | -------- | -------- |
| name | (String) Stock's display name | "Alibaba" |
| symbol | (String) Stock's symbol/key | "BABA" |
| name | (String) Optional: Stock's display name | "Alibaba" |
| quantity | (Integer) Optional: To calculate depotGrowth | 500 |

### Where the hack do I get the symbol for my favorite stocks?!
The easiest way to get the symbol for your stock is to open [finance.yahoo.com](https://finance.yahoo.com) and use the search field on the top. You'll find the symbol either in the search preview or in the result page's URL (i.e. https://finance.yahoo.com/quote/BMW.DE?p=BMW.DE where BMW.DE would be the symbol).
2 changes: 2 additions & 0 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Module.register("MMM-Jast", {
],
scroll: "vertical",
maxWidth: "100%",
numberDecimalsValues: 2,
numberDecimalsPercentages: 1,
showCurrency: true,
showChangePercent: true,
showChangeValue: false,
Expand Down
34 changes: 25 additions & 9 deletions src/client/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,39 @@ import { StockResponse } from "../models/StockResponse"
import { Config } from "../models/Config"

export default class JastUtils {
static getStockChange(stock: StockResponse): number {
return Number((stock.price?.regularMarketChange).toFixed(2)) || 0
static getStockChange(stock: StockResponse, config: Config): number {
return Number((stock.price?.regularMarketChange).toFixed(config.numberDecimalsValues)) || 0
}

static getStockChangePercent(stock: StockResponse): number {
return Number((stock.price?.regularMarketChangePercent * 100).toFixed(1)) || 0
static getStockChangePercent(stock: StockResponse, config: Config): number {
return Number((stock.price?.regularMarketChangePercent * 100).toFixed(config.numberDecimalsPercentages)) || 0
}

static getCurrentValue(stock: StockResponse): number {
return Number(stock.price?.regularMarketPrice?.toFixed(2)) || 0
static getCurrentValue(stock: StockResponse, config: Config): number {
return Number(stock.price?.regularMarketPrice?.toFixed(config.numberDecimalsValues)) || 0
}

static getStockChangeAsString(stock: StockResponse, config: Config): string {
return this.getStockChange(stock, config).toLocaleString()
}

static getStockChangePercentAsString(stock: StockResponse, config: Config): string {
return this.getStockChangePercent(stock, config).toLocaleString()
}

static getCurrentValueAsString(stock: StockResponse, config: Config): string {
return this.getCurrentValue(stock, config).toLocaleString()
}

static getCurrency(stock: StockResponse): string {
return stock.summaryDetail?.currency || "?"
}

static getDepotGrowth(config: Config, stocks: StockResponse[]): DepotGrowth[] {
static getStockName(stock: StockResponse): string {
return stock.meta.name || stock.price.longName
}

static getDepotGrowth(stocks: StockResponse[], config: Config): DepotGrowth[] {
let depotGrowth: DepotGrowth[] = []
for (const stock of stocks) {
try {
Expand All @@ -30,7 +46,7 @@ export default class JastUtils {
if (existingCurrency) {
existingCurrency.value = existingCurrency.value + growthForStock
} else {
depotGrowth.push({ value: growthForStock, currency: stock.price.currency })
depotGrowth.push({ value: growthForStock, currency: stock.price.currency, valueAsString: growthForStock.toLocaleString() })
}
}
} catch (err) {
Expand All @@ -40,7 +56,7 @@ export default class JastUtils {
}

depotGrowth.forEach(growth => {
growth.value = Number(growth.value.toFixed(2))
growth.value = Number(growth.value.toFixed(config.numberDecimalsValues))
})
return depotGrowth
}
Expand Down
6 changes: 4 additions & 2 deletions src/models/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type Config = {
stocks: Stock[],
scroll: "vertical" | "horizontal" | "none",
maxWidth: string,
numberDecimalsValues: number,
numberDecimalsPercentages: number,
showCurrency: boolean,
showChangePercent: boolean,
showChangeValue: boolean,
Expand All @@ -13,7 +15,7 @@ export type Config = {
};

type Stock = {
name: string,
symbol: string,
quantity: number
name?: string,
quantity?: number
}
1 change: 1 addition & 0 deletions src/models/DepotGrowth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type DepotGrowth = {
value: number
valueAsString: string
currency: string
}
16 changes: 8 additions & 8 deletions templates/HorizontalStockList.njk
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
<p class="jast-hticker jast-ticker-no{{ num }}">
<span class="jast-tickerframe">
{% for stock in stocks %}
<span class="jast-stock">{{ stock.meta.name }}:
{% if utils.getStockChange(stock) > 0 %}
<span class="jast-stock">{{ utils.getStockName(stock) }}:
{% if utils.getStockChange(stock, config) > 0 %}
{% set colorClass = "high" %}
{% elif utils.getStockChange(stock) < 0 %}
{% elif utils.getStockChange(stock, config) < 0 %}
{% set colorClass = "low " %}
{% else %}
{% set colorClass = "" %}
{% endif %}
<span class="{{ colorClass }}">
{{ utils.getCurrentValue(stock) }}
{{ utils.getCurrentValueAsString(stock, config) }}
{% if config.showCurrency %}{{ utils.getCurrency(stock) }}{% endif %}
{% if colorClass and (config.showChangeValue or config.showChangePercent) %}
<span class="jast-change">
({% if config.showChangeValue %}<span class="jast-change-value">{% if colorClass == "high" %}+{%endif%}{{ utils.getStockChange(stock) }}{% if config.showChangeValueCurrency %} {{ utils.getCurrency(stock) }}{% endif %}</span>{% endif %}
{% if config.showChangePercent %}<span class="jast-change-value">{% if colorClass == "high" %}+{%endif%}{{ utils.getStockChangePercent(stock) }}%</span>{% endif %})
({% if config.showChangeValue %}<span class="jast-change-value">{% if colorClass == "high" %}+{%endif%}{{ utils.getStockChangeAsString(stock, config) }}{% if config.showChangeValueCurrency %} {{ utils.getCurrency(stock) }}{% endif %}</span>{% endif %}
{% if config.showChangePercent %}<span class="jast-change-value">{% if colorClass == "high" %}+{%endif%}{{ utils.getStockChangePercentAsString(stock, config) }}%</span>{% endif %})
</span>
{% endif %}
</span>
</span>
{% endfor %}
{% if config.showDepotGrowth %}
{% set depotGrowth = utils.getDepotGrowth(config, stocks) %}
{% set depotGrowth = utils.getDepotGrowth(stocks, config) %}
<span class="jast-stock">{{ "depotGrowth" | translate | safe }}
{% for growth in depotGrowth %}
{% if growth.value > 0 %}
Expand All @@ -35,7 +35,7 @@
{% else %}
{% set colorClass = "" %}
{% endif %}
<span class="{{ colorClass }}{% if loop.index != 1 %} jast-multi-currency{% endif %}">{{ growth.value }} {{ growth.currency }}</span>
<span class="{{ colorClass }}{% if loop.index != 1 %} jast-multi-currency{% endif %}">{{ growth.valueAsString }} {{ growth.currency }}</span>
{% endfor %}
</span>
{% endif %}
Expand Down
16 changes: 8 additions & 8 deletions templates/VerticalStockList.njk
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@
{% endif %}
<ul {{ animationStyle | safe }}>
{% for stock in stocks %}
<li class="jast-stock">{{ stock.meta.name }}:
{% if utils.getStockChange(stock) > 0 %}
<li class="jast-stock">{{ utils.getStockName(stock) }}:
{% if utils.getStockChange(stock, config) > 0 %}
{% set colorClass = "high" %}
{% elif utils.getStockChange(stock) < 0 %}
{% elif utils.getStockChange(stock, config) < 0 %}
{% set colorClass = "low " %}
{% else %}
{% set colorClass = "" %}
{% endif %}
<span class="{{ colorClass }}">
{{ utils.getCurrentValue(stock) }}
{{ utils.getCurrentValueAsString(stock, config) }}
{% if config.showCurrency %}{{ utils.getCurrency(stock) }}{% endif %}
{% if colorClass and (config.showChangeValue or config.showChangePercent) %}
<span class="jast-change">
({% if config.showChangeValue %}<span class="jast-change-value">{% if colorClass == "high" %}+{%endif%}{{ utils.getStockChange(stock) }}{% if config.showChangeValueCurrency %} {{ utils.getCurrency(stock) }}{% endif %}</span>{% endif %}
{% if config.showChangePercent %}<span class="jast-change-value">{% if colorClass == "high" %}+{%endif%}{{ utils.getStockChangePercent(stock) }}%</span>{% endif %})
({% if config.showChangeValue %}<span class="jast-change-value">{% if colorClass == "high" %}+{%endif%}{{ utils.getStockChangeAsString(stock, config) }}{% if config.showChangeValueCurrency %} {{ utils.getCurrency(stock) }}{% endif %}</span>{% endif %}
{% if config.showChangePercent %}<span class="jast-change-value">{% if colorClass == "high" %}+{%endif%}{{ utils.getStockChangePercentAsString(stock, config) }}%</span>{% endif %})
</span>
{% endif %}
</span>
</li>
{% endfor %}
{% if config.showDepotGrowth %}
{% set depotGrowth = utils.getDepotGrowth(config, stocks) %}
{% set depotGrowth = utils.getDepotGrowth(stocks, config) %}
<li class="jast-stock">{{ "depotGrowth" | translate | safe }}
{% for growth in depotGrowth %}
{% if growth.value > 0 %}
Expand All @@ -43,7 +43,7 @@
{% else %}
{% set colorClass = "" %}
{% endif %}
<span class="{{ colorClass }}{% if loop.index != 1 %} jast-multi-currency{% endif %}">{{ growth.value }} {{ growth.currency }}</span>
<span class="{{ colorClass }}{% if loop.index != 1 %} jast-multi-currency{% endif %}">{{ growth.valueAsString }} {{ growth.currency }}</span>
{% endfor %}
</li>
{% endif %}
Expand Down

0 comments on commit 2526c36

Please sign in to comment.