Skip to content

tiaanduplessis/typefence

Repository files navigation

🤺 typefence

package version package downloads standard-readme compliant package license make a pull request

Basic runtime type checking

Table of Contents

Install

Install the package locally within you project folder with your package manager:

With npm:

npm install typefence

With yarn:

yarn add typefence

With pnpm:

pnpm add typefence

Usage

Kitchen sink

import {
  isArr,
  isArray,
  isBool,
  isBoolean,
  isDate,
  isEmpty,
  isErr,
  isEvery,
  isFunc,
  isFunction,
  isJSON,
  isNotStr,
  isNull,
  isNullish,
  isNum,
  isObj,
  isObject,
  isProm,
  isPromise,
  isSome,
  isStr,
  isString,
  isSym,
  isUndefined,
  isPrimitive,
} from "typefence";

isArr([]); // true
isArray(new Array(10)); // true

isBool(true); // true
isBoolean(false); // true
isBool(new Boolean()); // true

isDate(new Date()); // true

isEmpty([]); // true
isEmpty(""); // true
isEmpty({}); // true

isErr(new Error("Error")); // true

isJSON('{"foo": true}'); // true

isFunc(() => {}); // true
isFunction(function noop() {}); // true

isStr(""); // true
isString("true"); // true
isNotStr(false); // true

isSym(Symbol("test")); // true

isObj({}); // true
isObject({ foo: true }); // true

isProm(Promise.resolve({})); // true
isPromise(new Promise(() => {})); // true

isNull(null); // true
isUndefined(undefined); // true
isNullish(null); // true

const isEmptyObject = isEvery(isObj, isEmpty);
isEmptyObject({}); // true

isPrimitive("str"); // true

Negation

Most functions have a negated alternative for convenience:

import { isNotNullish } from "typefence";
isNotNullish({}); // true

API

For all configuration options, please see the API docs.

Contributing

Got an idea for a new feature? Found a bug? Contributions are welcome! Please open up an issue or make a pull request.

License

MIT © Tiaan du Plessis