ts-istruthy v1.0.2
isTruthy TypeScript version
TL;DR:
npm i ts-istruthy
This function checks if the given data is truthy.
By default: if an object, array, set, or map contains ANY truthy values, it is truthy.
By default: the value 0 is considered as truthy.
Package at: https://www.npmjs.com/package/ts-istruthy
GitHub repository at: https://github.com/AroenvR/ts-istruthy
Supports the following data types:
- boolean
- number
- string
- objects
- arrays
- nested arrays
- sets
- nested sets
- maps
- nested maps
- null
- undefined
- NaN
Disclaimer
This function is primarily designed for use in frontend applications, where it can be utilized to ensure that rendered elements are truthy and prevent potential crashes in the user interface.
While it may have additional potential use cases, this is the main purpose of the function.
Parameters
data
(required): The data, object or array to check.zero
(optional): If this parameter === false, then 0 is also considered falsy.obj
(optional): If this parameter === true, then the function will only allow objects where all values are truthy.
Returns
true
if the data is truthy.false
if the data is falsy.
Usage
import { isTruthy } from 'ts-istruthy';
console.log(isTruthy(true)); // true
console.log(isTruthy(false)); // false
console.log(isTruthy(0)); // true
console.log(isTruthy(0, false)); // false
Check the example test script (using Jest) for detailed examples.
Notes
- The function uses
Object.getOwnPropertyNames(data)
andObject.getPrototypeOf(data)
to check if the given data is an object and if it is empty or not. - The function uses
Array.isArray(data)
to check if the given data is an array. - The function uses
typeof(data)
to check the type of data. - The function uses
for...of
loop to iterate over sets and maps. - If
zero
parameter is set to false, the function considers 0 as falsy. - If
obj
parameter is set to true, the function will only allow objects where all values are truthy.
Currently known constraints
Things the isTruthy function cannot (currently) handle:
Classes
Funnctions
Generators
Enums
Symbols
BigInt
Promises
.. Maybe others I missed?
Possible vulnerability
Currently no circular reference check is implemented.