From 9151b59bc9c2d46a1168344944ae3c5e4a7964f1 Mon Sep 17 00:00:00 2001 From: Justin Pomeroy Date: Thu, 18 Feb 2016 10:25:56 -0600 Subject: [PATCH] Allow optional default value in noValue filter This updates the noValue filter so an optional default value can be provided instead of the '-'. Change-Id: I0fb800407ffc2166c7cda427833bb4dd012c0643 --- horizon/static/framework/util/filters/filters.js | 7 ++++--- horizon/static/framework/util/filters/filters.spec.js | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/horizon/static/framework/util/filters/filters.js b/horizon/static/framework/util/filters/filters.js index 98cd79e431..439a16691b 100644 --- a/horizon/static/framework/util/filters/filters.js +++ b/horizon/static/framework/util/filters/filters.js @@ -126,13 +126,14 @@ * @ngdoc filter * @name noValue * @description - * Replaces null / undefined / empty string with translated '-'. + * Replaces null / undefined / empty string with translated '-' or the optional + * default value provided. */ function noValueFilter() { - return function (input) { + return function (input, def) { if (input === null || angular.isUndefined(input) || (angular.isString(input) && '' === input.trim())) { - return gettext('-'); + return def || gettext('-'); } else { return input; } diff --git a/horizon/static/framework/util/filters/filters.spec.js b/horizon/static/framework/util/filters/filters.spec.js index da330369d5..f50c80e204 100644 --- a/horizon/static/framework/util/filters/filters.spec.js +++ b/horizon/static/framework/util/filters/filters.spec.js @@ -151,6 +151,15 @@ expect(noValueFilter('')).toBe('-'); expect(noValueFilter(' ')).toBe('-'); }); + + it('replaces undefined, null, blank with provided value', function () { + expect(noValueFilter(null, 'default')).toBe('default'); + expect(noValueFilter(undefined, 'default')).toBe('default'); + expect(noValueFilter('', 'default')).toBe('default'); + expect(noValueFilter(' ', 'default')).toBe('default'); + expect(noValueFilter('value', 'default')).toBe('value'); + expect(noValueFilter(false, 'default')).toBe(false); + }); }); describe('noName', function () {