@billdaddy/iskit
v0.1.1
Published
Tiny, type-safe predicates — isString/isNumber/isPlainObject/isEmpty and more, all narrowing TypeScript type guards. Zero dependencies.
Maintainers
Readme
iskit
Tiny, type-safe predicates —
isString,isNumber,isPlainObject,isEmpty, and more, all narrowing TypeScript type guards. Zero dependencies.
A bag of is* checks you write in every project — but done once, correctly, with
real type-guard signatures so each check narrows the type. isNumber rejects
NaN, isPlainObject actually means plain, and isEmpty only calls a value
empty if it's a container. Zero dependencies, tree-shakeable.
import { isString, isDefined, isEmpty } from "@billdaddy/iskit";
function f(x: unknown) {
if (isString(x)) x.toUpperCase(); // x is `string`
}
users.filter(isDefined); // drops null/undefined, type is User[]
if (isEmpty(input)) return; // "", [], {}, Map/Set(0) → trueWhy iskit?
- Everything narrows. Each guard carries a
value is Tsignature — no casts, works inif,filter, ternaries. - Sharper than
typeof.isNumberexcludesNaN;isDaterejects invalid dates;isPlainObjectexcludes arrays,Map/Set, and class instances. - Sensible
isEmpty. Empty means an empty container (string, array, object,Map/Set) or nullish — not every non-collection the way lodash does. - Non-empty narrowing.
isNonEmptyArraynarrows to[T, ...T[]], soarr[0]loses its| undefined. - Tree-shakeable.
sideEffects: false, ESM + CJS, zero dependencies.
Install
npm install @billdaddy/iskit
# or: pnpm add @billdaddy/iskit / yarn add @billdaddy/iskit / bun add @billdaddy/iskitPrimitives
import {
isString, isNumber, isInteger, isBoolean, isBigInt, isSymbol,
isFunction, isNull, isUndefined, isNil, isDefined, isPrimitive,
} from "@billdaddy/iskit";
isNumber(NaN); // false (a pass is always a usable number)
isNumber(42); // true
isNil(null); // true (null | undefined)
isDefined(0); // true (narrows away null | undefined)
isPrimitive([]); // falseObjects
import {
isObject, isArray, isPlainObject, isDate, isRegExp,
isMap, isSet, isError, isPromise,
} from "@billdaddy/iskit";
isPlainObject({}); // true
isPlainObject(new Date()); // false
isPlainObject(new Foo()); // false (class instance)
isDate(new Date("nope")); // false (invalid time)
isPromise({ then() {} }); // true (thenables count)Emptiness
import { isEmpty, isNonEmptyString, isNonEmptyArray } from "@billdaddy/iskit";
isEmpty(""); // true
isEmpty([]); // true
isEmpty({}); // true
isEmpty(new Set()); // true
isEmpty(0); // false ← not a container
isEmpty({ a: 1 }); // false
isNonEmptyString(s); // narrows to string
isNonEmptyArray(xs); // narrows to [T, ...T[]]Pairs well with
| Need | Use |
| --- | --- |
| Assert a condition and narrow / throw | assertkit |
| Deep equality / clone | equalkit |
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
MIT © Tung Tran
