JSCS Cleanup-style guide cleanup for filters.js

Use angular's isString function rather than using a typeof comparison.
Separate filter registrations from filter module declaration. Use $inject to
inject i18n dependency.

Change-Id: I269bc571119acfbe1f153109a63f77cd8c519873
Partially-Implements: blueprint jscs-cleanup
This commit is contained in:
cbeasley 2015-07-08 15:29:17 -07:00
parent a336dbd707
commit c33d9d83f5
2 changed files with 58 additions and 28 deletions

View File

@ -16,14 +16,16 @@
(function () { (function () {
'use strict'; 'use strict';
/** angular
* @ngdoc overview .module('horizon.framework.util.filters')
* @name horizon.framework.util.filters .filter('yesno', yesNoFilter)
* @description .filter('gb', gbFilter)
* horizon.framework.util.filters provides common filters to be used within Horizon. .filter('mb', mbFilter)
* .filter('title', titleFilter)
*/ .filter('noUnderscore', noUnderscoreFilter)
angular.module('horizon.framework.util.filters', ['horizon.framework.util.i18n']) .filter('decode', decodeFilter)
.filter('bytes', bytesFilter)
.filter('itemCount', itemCountFilter);
/** /**
* @ngdoc filter * @ngdoc filter
@ -32,11 +34,12 @@
* Evaluates given input for standard truthiness and returns translation * Evaluates given input for standard truthiness and returns translation
* of 'Yes' and 'No' for true/false respectively. * of 'Yes' and 'No' for true/false respectively.
*/ */
.filter('yesno', ['horizon.framework.util.i18n.gettext', function (gettext) { yesNoFilter.$inject = ['horizon.framework.util.i18n.gettext'];
function yesNoFilter(gettext) {
return function (input) { return function (input) {
return (input ? gettext("Yes") : gettext("No")); return (input ? gettext("Yes") : gettext("No"));
}; };
}]) }
/** /**
* @ngdoc filter * @ngdoc filter
@ -45,7 +48,7 @@
* Expects numeric value and suffixes translated 'GB' with spacing. * Expects numeric value and suffixes translated 'GB' with spacing.
* Returns empty string if input is not a number or is null. * Returns empty string if input is not a number or is null.
*/ */
.filter('gb', function () { function gbFilter() {
return function (input) { return function (input) {
if (isNaN(input) || null === input) { if (isNaN(input) || null === input) {
return ''; return '';
@ -53,7 +56,7 @@
return interpolate(gettext("%s GB"), [input.toString()]); return interpolate(gettext("%s GB"), [input.toString()]);
} }
}; };
}) }
/** /**
* @ngdoc filter * @ngdoc filter
@ -62,7 +65,7 @@
* Expects numeric value and suffixes translated 'MB' with spacing. * Expects numeric value and suffixes translated 'MB' with spacing.
* Returns empty string if input is not a number or is null. * Returns empty string if input is not a number or is null.
*/ */
.filter('mb', function () { function mbFilter() {
return function (input) { return function (input) {
if (isNaN(input) || null === input) { if (isNaN(input) || null === input) {
return ''; return '';
@ -70,7 +73,7 @@
return interpolate(gettext("%s MB"), [input.toString()]); return interpolate(gettext("%s MB"), [input.toString()]);
} }
}; };
}) }
/** /**
* @ngdoc filter * @ngdoc filter
@ -78,16 +81,16 @@
* @description * @description
* Capitalizes leading characters of individual words. * Capitalizes leading characters of individual words.
*/ */
.filter('title', function () { function titleFilter() {
return function (input) { return function (input) {
if (typeof input !== 'string') { if (!angular.isString(input)) {
return input; return input;
} }
return input.replace(/(?:^|\s)\S/g, function (a) { return input.replace(/(?:^|\s)\S/g, function (a) {
return a.toUpperCase(); return a.toUpperCase();
}); });
}; };
}) }
/** /**
* @ngdoc filter * @ngdoc filter
@ -95,14 +98,14 @@
* @description * @description
* Replaces all underscores with spaces. * Replaces all underscores with spaces.
*/ */
.filter('noUnderscore', function () { function noUnderscoreFilter() {
return function (input) { return function (input) {
if (typeof input !== 'string') { if (!angular.isString(input)) {
return input; return input;
} }
return input.replace(/_/g, ' '); return input.replace(/_/g, ' ');
}; };
}) }
/** /**
* @ngdoc filter * @ngdoc filter
@ -112,12 +115,12 @@
* in given mapping, return key. This is useful when translations for * in given mapping, return key. This is useful when translations for
* codes are present. * codes are present.
*/ */
.filter('decode', function () { function decodeFilter() {
return function (input, mapping) { return function (input, mapping) {
var val = mapping[input]; var val = mapping[input];
return angular.isDefined(val) ? val : input; return angular.isDefined(val) ? val : input;
}; };
}) }
/** /**
* @ngdoc filter * @ngdoc filter
@ -126,7 +129,7 @@
* Returns a human-readable approximation of the input of bytes, * Returns a human-readable approximation of the input of bytes,
* converted to a useful unit of measure. Uses 1024-based notation. * converted to a useful unit of measure. Uses 1024-based notation.
*/ */
.filter('bytes', function () { function bytesFilter() {
return function (input) { return function (input) {
var kb = 1024; var kb = 1024;
var mb = kb * 1024; var mb = kb * 1024;
@ -146,7 +149,7 @@
return interpolate(gettext("%s bytes"), [Math.floor(input)]); return interpolate(gettext("%s bytes"), [Math.floor(input)]);
} }
}; };
}) }
/** /**
* @ngdoc filter * @ngdoc filter
@ -155,7 +158,7 @@
* Displays translated count in table footer. * Displays translated count in table footer.
* Takes only finite numbers. * Takes only finite numbers.
*/ */
.filter('itemCount', function () { function itemCountFilter() {
return function (input) { return function (input) {
var isNumeric = (input !== null && isFinite(input)); var isNumeric = (input !== null && isFinite(input));
var number = isNumeric ? Math.round(input) : 0; var number = isNumeric ? Math.round(input) : 0;
@ -163,6 +166,5 @@
var format = ngettext('Displaying %s item', 'Displaying %s items', count); var format = ngettext('Displaying %s item', 'Displaying %s items', count);
return interpolate(format, [count]); return interpolate(format, [count]);
}; };
}); }
})();
}());

View File

@ -0,0 +1,28 @@
/*
* (c) Copyright 2015 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
'use strict';
/**
* @ngdoc overview
* @name horizon.framework.util.filters
* @description
* horizon.framework.util.filters provides common filters to be used within Horizon.
*
*/
angular
.module('horizon.framework.util.filters', ['horizon.framework.util.i18n']);
})();