Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
fix(language) Update formatting for monetary amount to align with las…
Browse files Browse the repository at this point in the history
…ter draft

Signed-off-by: Jerome Simeon <jeromesimeon@me.com>
  • Loading branch information
jeromesimeon committed Aug 11, 2020
1 parent d34bd3c commit e2315fb
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 71 deletions.
47 changes: 38 additions & 9 deletions backends/javascript/ergo-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,14 +874,15 @@ function dateTimeFormat(date,f) {

/* MonetaryAmount Formating */
function monetaryAmountFormat(v,f) {
return f.replace(/0.0.00?0?/gi, function(a){
const sep1 = a.charAt(1);
const sep2 = a.charAt(3);
const len = a.length-4;
return f.replace(/0(.)0((.)(0+))?/gi, function(_a,sep1,_b,sep2,digits){
const len = digits ? digits.length : 0;
const vs = v.toFixed(len);
const d = vs.substring(vs.length - len);
let res = sep2 + d;
let i = vs.substring(0,vs.length - (len+1));
let res = '';
if (sep2) {
const d = vs.substring(vs.length - len);
res += sep2 + d;
}
let i = vs.substring(0,vs.length - (len === 0 ? 0 : len+1));
while (i.length > 3) {
res = sep1 + i.substring(i.length - 3) + res;
i = i.substring(0, i.length - 3);
Expand All @@ -891,11 +892,39 @@ function monetaryAmountFormat(v,f) {
}
function codeSymbol(c) {
switch (c) {
case 'USD' : return '$';
case 'EUR' : return '€';
case 'JPY' : return '¥';
case 'GBP' : return '£';
case 'AUD' : return 'A$';
case 'CAD' : return 'C$';
case 'CHF' : return 'CHF';
case 'CNY' : return '元';
case 'HKD' : return 'HK$';
case 'NZD' : return 'NZ$';
case 'KRW' : return '₩';
case 'SGD' : return 'S$';
case 'MXN' : return 'MEX$';
case 'INR' : return '₹';
case 'RUB' : return '₽';
case 'ZAR' : return 'R';
case 'TRY' : return '₺';
case 'BRL' : return 'R$';
case 'TWD' : return 'NT$';
case 'PLN' : return 'zł';
case 'USD' : return '$';
case 'JPY' : return '¥';
case 'THB' : return '฿';
case 'IDR' : return 'Rp';
case 'HUF' : return 'Ft';
case 'CZK' : return 'Kč';
case 'ILS' : return '₪';
case 'CLP' : return 'CLP$';
case 'PHP' : return '₱';
case 'AED' : return 'د.إ';
case 'COP' : return 'COL$';
case 'SAR' : return '﷼';
case 'MYR' : return 'RM';
case 'RON' : return 'L';
case 'BGN' : return 'лв.';
default : return c; // Defaults to ISO code
}
}
Expand Down
64 changes: 35 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,4 @@
}
},
"dependencies": {}
}
}
2 changes: 1 addition & 1 deletion packages/ergo-cli/extracted/ergoccore.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/ergo-cli/extracted/ergotopcore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/ergo-compiler/extracted/compilercore.js

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions packages/ergo-compiler/test/logicmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ describe('LogicManager', () => {
logicManager.compileLogicSync(false);
logicManager.getInvokeCall('helloworld').length.should.equal(250);
logicManager.getDispatchCall().length.should.equal(172);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
logicManager.compileLogicSync(false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
});

it('should succeed creating a dispatch call for a JS logic file with a contract class (ES6)', () => {
Expand Down Expand Up @@ -194,9 +194,9 @@ describe('LogicManager', () => {
logicManager.compileLogic(false).then((logicCode) => {
logicManager.getInvokeCall('helloworld').length.should.equal(250);
logicManager.getDispatchCall().length.should.equal(172);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
logicManager.compileLogicSync(false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
});
});

Expand All @@ -213,16 +213,16 @@ describe('LogicManager', () => {
logicManager.compileLogicSync(false);
logicManager.getInvokeCall('helloworld').length.should.equal(250);
logicManager.getDispatchCall().length.should.equal(172);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
logicManager.compileLogicSync(false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
});

it('should load a logic file (without extension) to the script manager', () => {
const logicManager = new LogicManager('cicero');
logicManager.addLogicFile(ergoSample,'test');
logicManager.compileLogicSync(false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
});

it('should set the contract name', () => {
Expand All @@ -238,15 +238,15 @@ describe('LogicManager', () => {
logicManager.addLogicFile(ergoSample,'test.ergo');
logicManager.getTarget().should.equal('cicero');
logicManager.compileLogicSync(false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
logicManager.setTarget('es6', true);
logicManager.getTarget().should.equal('es6');
const contractName = 'org.accordproject.helloemit.HelloWorld';
logicManager.setContractName(contractName);
logicManager.getContractName().should.equal(ErgoCompiler.contractCallName(contractName));
logicManager.getInvokeCall('helloworld').length.should.equal(250);
logicManager.getDispatchCall().length.should.equal(234);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(30972);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(31861);
});

it('should fail to create init and dispatch for ES6 without a contract name', () => {
Expand All @@ -256,29 +256,29 @@ describe('LogicManager', () => {
logicManager.compileLogicSync(false);
logicManager.getInvokeCall('helloworld').length.should.equal(250);
logicManager.getDispatchCall().length.should.equal(234);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(30972);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(31861);
});

it('should set the compilation target to ES6 but not recompile the logic', () => {
const logicManager = new LogicManager('cicero');
logicManager.addLogicFile(ergoSample,'test.ergo');
logicManager.compileLogicSync(false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
logicManager.setTarget('es6', false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
});

it('should set the compilation target to ES5', () => {
const logicManager = new LogicManager('cicero');
logicManager.addLogicFile(ergoSample,'test.ergo');
logicManager.getTarget().should.equal('cicero');
logicManager.compileLogicSync(false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
logicManager.setTarget('es5', true);
logicManager.getTarget().should.equal('es5');
logicManager.getInvokeCall('helloworld').length.should.equal(174);
logicManager.getDispatchCall().length.should.equal(158);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(30826);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(31715);
});

it('should fail to create init code for Java', () => {
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('LogicManager', () => {
'model.cto'
]);
modelManager.getModels()[0].content.length.should.equal(1330);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(40229);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(41118);
});

it('should load a directory with formula', async function () {
Expand All @@ -326,7 +326,7 @@ describe('LogicManager', () => {
'model.cto'
]);
modelManager.getModels()[0].content.length.should.equal(1330);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(35187);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(36076);
});
});

Expand All @@ -343,7 +343,7 @@ describe('LogicManager', () => {
'model.cto'
]);
modelManager.getModels()[0].content.length.should.equal(1330);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(40229);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(41118);
});

it('should load a Zip with formula', async function () {
Expand All @@ -358,7 +358,7 @@ describe('LogicManager', () => {
'model.cto'
]);
modelManager.getModels()[0].content.length.should.equal(1330);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(35187);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(36076);
});
});

Expand All @@ -379,7 +379,7 @@ describe('LogicManager', () => {
'model.cto'
]);
modelManager.getModels()[0].content.length.should.equal(1330);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(40229);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(41118);
});

it('should load a Zip with formula', async function () {
Expand All @@ -398,7 +398,7 @@ describe('LogicManager', () => {
'model.cto'
]);
modelManager.getModels()[0].content.length.should.equal(1330);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(35187);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(36076);
});
});

Expand Down Expand Up @@ -436,15 +436,15 @@ describe('LogicManager', () => {
logicManager.compileLogicSync(false);
logicManager.getInvokeCall('helloworld').length.should.equal(250);
logicManager.getDispatchCall().length.should.equal(172);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
logicManager.updateLogic(ergoSample,'test.ergo');
logicManager.compileLogicSync(false);
logicManager.updateLogic(ergoSample,'testNEW.ergo');
logicManager.compileLogicSync(false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
logicManager.updateLogic(ergoSample3,'test.ergo');
logicManager.compileLogicSync(false);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(32667);
logicManager.getScriptManager().getCompiledScript().getContents().length.should.equal(33556);
});

});
Expand Down
Loading

0 comments on commit e2315fb

Please sign in to comment.