Collection · 47 utilities

Validator utilities

Checks that answer yes or no: emptiness, parity, equality and the Gregorian leap-year rules. Each states exactly what it compares.

Check whether all values are equal

Compare every item with the first using Object.is.

function allEqual(values) { return values.every(value => Object.is(value, values[0])); }validator

Check whether an object is empty

Check for own enumerable string keys on a JSON object.

function isEmpty(value) { return Object.keys(value).length === 0; }validator

Check whether an integer is even

Test parity for safe integers, including zero and negative values.

function isEven(value) { return value % 2 === 0; }validator

Check a leap year

Apply the Gregorian divisible-by-4, century and 400-year rules.

function isLeapYear(year) { return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); }validator

Check if a character is a digit

Test that a string is exactly one ASCII digit from 0 to 9.

function isDigit(char) { return /^[0-9]$/.test(char); }validator

Check if a date is a weekend

Return true when a YYYY-MM-DD date falls on a Saturday or Sunday.

function isWeekend(date) { return new Date(date).getUTCDay() % 6 === 0; }validator

Check if a date is between two dates

Test whether a YYYY-MM-DD date falls inside an inclusive range.

function isBetween({ date, min, max }) { const time = Date.parse(date); return time >= Date.par…validator

Check if a date is today

Compare the UTC calendar day of a date-time with the current one.

function isToday({ dateTime, now }) { return new Date(dateTime).toISOString().slice(0, 10) === …validator

Check if a date occurs in the current year

Compare the UTC year of a date-time with the current one.

function isCurrentYear({ dateTime, now }) { return new Date(dateTime).getUTCFullYear() === new …validator

Check if a given integer is a prime number

Trial division up to the square root, without allocating an array.

function isPrime(n) { if (n < 2) return false; for (let i = 2; i * i <= n; i++) if (n % i === 0…validator

Check if a date is a weekday

Return true for a YYYY-MM-DD date from Monday to Friday.

function isWeekday(date) { return new Date(date).getUTCDay() % 6 !== 0; }validator

Check if a number is a power of 2

Use the bit trick n & (n − 1) on a positive integer.

function isPowerOfTwo(n) { return n > 0 && (n & (n - 1)) === 0; }validator

Check if a number is in a given range

Test an inclusive range whose ends may be given in any order, with an optional tolerance.

function inRange({ value, a, b, threshold = 0 }) { return Math.min(a, b) - threshold <= value &…validator

Check if a number is odd

Test that an integer leaves a remainder when divided by 2.

function isOdd(n) { return n % 2 !== 0; }validator

Check if a value is a plain object

Return true for objects whose prototype is Object.prototype or null.

function isPlainObject(value) { return value !== null && typeof value === 'object' && [null, Ob…validator

Check if a value is a string

Use Object.prototype.toString so String objects count too.

function isString(value) { return Object.prototype.toString.call(value) === '[object String]'; …validator

Check if a value is base32 encoded

Test the RFC 4648 base32 alphabet and a length that is a multiple of 8.

function isBase32(value) { return value.length % 8 === 0 && /^[A-Z2-7]+=*$/.test(value); }validator

Check if a value is base58 encoded

Test the Bitcoin base58 alphabet, which leaves out 0, O, I and l.

function isBase58(value) { return /^[A-HJ-NP-Za-km-z1-9]*$/.test(value); }validator

Check if a value is nil

Return true for null and undefined.

function isNil(value) { return value == null; }validator

Check if an array is not empty

Return true for an array with at least one item.

function isNotEmpty(value) { return Array.isArray(value) && value.length > 0; }validator

Validate a Gregorian date

Check that a year, a 1-based month and a day form a real calendar date.

function isValidDate({ year, month, day }) { const leap = year % 4 === 0 && (year % 100 !== 0 |…validator

Check if a value is a number

Accept finite numbers and numeric strings.

function isNumber(value) { return !isNaN(parseFloat(value)) && isFinite(value); }validator

Check if a value is an object

Return true for any non-null object, including arrays.

function isObject(value) { return value !== null && typeof value === 'object'; }validator

Check if a value is base64 encoded

Test standard, padded base64.

function isBase64(value) { return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}…validator

Check if a value is a regular expression

Read the internal tag, which also works for values from another realm.

function isRegExp(value) { return Object.prototype.toString.call(value) === '[object RegExp]'; …validator

Check if an object is a Promise

Detect a thenable: an object or function with a then method.

function isPromise(value) { return value !== null && (typeof value === 'object' || typeof value…validator

Try a function in the local code lab, then read its limits before using it in an application.

Type two or more characters. Try “duplicate”, “webhook” or “retry”.