2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/utils/format.js
2019-08-30 15:34:47 +05:30

49 lines
1.3 KiB
JavaScript

const numberFormat = require('./numberFormat');
// const markdown = new (require('showdown').Converter)();
const luxon = require('luxon');
const frappe = require('frappejs');
module.exports = {
format(value, field) {
if (typeof field === 'string') {
field = { fieldtype: field };
}
if (field.fieldtype === 'Currency') {
if (field.currencyInfo) {
value = numberFormat.formatNumber(
value,
field.currencyInfo.numberFormat,
field.currencyInfo.symbol
);
} else {
value = numberFormat.formatNumber(value);
}
} else if (field.fieldtype === 'Text') {
// value = markdown.makeHtml(value || '');
} else if (field.fieldtype === 'Date') {
let dateFormat;
if (!frappe.SystemSettings) {
dateFormat = 'yyyy-MM-dd';
} else {
dateFormat = frappe.SystemSettings.dateFormat;
}
value = luxon.DateTime.fromISO(value).toFormat(dateFormat);
if (value === 'Invalid DateTime') {
value = '';
}
} else if (field.fieldtype === 'Check') {
typeof parseInt(value) === 'number'
? (value = parseInt(value))
: (value = Boolean(value));
} else {
if (value === null || value === undefined) {
value = '';
} else {
value = value + '';
}
}
return value;
}
};