nc.checker
Type checks and validators. Every isX accepts anything and never throws, and most of them are type guards: inside if (nc.isString(value)), your editor knows value is a string, in JavaScript too.
const { checker } = require("@ix-xs/node-comfort");
import { isArray } from "@ix-xs/node-comfort/checker";These functions are also available at the top level: nc.isArray() is nc.checker.isArray().
if (nc.isEmail(body.email) && nc.isPort(body.port)) connect(body);
nc.assert(user, "User not found");nc.checker.Functions
nc.checker.isArray()
isArray(value: unknown): value is any[]Is it an array?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is any[]
Example
nc.isArray([1, 2]); // true
nc.isArray({ length: 0 }); // falsenc.checker.isNumber()
isNumber(value: unknown): value is numberIs it a number? NaN is rejected, Infinity passes (see isFinite).
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is number
Example
nc.isNumber(42); // true
nc.isNumber(NaN); // false
nc.isNumber("42"); // false, use isNumeric for stringsnc.checker.isFinite()
isFinite(value: unknown): value is numberIs it a finite number? Unlike the global isFinite, strings are rejected.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is number
Example
nc.isFinite(42); // true
nc.isFinite(Infinity); // false
nc.isFinite("42"); // falsenc.checker.isInteger()
isInteger(value: unknown): value is numberIs it an integer?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is number
Example
nc.isInteger(4); // true
nc.isInteger(4.5); // falsenc.checker.isSafeInteger()
isSafeInteger(value: unknown): value is numberIs it an integer JavaScript can represent exactly (up to 2^53 - 1)?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is number
Example
nc.isSafeInteger(2 ** 53 - 1); // true
nc.isSafeInteger(2 ** 53); // falsenc.checker.isFloat()
isFloat(value: unknown): value is numberIs it a finite number with a decimal part?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is number
Example
nc.isFloat(4.2); // true
nc.isFloat(4); // falsenc.checker.isPositive()
isPositive(value: unknown): value is numberIs it a number greater than zero?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is number
Example
nc.isPositive(3); // true
nc.isPositive(0); // falsenc.checker.isNegative()
isNegative(value: unknown): value is numberIs it a number lower than zero?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is number
Example
nc.isNegative(-3); // true
nc.isNegative(0); // falsenc.checker.isBoolean()
isBoolean(value: unknown): value is booleanIs it true or false? Truthy and falsy values don't count.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is boolean
Example
nc.isBoolean(false); // true
nc.isBoolean(0); // falsenc.checker.isString()
isString(value: unknown): value is stringIs it a string?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
Example
nc.isString("hello"); // true
nc.isString(new String("hello")); // falsenc.checker.isSymbol()
isSymbol(value: unknown): value is symbolIs it a symbol?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is symbol
nc.checker.isBigInt()
isBigInt(value: unknown): value is bigintIs it a bigint?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is bigint
Example
nc.isBigInt(10n); // true
nc.isBigInt(10); // falsenc.checker.isUndefined()
isUndefined(value: unknown): value is undefinedIs it undefined?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is undefined
nc.checker.isNull()
isNull(value: unknown): value is nullIs it null?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is null
nc.checker.isNil()
isNil(value: unknown): value is null | undefinedIs it null or undefined?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is null | undefined
Example
nc.isNil(undefined); // true
nc.isNil(0); // falsenc.checker.isDefined()
isDefined<T>(value: T): value is NonNullable<T>Is it anything but null or undefined? Pass it to filter() to drop missing values and keep the right type.
Parameters
| Name | Type |
|---|---|
value | T |
Returns value is NonNullable<T>
Example
const ids = [1, null, 2, undefined].filter(nc.isDefined); // number[]nc.checker.isPrimitive()
isPrimitive(value: unknown): value is PrimitiveIs it a primitive (string, number, boolean, symbol, bigint, null or undefined)?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Primitive
nc.checker.isFunction()
isFunction(value: unknown): value is (...args: any[]) => anyIs it callable? Classes, async and generator functions count.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is (...args: any[]) => any
nc.checker.isAsyncFunction()
isAsyncFunction(value: unknown): value is (...args: any[]) => Promise<any>Was it declared with async? A function that merely returns a promise doesn't count.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is (...args: any[]) => Promise<any>
Example
nc.isAsyncFunction(async () => {}); // true
nc.isAsyncFunction(() => Promise.resolve()); // falsenc.checker.isGeneratorFunction()
isGeneratorFunction(value: unknown): value is (...args: any[]) => Generator<unknown, any, any>Is it a generator function (function*)?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is (...args: any[]) => Generator
nc.checker.isGenerator()
isGenerator(value: unknown): value is Generator<unknown, any, any>Is it a generator object, the result of calling a function*?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Generator
nc.checker.isClass()
isClass(value: unknown): value is new (...args: any[]) => anyIs it a class rather than a plain function?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is new (...args: any[]) => any
Example
nc.isClass(class User {}); // true
nc.isClass(function () {}); // falsenc.checker.isObject()
isObject(value: unknown): value is objectIs it an object, and not null? Arrays, dates and class instances count; use isPlainObject for {} literals only.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is object
nc.checker.isPlainObject()
isPlainObject(value: unknown): value is Record<string, unknown>Is it a plain object, made with {}, new Object() or Object.create(null)? Arrays, dates, maps and class instances are rejected.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Record<string, unknown>
Example
nc.isPlainObject({ a: 1 }); // true
nc.isPlainObject(new Date()); // falsenc.checker.isPromise()
isPromise(value: unknown): value is PromiseLike<any>Can it be awaited like a promise (anything with a then method)?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is PromiseLike<any>
nc.checker.isRegExp()
isRegExp(value: unknown): value is RegExpIs it a regular expression?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is RegExp
nc.checker.isDate()
isDate(value: unknown): value is DateIs it a Date, valid or not? Use isValidDate to reject Invalid Date.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Date
nc.checker.isValidDate()
isValidDate(value: unknown): value is DateIs it a Date holding a real point in time?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Date
Example
nc.isValidDate(new Date()); // true
nc.isValidDate(new Date("nope")); // falsenc.checker.isMap()
isMap(value: unknown): value is Map<unknown, unknown>Is it a Map?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Map<unknown, unknown>
nc.checker.isSet()
isSet(value: unknown): value is Set<unknown>Is it a Set?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Set<unknown>
nc.checker.isWeakMap()
isWeakMap(value: unknown): value is WeakMap<object, unknown>Is it a WeakMap?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is WeakMap<object, unknown>
nc.checker.isWeakSet()
isWeakSet(value: unknown): value is WeakSet<object>Is it a WeakSet?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is WeakSet<object>
nc.checker.isIterable()
isIterable(value: unknown): value is Iterable<unknown>Does it work with for...of? Strings, arrays, maps, sets and generators do.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Iterable<unknown>
nc.checker.isAsyncIterable()
isAsyncIterable(value: unknown): value is AsyncIterable<unknown>Does it work with for await...of, like streams and async generators?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is AsyncIterable<unknown>
nc.checker.isBuffer()
isBuffer(value: unknown): value is Buffer<ArrayBufferLike>Is it a Node.js Buffer?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Buffer
nc.checker.isTypedArray()
isTypedArray(value: unknown): value is AnyTypedArrayIs it a typed array (Uint8Array, Float64Array...)? Buffers count.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is AnyTypedArray
nc.checker.isError()
isError(value: unknown): value is ErrorIs it an Error, including subclasses and errors from other realms?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is Error
nc.checker.isEmpty()
isEmpty(value: unknown): booleanIs it empty? null, undefined, "", [], {} and empty maps and sets are. Numbers, booleans and functions never are.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns boolean
Example
nc.isEmpty({}); // true
nc.isEmpty(0); // false
nc.isEmpty(" "); // false, see isBlanknc.checker.isBlank()
isBlank(value: unknown): booleanIs it null, undefined, or a string with nothing but whitespace?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns boolean
Example
nc.isBlank(" \n"); // true
nc.isBlank(" a "); // falsenc.checker.isArrayOf()
isArrayOf<T>(value: unknown, guard: (item: unknown) => item is T): value is T[]Is it an array where every item passes guard?
Parameters
| Name | Type | Description |
|---|---|---|
value | unknown | |
guard | (item: unknown) => item is T | Checked against every item, like nc.isString. |
Returns value is T[]
Example
if (nc.isArrayOf(input, nc.isString)) input.join(", "); // input: string[]nc.checker.isOneOf()
isOneOf<L extends readonly unknown[]>(value: unknown, allowed: L): value is L[number]Is it one of the allowed values? With a constant list, the value gets the matching literal type.
Parameters
| Name | Type |
|---|---|
value | unknown |
allowed | L |
Returns value is L[number]
Example
const ROLES = ["admin", "user"] as const;
if (nc.isOneOf(input, ROLES)) input; // "admin" | "user"nc.checker.isEmail()
isEmail(value: unknown): value is stringDoes it look like an email address? The rules are practical rather than the full RFC: one @, no stray dots, a real top-level domain. International addresses are accepted.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
Example
nc.isEmail("jose@exemple.fr"); // true
nc.isEmail("john..doe@example.com"); // false
nc.isEmail("john@localhost"); // falsenc.checker.isURL()
isURL(value: unknown, options?: IsURLOptions): value is stringIs it an absolute URL with an allowed protocol (http and https by default)?
Parameters
| Name | Type |
|---|---|
value | unknown |
optionsoptional | IsURLOptions |
Returns value is string
Example
nc.isURL("https://example.com/a?b=1"); // true
nc.isURL("ftp://example.com", { protocols: ["ftp:"] }); // true
nc.isURL("http://localhost:3000", { requireTld: true }); // falsenc.checker.isUUID()
isUUID(value: unknown, options?: IsUUIDOptions): value is stringIs it a UUID?
Parameters
| Name | Type |
|---|---|
value | unknown |
optionsoptional | IsUUIDOptions |
Returns value is string
Example
nc.isUUID(nc.id.uuid()); // true
nc.isUUID(id, { version: 7 }); // only v7nc.checker.isJSON()
isJSON(value: unknown): value is stringIs it a string of valid JSON?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
Example
nc.isJSON('{"a":1}'); // true
nc.isJSON("{a:1}"); // falsenc.checker.isNumeric()
isNumeric(value: unknown): value is string | numberIs it a finite number, or a string that is exactly one (spaces around are fine)?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is number | string
Example
nc.isNumeric("4.2e3"); // true
nc.isNumeric(" 12 "); // true
nc.isNumeric("12px"); // falsenc.checker.isIP()
isIP(value: unknown, version?: 4 | 6): value is stringIs it an IP address?
Parameters
| Name | Type | Description |
|---|---|---|
value | unknown | |
versionoptional | 4|6 | Only accept IPv4 or IPv6. |
Returns value is string
Example
nc.isIP("192.168.0.1"); // true
nc.isIP("::1"); // true
nc.isIP("::1", 4); // falsenc.checker.isIPv4()
isIPv4(value: unknown): value is stringIs it an IPv4 address, like 192.168.0.1?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
nc.checker.isIPv6()
isIPv6(value: unknown): value is stringIs it an IPv6 address, like ::1?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
nc.checker.isPort()
isPort(value: unknown): booleanIs it a valid port (0 to 65535), as a number or a numeric string?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns boolean
Example
nc.isPort("8080"); // true
nc.isPort(70000); // falsenc.checker.isHex()
isHex(value: unknown): value is stringIs it a hexadecimal string? A 0x prefix is allowed.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
nc.checker.isHexColor()
isHexColor(value: unknown): value is stringIs it a CSS hex color (#rgb, #rgba, #rrggbb or #rrggbbaa)?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
Example
nc.isHexColor("#f80"); // true
nc.isHexColor("f80"); // false, the # is requirednc.checker.isBase64()
isBase64(value: unknown, options?: IsBase64Options): value is stringIs it valid Base64?
Parameters
| Name | Type |
|---|---|
value | unknown |
optionsoptional | IsBase64Options |
Returns value is string
Example
nc.isBase64("aGVsbG8="); // true
nc.isBase64("aGVsbG8", { urlSafe: true }); // truenc.checker.isSemver()
isSemver(value: unknown): value is stringIs it a semantic version? A leading v is fine.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
Example
nc.isSemver("v2.0.0-rc.1"); // true
nc.isSemver("1.2"); // falsenc.checker.isISODate()
isISODate(value: unknown): value is stringIs it an ISO 8601 date or date-time, and a real calendar date?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
Example
nc.isISODate("2024-02-29T10:00:00Z"); // true
nc.isISODate("2023-02-29"); // false, not a leap yearnc.checker.isSlug()
isSlug(value: unknown): value is stringIs it a URL slug like my-first-post? See nc.str.slugify() to make one.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
nc.checker.isAlpha()
isAlpha(value: unknown): value is stringIs it made only of letters, in any alphabet?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
Example
nc.isAlpha("Élodie"); // true
nc.isAlpha("abc1"); // falsenc.checker.isAlphanumeric()
isAlphanumeric(value: unknown): value is stringIs it made only of letters and digits, in any alphabet?
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
nc.checker.isCreditCard()
isCreditCard(value: unknown): value is stringCould it be a card number? Checks the length and the Luhn checksum; spaces and dashes are ignored.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
Example
nc.isCreditCard("4242 4242 4242 4242"); // truenc.checker.isJWT()
isJWT(value: unknown): value is stringDoes it look like a JSON Web Token? The signature is not verified; use nc.crypto.verifyJWT() for that.
Parameters
| Name | Type |
|---|---|
value | unknown |
Returns value is string
nc.checker.assert()
assert(condition: unknown, message?: string | (() => string) | undefined): asserts conditionThrows an AssertionError if condition is falsy. Afterwards, TypeScript knows the condition holds.
Parameters
| Name | Type | Description |
|---|---|---|
condition | unknown | |
messageoptional | string | (() => string) | A message, or a function that builds it.Default: "Assertion failed" |
Returns asserts condition
Throws AssertionError
Example
const user = users.find((u) => u.id === id);
nc.assert(user, `User ${id} not found`);
user.name; // no "possibly undefined" herenc.checker.assertType()
assertType<T>(value: unknown, guard: (value: unknown) => value is T, message?: string | (() => string) | undefined): asserts value is TThrows an AssertionError unless value passes guard. Afterwards, value has the guarded type.
Parameters
| Name | Type | Description |
|---|---|---|
value | unknown | |
guard | (value: unknown) => value is T | A check like nc.isString. |
messageoptional | string | (() => string) |
Returns asserts value is T
Throws AssertionError
Example
nc.assertType(config.port, nc.isInteger, "port must be an integer");Types
Import any of them in TypeScript with import type { AnyTypedArray } from "@ix-xs/node-comfort", or in JavaScript with import("@ix-xs/node-comfort").AnyTypedArray.
AnyTypedArray
Any built-in typed array.
type AnyTypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64ArrayIsBase64Options
Options for isBase64().
| Property | Type | Description |
|---|---|---|
urlSafeoptional | boolean | Expect the URL-safe alphabet (- and _), padding optional. |
IsURLOptions
Options for isURL().
| Property | Type | Description |
|---|---|---|
protocolsoptional | string[] | Accepted protocols, colon included. Defaults to ["http:", "https:"]. |
requireTldoptional | boolean | Require a domain like example.com, rejecting localhost and bare IPs. |
IsUUIDOptions
Options for isUUID().
| Property | Type | Description |
|---|---|---|
versionoptional | 4 | 6 | 2 | 1 | 3 | 5 | 7 | 8 | Only accept this version. Otherwise versions 1 to 8 pass, plus the nil and max UUIDs. |
Primitive
Any primitive value.
type Primitive = string | number | boolean | symbol | bigint | null | undefined