Created
July 13, 2017 14:48
-
-
Save guilhermegregio/ed29f167dbe40d45c8db8c9fb7c5dc69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var expToNumber = { | |
/** | |
* Used to return only number of string value | |
*/ | |
parseExpression: function (expression) { | |
var parts = expression.split('|'); | |
if (parts.length < 2) { | |
var sb = []; | |
sb.push("[1] - tonumber(text | format) - tonumber(R$16,70|d) - 16.7"); | |
sb.push("[2] - tonumber(text | format) - tonumber(R$16,70|i) - 16"); | |
throw sb.join('\n'); | |
} | |
var text = parts[0].trim(); | |
var format = parts[1].trim(); | |
var item = text.replace(/[^0-9,]/g, '').replace(',', '.'); | |
switch (format) { | |
case 'i': | |
return parseInt(item).toString(); | |
case 'd': | |
return parseFloat(item).toFixed(2); | |
default: | |
break; | |
} | |
} | |
}; | |
module.exports = expToNumber; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment