UNPKG

checkvar

Version:

It checks if a variable is empty or not

102 lines (101 loc) 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CheckType = exports.NOT_EMPTY = exports.EMPTY = void 0; exports.EMPTY = false; exports.NOT_EMPTY = true; var CheckType; (function (CheckType) { CheckType[CheckType["NULL"] = 0] = "NULL"; CheckType[CheckType["NUMBER"] = 1] = "NUMBER"; CheckType[CheckType["BOOLEAN"] = 2] = "BOOLEAN"; CheckType[CheckType["SYMBOL"] = 3] = "SYMBOL"; CheckType[CheckType["STRING"] = 4] = "STRING"; CheckType[CheckType["ARRAY"] = 5] = "ARRAY"; CheckType[CheckType["OBJECT"] = 6] = "OBJECT"; CheckType[CheckType["FUNCTION"] = 7] = "FUNCTION"; CheckType[CheckType["BIGINT"] = 8] = "BIGINT"; CheckType[CheckType["NAN"] = 9] = "NAN"; CheckType[CheckType["UNDEFINED"] = 10] = "UNDEFINED"; })(CheckType = exports.CheckType || (exports.CheckType = {})); function checkvar(el, fullResponse = false) { let response; if (el === null) { response = { type: CheckType.NULL, result: exports.EMPTY, }; } else if (el === undefined) { response = { type: CheckType.UNDEFINED, result: exports.EMPTY, }; } else if (typeof el === 'number') { if (Number.isNaN(el)) { response = { type: CheckType.NAN, result: exports.EMPTY, }; } else { response = { type: CheckType.NUMBER, result: exports.NOT_EMPTY, }; } } else if (typeof el === 'boolean') { response = { type: CheckType.BOOLEAN, result: exports.NOT_EMPTY, }; } else if (typeof el === 'bigint') { response = { type: CheckType.BIGINT, result: exports.NOT_EMPTY, }; } else if (typeof el === 'string') { response = { type: CheckType.STRING, result: el.trim().length !== 0, }; } else if (Object.prototype.toString.call(el).indexOf('Function') > -1) { const theFunction = el; const m = theFunction.toString().match(/\{([\s\S]*)\}/m)[1]; const body = m .replace(/^\s*\/\/.*$/mg, '') .replace(/\/\*([\s\S]*?)\*\//g, '') .trim(); response = { type: CheckType.FUNCTION, result: body.length !== 0, }; } else if (typeof el === 'symbol') { response = { type: CheckType.SYMBOL, result: exports.NOT_EMPTY, }; } else if (typeof el === 'object') { if (Array.isArray(el)) { response = { type: CheckType.ARRAY, result: el.length !== 0, }; } else { const json = JSON.stringify(el); response = { type: CheckType.OBJECT, result: json !== '{}' && json !== '""', }; } } return fullResponse === true ? response : response.result; } exports.default = checkvar;