简体   繁体   中英

How to catch null, undefined, blank values with AngularJS filter

I'm attempting to write a filter for use in a grid that will catch all null, undefined, blank string, or other similar values and display a dash "-". I've written the following so far, but it doesn't catch null values, and I'm wondering if it could be more succinct and possibly refactored to avoid three layers of nested if/else statements. Percentage values need to be checked that they're over 0 and under 1. Also, negative numbers and 0's should be returned as is. Thanks!

angular.module('AdverseEventsExplorer.main').filter('emptyCellFilter', function ($filter) {
    return function (input, cellFilter, args1, args2) {    

        if (cellFilter == undefined) {
            return (angular.isNumber(input) || angular.isDefined(input) && input.length > 0) ? input : '-';
        } else {
            if (cellFilter.match(/pctg|percent|pctgFilter|incidence/ig)) {
                return (input > 0 && input < 1.0000000) ? $filter(cellFilter)(input, args1, args2) : '-';
            } else {
                return (angular.isNumber(input) || angular.isDefined(input) && input.length > 0) ? input : '-';
            }
        }

    };
});

Version 2.0 taking into account @tymeJV's comment:

angular.module('AdverseEventsExplorer.main').filter('emptyCellFilter', function ($filter) {
    return function (input, cellFilter, args1, args2) {

        if (!cellFilter) {
            return (angular.isNumber(input) || (input)) ? input : '-';
        } else {
            if (cellFilter.match(/pctg|percent|pctgFilter|incidence/ig)) {
                return (input > 0 && input < 1.0000000) ? $filter(cellFilter)(input, args1, args2) : '-';
            } else {
                return (angular.isNumber(input) || (input)) ? $filter(cellFilter)(input, args1, args2) : '-';
            }
        }

    };
});

Whenever you encounter a function that's getting too complex to refactor try extracting some of the smaller statements to concisely named variables. It makes it much easier for our brains to keep track of the function's requirements, and it's also more readable to new devs reading your code.

var inputHasValue = angular.isNumber(input) || input;
if(!inputHasValue){
    return '-';
}
if (!cellFilter) {
    return input;
}

var isPercentageCell = cellFilter.match(/pctg|percent|pctgFilter|incidence/ig);
var valueIsInRange = input > 0 && input < 1;
if(!isPercentageCell || valueIsInRange){
    return $filter(cellFilter)(input, args1, args2);
}
return '-';
typeof x ==='number' || !!x 

is false when x is null, undefined or empty string

Only one case in which it doesn't work – if you need to filter boolean variables, but your case doesn't seem to need it.

Anyway in that case you can use

typeof x === 'boolean' || typeof x ==='number' || !!x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM