@hgargg-0710/one
Version:
A tiny npm library purposed for providing beautiful solutions to frequent miniature (one-line/one-expression) tasks for various JS datatypes.
80 lines (79 loc) • 2.02 kB
JavaScript
import { not } from "../boolean/boolean.js";
import { isEmpty } from "../string/string.js";
/**
* Returns whether a given `x` is a number primitive
*/
export const isNumber = (x) => typeof x === "number";
/**
* Returns whether `x` is a function
*/
export const isFunction = (x) => typeof x === "function";
/**
* Returns whether `x` is a string primitive
*/
export const isString = (x) => typeof x === "string";
/**
* Returns whether `x` is a boolean primitive
*/
export const isBoolean = (x) => typeof x === "boolean";
/**
* Returns whether `x` is a symbol primitive
*/
export const isSymbol = (x) => typeof x === "symbol";
/**
* Returns whether `x` is an object
*/
export const isObject = (x) => typeof x === "object";
/**
* Returns whether `x` is `null`
*/
export const isNull = (x) => x === null;
/**
* Returns whether `x` is `undefined`
*/
export const isUndefined = (x) => x === undefined;
/**
* Returns whether `x == null`
*/
export const isNullary = (x) => x == null;
/**
* Returns `typeof x`
*/
export const typeOf = (x) => typeof x;
/**
* Returns whether `x` is an `Array`
*/
export const isArray = (x) => x instanceof Array;
/**
* Returns whether `x` is a `Set`
*/
export const isSet = (x) => x instanceof Set;
/**
* Returns whether `x` is a `Map`
*/
export const isMap = (x) => x instanceof Map;
/**
* Returns a bool indicating whether it is possible to call `Number(x)` without:
*
* 1. getting `NaN`
* 2. getting an error (this happens if `x` is a symbol)
* 3. `x` being an empty string
*/
export function isNumberConvertible(x) {
return ((isNumber(x) && !isNaN(x)) ||
(isString(x) && !isNaN(Number(x)) && !isEmpty(x)) ||
isBoolean(x) ||
isNull(x));
}
/**
* Returns whether `x` is a truthy value
*/
export const isTruthy = (x) => !!x;
/**
* Checks whether the given `x` is `Falsy`
*/
export const isFalsy = not;
/**
* Returns either `x` if it's a non-`null` object, or `false`, if it isn't
*/
export const isStruct = (x) => isObject(x) && x;