Skip to content

Instantly share code, notes, and snippets.

@brentonhouse
Forked from adamtarmstrong/ti_helper.js
Created April 29, 2020 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentonhouse/05094398350b5cd0dc5c2e0835719116 to your computer and use it in GitHub Desktop.
Save brentonhouse/05094398350b5cd0dc5c2e0835719116 to your computer and use it in GitHub Desktop.
Titanium Helper Functions
//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