1import { isArray, isNull, isObject, isUndefined } from 'lodash'; 2import angular from 'angular'; 3import coreModule from '../core_module'; 4import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv'; 5import { dateTime } from '@grafana/data'; 6 7coreModule.filter('stringSort', () => { 8 return (input: any) => { 9 return input.sort(); 10 }; 11}); 12 13coreModule.filter('slice', () => { 14 return (arr: any[], start: any, end: any) => { 15 if (!isUndefined(arr)) { 16 return arr.slice(start, end); 17 } 18 return arr; 19 }; 20}); 21 22coreModule.filter('stringify', () => { 23 return (arr: any[]) => { 24 if (isObject(arr) && !isArray(arr)) { 25 return angular.toJson(arr); 26 } else { 27 return isNull(arr) ? null : arr.toString(); 28 } 29 }; 30}); 31 32coreModule.filter('moment', () => { 33 return (date: string, mode: string) => { 34 switch (mode) { 35 case 'ago': 36 return dateTime(date).fromNow(); 37 } 38 return dateTime(date).fromNow(); 39 }; 40}); 41 42function interpolateTemplateVars(templateSrv: TemplateSrv = getTemplateSrv()) { 43 const filterFunc: any = (text: string, scope: any) => { 44 let scopedVars; 45 if (scope.ctrl) { 46 scopedVars = (scope.ctrl.panel || scope.ctrl.row).scopedVars; 47 } else { 48 scopedVars = scope.row.scopedVars; 49 } 50 51 return templateSrv.replaceWithText(text, scopedVars); 52 }; 53 54 filterFunc.$stateful = true; 55 return filterFunc; 56} 57 58coreModule.filter('interpolateTemplateVars', interpolateTemplateVars); 59export default {}; 60