diff --git a/package-lock.json b/package-lock.json
index edaa304..ea573a7 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "mathjs-mathml",
- "version": "0.3.0",
+ "version": "0.3.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
diff --git a/package.json b/package.json
index 3bfeb5b..11c0646 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "mathjs-mathml",
- "version": "0.3.0",
+ "version": "0.3.1",
"description": "Translation of mathjs object to MathML",
"main": "src/index.js",
"scripts": {
diff --git a/src/cmathml-handler.js b/src/cmathml-handler.js
index e8a617b..f58df6c 100644
--- a/src/cmathml-handler.js
+++ b/src/cmathml-handler.js
@@ -31,15 +31,28 @@ function cMathMLHandler(_this, { handler, csymbols }){
} else if (_this.type === 'FunctionNode') {
let args = _this.args
.map((arg) => arg.toString({handler, csymbols}));
- if(_this.fn.name==='cube'){
+ if (_this.fn.name==='cube') {
return `${args[0]}3`;
- }else if(_this.fn.name==='square'){
+ } else if(_this.fn.name==='square') {
return `${args[0]}2`;
- }else if(_this.fn.name==='log' && _this.args.length===2){
+ } else if(_this.fn.name==='log' && _this.args.length===2) {
return `${args[1]}${args[0]}`;
- }else if(_this.fn.name==='log2'){
+ } else if(_this.fn.name==='log2') {
return `2${args[0]}`;
- }else{ // change only function name
+ } else if (_this.fn.name === 'piecewise') {
+ if (args.length === 0) throw new Error('piecewise function must have at least one argument.');
+
+ let hasOtherwise = args.length % 2 == 1; // is odd
+ let otherwiseTag = hasOtherwise ? `${args[args.length - 1]}` : ``;
+
+ let piecesCount = Math.floor(args.length / 2);
+ let piecesTags = [];
+ for (let i = 0; i < piecesCount; i++) {
+ let tag = `${args[2*i+1]}${args[2*i]}`;
+ piecesTags.push(tag);
+ }
+ return `${piecesTags.join('')}${otherwiseTag}`;
+ } else { // change only function name
return `<${dictFunc[_this.fn.name] || _this.fn.name}/>${args.join('')}`;
}
} else if (_this.type === 'OperatorNode') {
diff --git a/test/cases/nodes.json b/test/cases/nodes.json
index 57761d3..46302b6 100644
--- a/test/cases/nodes.json
+++ b/test/cases/nodes.json
@@ -132,5 +132,13 @@
"not false": {
"formula": "not false",
"expected": ""
+ },
+ "piecewise": {
+ "formula": "123 * piecewise(x > 1, 1, x > 2, 2, 0)",
+ "expected": "1231x12x20"
+ },
+ "piecewise without otherwise": {
+ "formula": "123 * piecewise(x > 1, 1, x > 2, 2)",
+ "expected": "1231x12x2"
}
}