Skip to content

Commit

Permalink
Refactor money format functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
benguozakinci@gmail.com authored and benguozakinci@gmail.com committed Sep 2, 2021
1 parent 2ab39c4 commit 0a91934
Showing 1 changed file with 40 additions and 49 deletions.
89 changes: 40 additions & 49 deletions app/Traits/Charts.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function getDonutChartOptions($colors)
];
}

public function formatMoney( ){

}

public function getLineChartOptions()
{
return [
Expand All @@ -109,67 +113,53 @@ public function getLineChartOptions()
'position' => 'nearest',
'callbacks' => [
'label' => new Raw("function(tooltipItem, data) {
const isPrefix = '" . config('money.' . setting('default.currency') . '.symbol_first') . "'
const money = {
const moneySettings = {
decimal: '" . config('money.' . setting('default.currency') . '.decimal_mark') . "',
thousands: '". config('money.' . setting('default.currency') . '.thousands_separator') . "',
prefix: '" . config('money.' . setting('default.currency') . '.symbol') . "',
suffix: '" . config('money.' . setting('default.currency') . '.symbol') . "',
symbol: '" . config('money.' . setting('default.currency') . '.symbol') . "',
isPrefix: '" . config('money.' . setting('default.currency') . '.symbol_first') . "',
precision: '" . config('money.' . setting('default.currency') . '.precision') . "',
}
};
const formattedCurrency = function (input, opt = moneySettings) {
function fixed (precision) {
return Math.max(0, Math.min(precision, 20));
};
function toStr(value) {
return value ? value.toString() : '';
};
function numbersToCurrency(numbers, precision) {
var exp = Math.pow(10, precision);
var float = parseFloat(numbers) / exp;
return float.toFixed(fixed(precision));
};
function joinIntegerAndDecimal (integer, decimal, separator) {
return decimal ? integer + separator + decimal : integer;
};
const format = function (input, opt = {
decimal: '" . config('money.' . setting('default.currency') . '.decimal_mark') . "',
thousands: '" . config('money.' . setting('default.currency') . '.thousands_separator') . "',
prefix: '" . config('money.' . setting('default.currency') . '.symbol') . "',
suffix: '" . config('money.' . setting('default.currency') . '.symbol') . "',
precision: '" . config('money.' . setting('default.currency') . '.precision') . "',
}) {
if (typeof input === 'number') {
input = input.toFixed(fixed(opt.precision));
}
};
var negative = input.indexOf('-') >= 0 ? '-' : '';
var numbers = toStr(input).replace(/\D+/g, '') || '0';
var currency = numbersToCurrency(numbers, 2);
var currency = numbersToCurrency(numbers, opt.precision);
var parts = toStr(currency).split('.');
var integer = parts[0];
var integer = parts[0].replace(/(\d)(?=(?:\d{3})+\b)/gm, opt.thousands);;
var decimal = parts[1];
integer.replace(/(\d)(?=(?:\d{3})+\b)/gm, `,`);
return opt.prefix + negative + joinIntegerAndDecimal(integer, decimal, opt.decimal) + opt.suffix;
}
const fixed = function (precision) {
return between(0, precision, 20)
}
const joinIntegerAndDecimal = function (integer, decimal, separator) {
return decimal ? integer + separator + decimal : integer;
}
const toStr = function (value) {
return value ? value.toString() : '';
}
const numbersToCurrency = function (numbers, precision) {
var exp = Math.pow(10, precision);
var float = parseFloat(numbers) / exp;
return float.toFixed(fixed(precision));
}
const between = function (min, n, max) {
return Math.max(min, Math.min(n, max));
}
if ('" . config('money.' . setting('default.currency') . '.symbol_first') . "' === 1) {
return format(tooltipItem.yLabel);
}
return format(tooltipItem.yLabel);
if(opt.isPrefix) {
return opt.symbol + negative + joinIntegerAndDecimal(integer, decimal, opt.decimal)
}
return negative + joinIntegerAndDecimal(integer, decimal, opt.decimal) + opt.suffix;
};
return formattedCurrency(tooltipItem.yLabel, moneySettings);
}")
],
],
Expand All @@ -178,6 +168,7 @@ public function getLineChartOptions()
'ticks' => [
'beginAtZero' => true,
'callback' => new Raw("function(value, index, values) {
return '" . config('money.' . setting('default.currency') . '.symbol') . "' + value;
}"),
],
Expand Down

0 comments on commit 0a91934

Please sign in to comment.