Primitives
Six predicates for primitive-type and numeric checks. All are O(1) and run anywhere.
| Predicate | Quick rule |
|---|---|
isString(v) | typeof v === "string" (excludes new String(...)) |
isNumeric(v) | Number-or-numeric-string, regex-based |
isInt(v) | typeof v === "number" AND Number.isInteger(v) |
isFloat(v) | typeof v === "number" AND finite AND stringifies to /^-?\d+\.\d+$/ |
isPrimitive(v) | string | number | boolean | bigint (NOT symbol/null/undefined) |
isScalar(v) | string | number | boolean | bigint | symbol |
isString
isString(""); // trueisString("hello"); // trueisString(`x${1}`); // true
isString(new String("x")); // false (wrapper objects are typeof "object")isString(0); // falseisString(null); // falseFor TS narrowing, wrap it in a typed guard — isString itself is typed as (value: any) => boolean, not value is string:
import { isString } from "@mongez/supportive-is";
function isStr(v: unknown): v is string { return isString(v);}isNumeric
Recognizes numbers AND numeric strings:
isNumeric(0); // trueisNumeric(-1.5); // trueisNumeric("12"); // trueisNumeric("+1"); // trueisNumeric("1.5E-3"); // true
isNumeric(""); // falseisNumeric("1."); // false (trailing dot)isNumeric("12abc"); // falseisNumeric(null); // falseisInt & isFloat
Both require typeof value === "number":
isInt(0); // trueisInt(1); // trueisInt(-1); // trueisInt(1.0); // true (`Number.isInteger(1.0)`)isInt(1e21); // true (still an integer)
isInt(1.5); // falseisInt("2"); // false (data type matters)isInt(NaN); // falseisInt(Infinity); // false
isFloat(1.5); // trueisFloat(0.1); // trueisFloat(-1.5); // true
isFloat(1); // falseisFloat(1.0); // false (no decimal in "1")isFloat("1.5"); // falseisFloat(NaN); // falseisFloat(Infinity); // falseisPrimitive vs isScalar
The only difference is Symbol:
| Value | isPrimitive | isScalar |
|---|---|---|
"x" | true | true |
1, 1.5, 1n | true | true |
true, false | true | true |
Symbol("x") | false | true |
null | false | false |
undefined | false | false |
[], {}, () => {} | false | false |
isPrimitive is named for “what you can use in + / ===”, which excludes Symbol. isScalar is “anything that isn’t an object reference”.
Notes
isPrimitive(Symbol(...))returningfalseis intentional — callisScalarinstead if you want symbols included.isIntnow delegates toNumber.isInteger, andisFloatgates onNumber.isFinite, so both correctly handle signed values and rejectNaN/Infinity.