Created
July 24, 2018 16:48
-
-
Save adamtarmstrong/d0e1667ec2e23837e8971cf5a86aa435 to your computer and use it in GitHub Desktop.
Titanium Helper Functions
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 tiHelper = require('ti_helper'); | |
/* | |
* USAGE | |
* tiHelper.currency(numberToFormat,0); | |
*/ | |
exports.currency = function(number, decimals, decSymbol, thousSymbol) { //number, decimal places , decimal symobl (.), thousands separator (,) | |
decimals = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals, | |
decSymbol = decSymbol == undefined ? "." : decSymbol, | |
thousSymbol = thousSymbol == undefined ? "," : thousSymbol, | |
posNegSymbol = number < 0 ? "-" : "", | |
i = parseInt(number = Math.abs(+number || 0).toFixed(decimals)) + "", | |
j = (j = i.length) > 3 ? j % 3 : 0; | |
return posNegSymbol + '$' + (j ? i.substr(0, j) + thousSymbol : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousSymbol) + (decimals ? decSymbol + Math.abs(number - i).toFixed(decimals).slice(2) : ""); | |
}; | |
/* | |
* -extends: https://docs.appcelerator.com/platform/latest/#!/guide/Lightweight_Persistence_with_the_Properties_API | |
* USAGE | |
* tiHelper.setJson("testJsonVarName",jsonObject); //saves JS Object as {String} | |
* tiHelper.getJson("testJsonVarName"); //returns JS Object or FALSE | |
*/ | |
exports.setJson = function(propName, jsObject) { | |
return Ti.App.Properties.setString(propName, JSON.stringify(jsObject)); | |
}; | |
exports.getJson = function(propName) { | |
if (Ti.App.Properties.hasProperty(propName)) { | |
return JSON.parse(Ti.App.Properties.getString(propName)); | |
} else { | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment