salix-front/src/composables/getTotal.js

11 lines
383 B
JavaScript

import { toCurrency } from 'src/filters';
export function getTotal(rows, key, opts = {}) {
const { currency, cb, decimalPlaces } = opts;
const total = rows.reduce((acc, row) => acc + +(cb ? cb(row) : row[key] || 0), 0);
return currency
? toCurrency(total, currency == 'default' ? undefined : currency)
: parseFloat(total).toFixed(decimalPlaces ?? 2);
}