@comscen/islib
v1.0.4
Published
A collection of blazing-fast is* utility functions. Because typeof undefined === 'undefined' is SO much typing.
Maintainers
Readme
@comscen/islib
Because
typeof undefined === 'undefined'is SO much typing.
A collection of blazing-fast is* utility functions for JavaScript and TypeScript. Over 80 functions covering numbers, strings, arrays, types, dates, and booleans.
Heavily inspired by the legendary is-even package (which has 900k+ weekly downloads), but with slightly more functions. Just slightly.
Installation
npm install @comscen/islibUsage
import {
isEven,
isPrime,
isPalindrome,
isURL,
isLeapYear,
} from "@comscen/islib";
isEven(42); // true (bitwise AND, obviously)
isPrime(7919); // true (6k±1 optimized)
isPalindrome("racecar"); // true (two-pointer, zero allocations)
isURL("https://example.com"); // true
isLeapYear(2000); // true
isHappy(7); // true (Floyd's cycle detection)
isFibonacci(89); // true (perfect square math trick, O(1))API
Numbers
| Function | Description | Algorithm |
| ------------------------ | ----------------------- | --------------------------------- |
| isEven(n) | n is even | (n & 1) === 0 bitwise AND |
| isOdd(n) | n is odd | (n & 1) !== 0 bitwise AND |
| isPositive(n) | n > 0 | comparison |
| isNegative(n) | n < 0 | comparison |
| isZero(n) | n === 0 | strict equality |
| isPowerOfTwo(n) | n is a power of 2 | (n & (n-1)) === 0 bit trick |
| isInteger(n) | no fractional part | Number.isInteger |
| isFloat(n) | finite non-integer | isFinite && !isInteger |
| isNaN(n) | not a number | n !== n identity trick |
| isInfinity(n) | ±Infinity | equality check |
| isPrime(n) | prime number | 6k±1 trial division |
| isPerfectSquare(n) | perfect square | Math.sqrt floor check |
| isFibonacci(n) | Fibonacci number | 5n²±4 perfect square check (O(1)) |
| isDivisibleBy(n, d) | n % d === 0 | modulo |
| isMultipleOf(n, m) | alias for isDivisibleBy | modulo |
| isWholeNumber(n) | non-negative integer | isInteger && n >= 0 |
| isInRange(n, min, max) | min ≤ n ≤ max | comparison |
| isBetween(n, min, max) | min < n < max | strict comparison |
| isPerfectNumber(n) | sum of divisors = n | trial division |
| isTriangular(n) | triangular number | 8n+1 perfect square check |
| isNumericPalindrome(n) | palindrome number | two-pointer scan |
| isArmstrong(n) | narcissistic number | digit power sum |
| isDigitAnagram(n, m) | same digits | sorted digit comparison |
| isHappy(n) | happy number | Floyd's cycle detection |
Strings
| Function | Description |
| --------------------- | -------------------------------------- |
| isPalindrome(s) | reads same forwards and backwards |
| isAnagram(s1, s2) | same characters, different order |
| isEmail(s) | valid email address |
| isURL(s) | valid http/https/ftp URL |
| isEmpty(s) | length === 0 |
| isBlank(s) | empty or whitespace-only |
| isUpperCase(s) | all uppercase letters |
| isLowerCase(s) | all lowercase letters |
| isAlpha(s) | only a–z, A–Z |
| isAlphanumeric(s) | only a–z, A–Z, 0–9 |
| isHexColor(s) | valid CSS hex color (#RGB or #RRGGBB) |
| isIPv4(s) | valid IPv4 address |
| isIPv6(s) | valid IPv6 address |
| isJSON(s) | valid JSON string |
| isNumericString(s) | coercible to a finite number |
| isTitleCase(s) | Title Case formatting |
| isBase64(s) | valid base64 encoding |
| isUUID(s) | valid UUID v1–v5 |
| isWhitespace(s) | non-empty, only whitespace chars |
| isASCII(s) | only code points 0–127 |
| isSemVer(s) | valid semantic version |
| isStrongPassword(s) | ≥8 chars, upper, lower, digit, special |
| isHexString(s) | valid hexadecimal byte string |
Arrays
| Function | Description |
| ------------------------------- | ---------------------------------------- |
| isArrayEmpty(arr) | arr.length === 0 |
| isSorted(arr) | ascending order |
| isSortedDesc(arr) | descending order |
| isSubset(arr, parent) | all elements exist in parent |
| isUnique(arr) | no duplicate elements |
| isFlat(arr) | no nested arrays |
| isEqualArray(a, b) | same elements in same order (===) |
| isArrayContaining(arr, value) | value exists in array |
| isAll(arr, predicate) | every element satisfies predicate |
| isAny(arr, predicate) | at least one element satisfies predicate |
| isNone(arr, predicate) | no element satisfies predicate |
Types
| Function | Description |
| ---------------------- | -------------------------------------------- |
| isArray(v) | Array.isArray |
| isObject(v) | plain object (not array, not class instance) |
| isFunction(v) | callable value |
| isString(v) | string primitive |
| isNumber(v) | number primitive (excludes NaN) |
| isFiniteNumber(v) | finite number (excludes NaN and ±Infinity) |
| isBoolean(v) | true or false |
| isNull(v) | === null |
| isUndefined(v) | === undefined |
| isNullOrUndefined(v) | null or undefined |
| isSymbol(v) | Symbol |
| isBigInt(v) | BigInt |
| isDate(v) | valid Date instance |
| isRegExp(v) | RegExp |
| isPromise(v) | Promise or thenable |
| isIterable(v) | implements Symbol.iterator |
| isMap(v) | Map instance |
| isSet(v) | Set instance |
| isError(v) | Error instance |
| isClassInstance(v) | non-plain object with prototype |
Dates
| Function | Description |
| --------------------- | -------------------------- |
| isLeapYear(year) | Gregorian leap year |
| isWeekend(date) | Saturday or Sunday |
| isWeekday(date) | Monday through Friday |
| isBefore(d1, d2) | d1 < d2 |
| isAfter(d1, d2) | d1 > d2 |
| isToday(date) | same calendar day as today |
| isValidDate(value) | valid Date object |
| isPast(date) | before now |
| isFuture(date) | after now |
| isSameDay(d1, d2) | same year/month/day |
| isSameMonth(d1, d2) | same year/month |
| isSameYear(d1, d2) | same year |
Booleans
| Function | Description |
| ------------------------ | ---------------------------- |
| isTrue(v) | === true |
| isFalse(v) | === false |
| isTruthy(v) | coerces to true |
| isFalsy(v) | coerces to false |
| isStrictlyTrue(v) | alias for isTrue |
| isStrictlyFalse(v) | alias for isFalse |
| isEitherBoolean(v) | true or false |
| isAllTruthy(...values) | all values are truthy |
| isAnyTruthy(...values) | at least one value is truthy |
Performance
Most functions run in O(1) time with zero heap allocations. Key optimizations:
- Bitwise tricks:
isEven/isOdduse bitwise AND instead of modulo - Power of two:
isPowerOfTwouses the classic(n & (n - 1)) === 0single-instruction trick - NaN detection:
isNaNusesn !== n— the fastest possible NaN check (NaN is the only value not equal to itself) - Fibonacci:
isFibonacciuses the 5n²±4 perfect square theorem — O(1), no sequence generation - Triangular numbers:
isTriangularuses the 8n+1 perfect square theorem — O(1) - Primality:
isPrimeuses 6k±1 trial division, checking ~√n/3 candidates - Happy numbers:
isHappyuses Floyd's two-pointer cycle detection — no growing Sets - Anagram:
isAnagramuses a Map for O(n) character frequency instead of sorting - Subset:
isSubsetbuilds a parent Set for O(m+n) instead of O(m·n) - Palindrome:
isPalindrome/isNumericPalindromeuse two-pointer scan — no string reversal allocation - ASCII:
isASCIIuses a raw charCode loop — no regex engine overhead
Tree-shaking
@comscen/islib ships both CommonJS and ESM builds. Any modern bundler will tree-shake unused functions automatically.
// Only isEven ends up in your bundle
import { isEven } from "@comscen/islib";Zero dependencies
No runtime dependencies. TypeScript types are included.
License
MIT
