Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
chore: deprecate numberFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed Jan 7, 2022
1 parent 021d1d2 commit 36f9e47
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 212 deletions.
18 changes: 8 additions & 10 deletions common/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
const utils = require('../utils');
const numberFormat = require('../utils/numberFormat');
const format = require('../utils/format');
const errors = require('./errors');
const BaseDocument = require('frappejs/model/document');
const BaseMeta = require('frappejs/model/meta');

module.exports = {
initLibs(frappe) {
Object.assign(frappe, utils);
Object.assign(frappe, numberFormat);
Object.assign(frappe, format);
frappe.errors = errors;
frappe.BaseDocument = BaseDocument;
frappe.BaseMeta = BaseMeta;
}
}
initLibs(frappe) {
Object.assign(frappe, utils);
Object.assign(frappe, format);
frappe.errors = errors;
frappe.BaseDocument = BaseDocument;
frappe.BaseMeta = BaseMeta;
},
};
3 changes: 1 addition & 2 deletions model/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const frappe = require('frappejs');
const Observable = require('frappejs/utils/observable');
const naming = require('./naming');
const { isPesa } = require('../utils/index');
const { round } = require('frappejs/utils/numberFormat');
const { DEFAULT_INTERNAL_PRECISION } = require('../utils/consts');

module.exports = class BaseDocument extends Observable {
Expand Down Expand Up @@ -696,7 +695,7 @@ module.exports = class BaseDocument extends Observable {
}
const precision =
frappe.SystemSettings.internalPrecision ?? DEFAULT_INTERNAL_PRECISION;
return round(value, precision);
return frappe.pesa(value).clip(precision).float;
}

isNew() {
Expand Down
35 changes: 0 additions & 35 deletions tests/test_utils.js

This file was deleted.

75 changes: 56 additions & 19 deletions utils/format.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const numberFormat = require('./numberFormat');
const luxon = require('luxon');
const frappe = require('frappejs');
const { DEFAULT_DISPLAY_PRECISION, DEFAULT_LOCALE } = require('./consts');

module.exports = {
format(value, df, doc) {
Expand All @@ -13,7 +13,8 @@ module.exports = {
}

if (df.fieldtype === 'Currency') {
value = formatCurrency(value, df, doc);
const currency = getCurrency(df, doc);
value = formatCurrency(value, currency);
} else if (df.fieldtype === 'Date') {
let dateFormat;
if (!frappe.SystemSettings) {
Expand Down Expand Up @@ -47,34 +48,70 @@ module.exports = {
}
return value;
},
formatCurrency,
formatNumber,
};

function getCurrency(df, doc) {
if (!(doc && df.getCurrency)) {
return df.currency || frappe.AccountingSettings.currency || '';
}

if (doc.meta && doc.meta.isChild) {
return df.getCurrency(doc, doc.parentdoc);
}

return df.getCurrency(doc);
}

function formatCurrency(value, df, doc) {
const currency = getCurrency(df, doc);
function formatCurrency(value, currency) {
let valueString;
try {
valueString = numberFormat.formatCurrency(value);
valueString = formatNumber(value);
} catch (err) {
err.message += ` value: '${value}', type: ${typeof value}`;
console.error(df);
throw err;
}
const currencySymbol = frappe.currencySymbols[currency];

const currencySymbol = frappe.currencySymbols[currency];
if (currencySymbol) {
return currencySymbol + ' ' + valueString;
}

return valueString;
}

function formatNumber(value) {
const currencyFormatter = getNumberFormatter();
if (typeof value === 'number') {
return currencyFormatter.format(value);
}

if (value.round) {
return currencyFormatter.format(value.round());
}

const formattedCurrency = currencyFormatter.format(value);
if (formattedCurrency === 'NaN') {
throw Error(
`invalid value passed to formatCurrency: '${value}' of type ${typeof value}`
);
}

return formattedCurrency;
}

function getNumberFormatter() {
if (frappe.currencyFormatter) {
return frappe.currencyFormatter;
}

const locale = frappe.SystemSettings.locale ?? DEFAULT_LOCALE;
const display =
frappe.SystemSettings.displayPrecision ?? DEFAULT_DISPLAY_PRECISION;

return (frappe.currencyFormatter = Intl.NumberFormat(locale, {
style: 'decimal',
minimumFractionDigits: display,
}));
}

function getCurrency(df, doc) {
if (!(doc && df.getCurrency)) {
return df.currency || frappe.AccountingSettings.currency || '';
}

if (doc.meta && doc.meta.isChild) {
return df.getCurrency(doc, doc.parentdoc);
}

return df.getCurrency(doc);
}
146 changes: 0 additions & 146 deletions utils/numberFormat.js

This file was deleted.

0 comments on commit 36f9e47

Please sign in to comment.