Language/Java Script

JS/is empty 처리 및 nvl2 정리

건담아빠 2021. 3. 19. 11:45

 

script로 null 체크 후 오라클 대체 텍스트를 넣는 부분들이 매번 생각하고 처리하는게 귀찮아서 만들어 두고 쓰던거 정리해 둔다.

 

유틸

var CORE = CORE || {};

/**
 * NULL 이면 대체 Text
 *
 * @param sVal
 * @param sReplaceVal
 * @returns {*}
 */
CORE.nvl2 = function (sVal, sReplaceVal) {
    if (CORE.isEmpty(sVal) === true) {
        return sReplaceVal;
    } else {
        return sVal;
    }
};

/**
 * Empty 여부 체크
 *
 * @param sVal
 * @returns {boolean}
 */
CORE.isEmpty = function (sVal) {
    if (sVal === true || sVal === false) {
        return false;
    }

    if (sVal != null && sVal != "" && (typeof sVal != "undefined")) {
        if ((typeof sVal.valueOf() == "string" && sVal.length > 0) || (typeof sVal.valueOf() == "number") || (typeof sVal.valueOf() == 'object') || (typeof sVal == 'function')) {
            return false;
        } else {
            return true;
        }
    } else {
        if ((sVal == null || sVal == '' || (typeof sVal == "undefined"))) {
            return true;
        } else {
            return false;
        }
    }
};

 

사용방법

if (CORE.isEmpty(sVal) === true) {
    console.log('is empty');
} else {
    console.log('is not empty');
}

var sText = CORE.nvl2('', '대체 값');
console.log(sText);