Skip to content

Commit 5d0bece

Browse files
author
jumptuner
committed
hack the currency to act more like cloud api for cur
1 parent bdbe01d commit 5d0bece

File tree

5 files changed

+62
-18
lines changed

5 files changed

+62
-18
lines changed

examples/simplecur.odt

4.77 KB
Binary file not shown.

formatters/number.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,33 +161,49 @@ function formatN(d, precision) {
161161
* @return {String} return converted values
162162
*
163163
*/
164-
function formatC(d, precisionOrFormat) {
165-
if (d !== null) console.log("formatC", d, precisionOrFormat);
164+
function formatC(d, precisionOrFormat, currencyCode) {
165+
if (d !== null) console.log("formatC d:", d);
166+
if (precisionOrFormat !== null)
167+
console.log("formatC precisionOrFormat:", precisionOrFormat);
168+
if (currencyCode !== null) console.log("formatC currencyCode:", currencyCode);
169+
170+
// Default currency to USD if not provided
171+
var _currency = currencyCode || "USD";
172+
166173
if (d !== null && typeof d !== "undefined") {
167-
var _locale = locale[this.lang] || locale.en; // TODO optimize, this test should be done before
168-
var _currency = this.modifiedCurrencyTarget || this.currency.target;
174+
var _locale = locale[this.lang] || locale.en;
175+
176+
// Access currency information
169177
var _currencyInfo = currency[_currency];
178+
if (!_currencyInfo) {
179+
console.error("Invalid or unsupported currency code:", _currency);
180+
return d; // Optionally handle this case more gracefully
181+
}
182+
170183
var _precision = _currencyInfo.precision;
171184
var _customPrec = parseInt(precisionOrFormat, 10);
172-
var _formatFn = _locale.currency.L;
185+
186+
// Determine precision or formatting function
173187
if (!isNaN(_customPrec)) {
174188
_precision = _customPrec;
175-
} else if (_locale.currency[precisionOrFormat] instanceof Function) {
189+
} else if (
190+
typeof precisionOrFormat === "string" &&
191+
_locale.currency[precisionOrFormat] instanceof Function
192+
) {
176193
_formatFn = _locale.currency[precisionOrFormat];
194+
} else {
195+
_formatFn = _locale.currency.L; // Default formatting function
177196
}
178-
var _valueRaw = _format(convCurr.call(this, d), _locale.number, _precision);
179-
// reset modifiedCurrencyTarget for next use
180-
this.modifiedCurrencyTarget = null;
181-
return _formatFn(
182-
_valueRaw,
183-
_currencyInfo.symbol,
184-
_currencyInfo.minSymbol,
185-
// eslint-disable-next-line eqeqeq
186-
d != 1 ? _currencyInfo.major + "s" : _currencyInfo.major,
187-
// eslint-disable-next-line eqeqeq
188-
d != 1 ? _currencyInfo.minor + "s" : _currencyInfo.minor,
189-
_currencyInfo.name
197+
198+
// Convert and format the value
199+
var _valueRaw = _format(
200+
convCurr.call(this, d, _currency),
201+
_locale.number,
202+
_precision
190203
);
204+
205+
// Prepend the currency symbol and return the result
206+
return _currencyInfo.symbol + _valueRaw;
191207
}
192208
return d;
193209
}

test/al.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var assert = require("assert");
2+
var carbone = require("../lib/index");
3+
var path = require("path");
4+
var fs = require("fs");
5+
var helper = require("../lib/helper");
6+
var params = require("../lib/params");
7+
var input = require("../lib/input");
8+
var converter = require("../lib/converter");
9+
var testPath = path.join(__dirname, "test_file");
10+
var spawn = require("child_process").spawn;
11+
var execSync = require("child_process").execSync;
12+
// Data to inject
13+
var data = {
14+
firstname: "John",
15+
lastname: "Doe",
16+
amount: 1000,
17+
};
18+
19+
// Generate a report using the sample template provided by carbone module
20+
// This LibreOffice template contains "Hello {d.firstname} {d.lastname} !"
21+
// Of course, you can create your own templates!
22+
carbone.render("../examples/simplecur.odt", data, function (err, result) {
23+
if (err) {
24+
return console.log(err);
25+
}
26+
// write the result
27+
fs.writeFileSync("resultcur.odt", result);
28+
});

test/result.odt

7.93 KB
Binary file not shown.

test/resultcur.odt

4.34 KB
Binary file not shown.

0 commit comments

Comments
 (0)