commonsApp.filter('slice', function() { return function(arr, start, end) { return (arr || []).slice(start, end); }; }); commonsApp.filter('orderObjectBy', function() { return function(items, field, reverse) { var filtered = []; angular.forEach(items, function(item) { filtered.push(item); }); filtered.sort(function (a, b) { return (a[field] > b[field] ? 1 : -1); }); if(reverse) filtered.reverse(); return filtered; }; }); commonsApp.filter("toArray", function(){ return function(obj) { var result = []; angular.forEach(obj, function(val, key) { result.push(val); }); return result; }; }); commonsApp.filter("orderEmpty", function () { return function (array, key, type) { var present, empty, result; if(!angular.isArray(array)) return; present = array.filter(function (item) { return item[key]; }); empty = array.filter(function (item) { return (angular.isDefined(item[key]) ? 0 : -1); // return !item[key] || !key; }); switch(type) { case 'toBottom': result = present.concat(empty); break; case 'toTop': result = empty.concat(present); break; // ... etc, etc ... default: result = array; break; } return result; }; }); commonsApp.filter("ensureHttpPrefix", function () { return function(value) { if(value){ // Need to add prefix if we don't have http:// prefix already AND we don't have part of it if(value && !/^(http|https):\/\//i.test(value) && 'http://'.indexOf(value) === -1) { return 'http://' + value; } else return value; } }; }); commonsApp.filter("removeHttpPrefix", function () { return function(value) { if(value){ if(value && /^(http|https):\/\//i.test(value) && 'http://'.indexOf(value) === -1) { temp = value; if(temp.substr(-1) === '/') { temp = temp.substr(0, temp.length - 1); } value = temp.replace(/^(https?:\/\/)?(www\.)?/,''); return value; } else { temp = value; value = temp.replace(/^(https?:\/\/)?(www\.)?/,''); return value; } } }; }); commonsApp.filter("dateFormat_MMM_dd_yyyy", function(){ return function(obj) { var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var date = new Date(obj); return { "date":monthNames[date.getMonth()] + " " + date.getDate() +",", "year":date.getFullYear()}; }; }); commonsApp.filter("dateFormat_MMM_dd_yyyy_Cur_UTC", function(){ return function(obj) { var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var curDate = new Date(); var offset = curDate.getGmtOffset(); var date = new Date(obj + (3600000*offset)); return { "date":monthNames[date.getMonth()] + " " + date.getDate() +",", "year":date.getFullYear()}; }; }); commonsApp.filter('ellipsis', function() { return function (text, length, end) { if(text != undefined){ if (isNaN(length)) length = 10; if (end === undefined) end = "..."; if (text.length <= length || text.length - end.length <= length) { return text; } else { return String(text).substring(0, length-end.length) + end; } } }; }); commonsApp.filter('checkCompanyEndsWithS', function(){ return function(compName){ var pattern = /s$/i; if(compName.match(pattern)){ return compName + "'"; }else{ return compName + "'s"; } } }); commonsApp.filter('timeFormatDDM', function(){ return function (timeStamp, format,appendText) { var diffTime = new Date().getTime() - timeStamp; var diffDays = Math.floor(diffTime / (24*60*60*1000)); if (diffDays == 0) { var result={}; var diffHrs = diffTime / (60*60*1000); if (diffHrs >= 1) { result=Math.floor(diffHrs) + " h"; } else { var diffMins = diffTime / (60*1000); result=Math.floor(diffMins) + " m"; } if(appendText!=undefined){ result=result+" "+appendText; } return result; } return $.datepicker.formatDate( format,new Date(timeStamp)); } });