Skip to content

Instantly share code, notes, and snippets.

@imixtron
Last active September 19, 2019 08:26
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 imixtron/77d228b0ea6ebdbcc450730ae8ee36b6 to your computer and use it in GitHub Desktop.
Save imixtron/77d228b0ea6ebdbcc450730ae8ee36b6 to your computer and use it in GitHub Desktop.
Helper functions for angular.js
//server; /api/
var APP_PREFIX = 'pMeds_',
APP_URL = '/api/',
APP_TIMEOUT = 1000 * 30,
HTTP_REQUEST = null,
APP_DATA = {};
APP_FORMAT = 'json';
var API = {
isLoggedIn: function () {
return API.storage.get('isLoggedIn');
},
removeDuplicates: function (list) {
var result = [];
list.forEach(function (value, index) {
if (result.indexOf(value) == -1) {
result.push(value);
}
// console.log(i, e);
});
result.sort();
return result;
},
getCurrentFiscalYear: function () {
var fiscalYear = API.getFiscalYear();
var returnValue = fiscalYear.split(' - ')[0];
return returnValue;
},
logout: function () {
API.storage.remove("token");
API.storage.remove("username");
API.storage.remove("email");
API.storage.remove("role");
API.storage.remove("section");
API.storage.remove("name");
API.storage.remove("isLoggedIn");
API.reload();
},
getMimeType: function (type) {
var fileType = "";
switch (type) {
case "rtf":
fileType = "application/rtf";
break;
case "pdf":
fileType = "application/pdf";
break;
case "txt":
fileType = "text/plain";
break;
case "jpeg":
fileType = "image/jpeg";
break;
case "jpg":
fileType = "image/jpg";
break;
case "png":
fileType = "image/png";
break;
case "doc":
fileType = "image/msword";
break;
case "doc":
fileType = "image/msword";
break;
case "tiff":
fileType = "image/tiff";
break;
case "tiff":
fileType = "image/tiff";
break;
default:
fileType = "unknown"; // Or you can use the blob.type as fallback
break;
}
return fileType;
},
validatePassword: function(v) {
return /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/.test(v)
},
historyBack: function () {
window.history.back();
},
reload: function () {
location.reload();
},
capitalizeFirstLetter: function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
isNumeric: function (number) {
return !isNaN(parseFloat(number)) && isFinite(number);
},
getFiscalYear : function () {
var currentMonth = moment().month() + 1;
var year = "";
if (currentMonth >=7 && currentMonth <= 12 ) {
year = moment().year() + 1;
} else {
year = moment().year();
}
var fiscalRange = year + " - " + (year + 1);
return fiscalRange;
},
storage: {
get: function (key, skipParse) {
var data = localStorage.getItem(APP_PREFIX + key);
if (data) {
if (!skipParse) {
data = JSON.parse(data);
}
return data;
}
},
set: function (key, value, skipParse) {
if (!skipParse) {
value = JSON.stringify(value);
}
localStorage.setItem(APP_PREFIX + key, value);
},
remove: function (key) {
localStorage.removeItem(APP_PREFIX + key);
},
push: function (key, value) {
var data = this.get(key);
data.push(value);
this.set(key, data);
},
pull: function (key, findBy, value) {
var data = this.get(key);
data.removeObject(data.findBy(findBy, value));
this.set(key, data);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment