A simple set of functions that return a boolean.
- Supports TypeScript!
- Supports Node and browser
- Includes full JSDoc documentation
- Very lightweight!
npm install simple-bool --save
Import the script:
<script src="https://joker876.github.io/simple-bool/simple-bool.min.js">
And import the functions from a global object:
SimpleBool.isDefined();
// or
const { isDefined, isNumber, ... } = SimpleBool;
import * from 'simple-bool';
// or
import { /* function names here */ } from 'simple-bool';
isDefined(value: any): boolean
Returns false
if the value is undefined
or null
. Otherwise returns true
.
isNull(value: any): boolean
Returns true
if the value is null
. Otherwise returns false
.
isBoolean(value: any): boolean
Returns true
if the value is true
or false
. Otherwise returns false
.
isAnyString(value: any): boolean
Returns true
if the value is of type string
. Otherwise returns false
.
isString(value: any): boolean
Returns true
if the value is of type string
, and is not an empty string. Otherwise returns false
.
isNumber(value: any): boolean
Returns true
if the value is of type number
, and is not a NaN
. Otherwise returns false
.
isInt(value: any): boolean
Returns true
if the value is a number, and it doesn't have any decimal places. Otherwise returns false
.
isFloat(value: any): boolean
Returns true
if the value is a number, and it does have some decimal places. Otherwise returns false
.
isObject(value: any): boolean
Returns true
if the value is of type object
, and is defined. Otherwise returns false
.
isArray(value: any): boolean
Returns true
if the value is an array. Otherwise returns false
.
isEmpty(value: object | string): boolean
Returns true
if:
- the value is a string, and its length is greater than 0,
- the value is an array, and it has at least 1 item,
- the value is an object, and it has at least 1 key.
Otherwise returns false
.
isClassDeclaration(value: any): boolean
Returns true
if the value is a class declaration. Otherwise returns false
. All native classes will return false
.
Example:
class Example {
constructor() {}
}
isClassDeclaration(Example); // -> true
isClassDeclaration(RegExp); // -> false
isInstanceOf(value: any, cls: Function): boolean
Returns true
if the value is an instance of the class cls. Otherwise returns false
.
isPromise(value: any): boolean
Returns true
if the value is a Promise. Otherwise returns false
.
isFunction(value: any): boolean
Returns true
if the value is an instance of Function. Otherwise returns false
.
All standard functions, arrow functions, classes, constructors, etc. count towards being a Function.
isRegExp(value: any): boolean
Returns true
if the value is a regular expression. Otherwise returns false
.
isDate(value: any): boolean
Returns true
if the value is is an instance of Date, or can be parsed into a valid Date. Otherwise returns false
.
All numbers return true
when passed into isDate
.
hasProp(value: any, property: PropertyKey): boolean
Returns true
if the value is an object which has a certain property property. Otherwise returns false
.
evaluate(value: any): boolean
Returns true
if:
- the value is equal to
true
, - the value is a number, and is not 0,
- the value is an array, and it has at least 1 item,
- the value is an object, and it has at least 1 key,
- the value is any of those strings: (case insensitive)
'yes', 'y', '1', 't', 'true', 'on', 'sure'
Otherwise returns Boolean(value)
.
all<T>(array: T[], fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it returns true
only if fn returned true
for all items. Otherwise returns false
.
most<T>(array: T[], fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it returns true
only if fn returned true
for at least 50% of all items. Otherwise returns false
.
any<T>(array: T[], fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it returns true
if fn returned true
for at least 1 item. Otherwise returns false
.
none<T>(array: T[], fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it returns true
only if fn returned false
for all items. Otherwise returns false
.
some<T>(array: T[], threshold: number, fn: (value: T) => boolean = Boolean): boolean
Firstly, it flattens the given array.
For each item in array, it calls fn and passes the item.
It counts the times fn returns either true
or false
.
At the end, it compares these amounts to the threshold:
- if the threshold is less than or equal to 0, it always returns
true
, - if the threshold is between 0 and 1 (non-inclusive), it is treated as a percetage, and it returns
true
only if fn returnedtrue
at least that many percent of all executions, - if the threshold is greater than or equal to 1, it returns
true
only if fn returnedtrue
at least that many times.
In all other cases, it returns false
.