Created
September 11, 2018 16:36
-
-
Save santaklouse/729c3feb328fde289d35603c09cf2763 to your computer and use it in GitHub Desktop.
AngularJS filters example
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
| (function(angular) { | |
| 'use strict'; | |
| angular.module('unknown.filters', ['unknown.services']) | |
| .filter('formatMinutes', ['dateUtils', function(dateUtils) { | |
| return function(minutes, leadingZero) { | |
| if (_.isUndefined(leadingZero)) { | |
| leadingZero = true; | |
| } | |
| if (!minutes) { | |
| return leadingZero ? '00:00' : '0:00'; | |
| } | |
| var hour = Math.floor(minutes / 60); | |
| hour = leadingZero ? (hour ? dateUtils._zlead(hour) : '00') : hour; | |
| var min = minutes - (hour * 60); | |
| return [hour, min || '00'].join(':'); | |
| }; | |
| }]) | |
| .filter('decodeEntity', function() { | |
| return function(string) { | |
| if (!string) { | |
| return ''; | |
| } | |
| return string | |
| .replace(/"/g, '"') | |
| .replace(/'/g, "'") | |
| .replace(/</g, '<') | |
| .replace(/>/g, '>') | |
| .replace(/$/g, '$') | |
| .replace(/&/g, '&'); | |
| } | |
| }) | |
| .filter('procEntity', function() { | |
| return function(str) { | |
| if (!str) { | |
| return ''; | |
| } | |
| return str | |
| .replace(/&/g, '&') | |
| .replace(/"/g, '"') | |
| .replace(/'/g, ''') | |
| .replace(/</g, '<') | |
| .replace(/>/g, '>') | |
| .replace(/{/g, '{') | |
| .replace(/}/g, '}') | |
| .replace(/\$/g, '$'); | |
| } | |
| }) | |
| .filter('procSlash', function() { | |
| return function(str) { | |
| if (!str) { | |
| return ''; | |
| } | |
| return str | |
| .replace(/\//g, '/'); | |
| } | |
| }) | |
| .filter('formatTz', ['dateUtils', function(dateUtils) { | |
| return function() { | |
| return dateUtils.formatTz.apply(dateUtils, arguments); | |
| }; | |
| }]) | |
| .filter('formatCurrency', ['$filter', function($filter) { | |
| /** | |
| * formatting currency | |
| * @param {Number} number number to format | |
| * @param {Object} options formatting options (symbol, positiveFormat, negativeFormat, decimalSymbol, digitGroupSymbol, groupDigits, round) | |
| * @returns {String} formatted string | |
| * @example | |
| * $filter('formatCurrency')(2424.2);//returns '$2,424.20' | |
| */ | |
| return function(number, options) { | |
| options = angular.extend({ | |
| symbol: '$', | |
| positiveFormat: '%v', | |
| negativeFormat: '(%v)', | |
| showZero: true, | |
| round: 2, | |
| decimalZeros: 2 | |
| }, options); | |
| if (!number || isNaN(number) || (number === '-' && options.round === -1)) { | |
| if (!options.showZero) { | |
| return ' '; | |
| } | |
| number = 0; | |
| } | |
| var format = (number >= 0) ? options.positiveFormat : options.negativeFormat, | |
| numStr = $filter('currency')(Math.abs(number), options.symbol, options.decimalZeros); | |
| //in case numStr starts with $0 - IE treats it as special char and replacing does not work correctly, | |
| // adding one more $ sing at the start of the string fixes issue | |
| numStr = options.symbol === '$' ? '$' + numStr : numStr; | |
| return format.replace(/%v/g, numStr); | |
| } | |
| }]) | |
| .filter('floatToTime', function() { | |
| /** | |
| * converts number to time | |
| * @param {String|Number} val number to convert | |
| * @returns {String} time string | |
| * @exampleg6yuy | |
| * for value 10.5 returns 10:30 | |
| */ | |
| return function(val, round) { | |
| var hours = parseInt(val, 10), | |
| minutes = 0; | |
| if (round) { | |
| minutes = Math.round(Math.round(((parseFloat(val) - hours) * 60) / 10) * 10); | |
| } else { | |
| minutes = Math.round((parseFloat(val) - hours) * 60); | |
| } | |
| if (minutes === 60) { | |
| minutes = 0; | |
| hours++; | |
| } | |
| return hours + ':' + ('00' + minutes).slice(-2); | |
| } | |
| }) | |
| .filter('shortDateParse', ['dateUtils', function(dateUtils) { | |
| return function(str) { | |
| return dateUtils.shortDateParse(str); | |
| } | |
| }]) | |
| .filter('capitalize', ['utils', function(utils) { | |
| return function() { | |
| return utils.capitalize.apply(utils, arguments); | |
| }; | |
| }]) | |
| })(angular); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment