Created
July 7, 2020 20:20
-
-
Save trafficinc/717d452e39a12f711c618ed659ec0af2 to your computer and use it in GitHub Desktop.
JS Type checking helper functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isDefined(val) { | |
return typeof val !== 'undefined'; | |
} | |
function isUndefined(val) { | |
return typeof val === 'undefined'; | |
} | |
function isNull(val) { | |
return val === null && typeof val === 'object'; | |
} | |
function isNullOrUndefined(val) { | |
return val || isUndefined(val); | |
} | |
function isBoolean(val) { | |
return typeof val === typeof true; | |
} | |
function isTrue(val) { | |
return val === true; | |
} | |
function isFalse(val) { | |
return val === false; | |
} | |
function isTruthy(val) { | |
return !!val; | |
} | |
// or can just use TruthyDeep | |
function isTruthyShallow(obj) { | |
const values = Object.values(obj); | |
const tracker = { | |
count: 0, | |
}; | |
const len = values.length; | |
for (let i = 0; i < len; i += 1) { | |
if (!!values[i] === false) { | |
tracker.count += 1; | |
} | |
} | |
if (tracker.count > 0) { | |
return false; | |
} | |
return true; | |
} | |
// Check deep objects for truthy, {name:"Tim",jobs:[jobOne:12],likes:{like:'yes'}} | |
// in JavaScript anything other than false, 0, '', "", null, undefined and NaN is considered truthy. | |
function isTruthyDeep(obj) { | |
const tracker = { | |
count: 0, | |
}; | |
function deepCheck(obj) { | |
const values = Object.values(obj); | |
const len = values.length; | |
for (let i = 0; i < len; i += 1) { | |
if (isObject(values[i])|| isArray(values[i])) { | |
deepCheck(values[i]); | |
} else if (!!values[i] === false) { | |
tracker.count += 1; | |
} | |
} | |
} | |
deepCheck(obj); | |
if (tracker.count > 0) { | |
return false; | |
} | |
return true; | |
} | |
function isFalsy(val) { | |
return !val; | |
} | |
function isObject(obj) { | |
return ( | |
typeof obj === 'object' && | |
obj === Object(obj) && | |
Object.prototype.toString.call(obj) !== '[object Array]' && | |
Object.prototype.toString.call(obj) !== '[object Date]' | |
); | |
} | |
function isEmptyObject(obj) { | |
return isObject(obj) && Object.keys(obj).length === 0; | |
} | |
function isString(val) { | |
return typeof val === 'string'; | |
} | |
function isEmptyString(val) { | |
return isString(val) && val.length === 0; | |
} | |
function isNumber(val) { | |
return typeof val === 'number' && Number.isFinite(val); | |
} | |
function isArray(val) { | |
return !!Array.isArray(val); | |
} | |
function isEmptyArray(val) { | |
return isArray(val) && val.length === 0; | |
} | |
function isFunction(val) { | |
return typeof val === 'function'; | |
} | |
function isDate(val) { | |
return ( | |
val instanceof Date || | |
Object.prototype.toString.call(val) === '[object Date]' | |
); | |
} | |
function isSymbol(val) { | |
return ( | |
typeof val === 'symbol' || | |
(typeof val === 'object' && | |
Object.prototype.toString.call(val) === '[object Symbol]') | |
); | |
} | |
function safeObject(val) { | |
return val; | |
} | |
function safeObjectOrEmpty(val) { | |
if (val) return val; | |
return {}; | |
} | |
function safeString(val) { | |
if (isString(val)) return val; | |
return ''; | |
} | |
function safeNumber(val) { | |
if (isNumber(val)) return val; | |
return 0; | |
} | |
function safeBoolean(val) { | |
if (isBoolean(val)) return val; | |
return false; | |
} | |
function safeFunction(func) { | |
if (isFunction(func)) return func; | |
return () => {}; | |
} | |
function safeArray(arr) { | |
if (isArray(arr)) return arr; | |
if (!isNullOrUndefined(arr)) return [arr]; | |
return []; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment