@chriscodesthings/is
v0.3.0
Published
Type checking utilities
Downloads
204
Maintainers
Readme
is

Type checking utilities
Description
A collection of lightweight type checking and validation functions.
See...
Install from NPM
npm install --save @chriscodesthings/isUsage
Node / CDN
// Node
import * as is from '@chriscodesthings/is';
// ... or ... //
// jsDelivr
import * as is from 'https://cdn.jsdelivr.net/npm/@chriscodesthings/is';
// ... or ... //
// Unpkg
import * as is from 'https://unpkg.com/@chriscodesthings/is';
is.isStr("hello world!"); // trueReference
Numbers
| Function | Description |
| :--- | :--- |
| is.isNum(x) | Checks if a value is a finite number. |
| is.isInt(x) | Checks if a value is an integer. |
| is.isPct(x) | Checks if a string is a percentage. |
Strings
| Function | Description |
| :--- | :--- |
| is.isStr(x) | Checks if a value is a string. |
| is.isWord(str, [options]) | Checks if a string is a "word" consisting of allowed characters. |
| is.isBalancedChr(str,c1,c2) | Checks if a string is balanced, e.g. ( and ) are properly matched. Single character version. |
Arrays
| Function | Description |
| :--- | :--- |
| is.isArr(x) | Checks if a value is an array. |
Functions
Number Functions
is.isNum
Checks if a variable is a finite number.
is.isNum(42); // true
is.isNum(NaN); // falseis.isInt
Checks if a variable is a mathematical integer.
is.isInt(42); // true
is.isInt(42.5); // falseis.isPct
Checks if a string is a percentage.
is.isPct("25%"); // true
is.isPct("15"); // false
is.isPct(7); // falseString Functions
is.isStr
Checks if a variable is a string or a String object.
is.isStr('hello'); // true
is.isStr(123); // falseis.isWord
Checks if a string consists only of alphabetical characters by default. Use options to permit hyphens, underscores, or numbers.
is.isWord('hello'); // true
is.isWord('hello-world'); // false
is.isWord('hello-world', { hyphen: true }); // true
is.isWord('user_123', { underscore: true, numbers: true }); // trueis.isBalancedChr
Checks if a string has balanced characters (e.g., brackets). This function uses a depth counter to ensure every opening character has a corresponding closing character in the correct order.
isBalancedChr("(a + b)", "(", ")"); // true
isBalancedChr(")a + b)", "(", ")"); // false (close bracket first)
isBalancedChr("((a + b)", "(", ")"); // false (unclosed bracket)
isBalancedChr("<div></div>", "<", ">"); // trueNote: chr1 and chr2 must be single characters. For validating balanced multi-character sequences, use isBalancedStr.
Array Functions
is.isArr
Determines if a variable is an Array.
is.isArr([1, 2, 3]); // true
is.isArr({}); // falseSee Also...
- parse-number — A numeric sanitizer that fixes Number() edge cases, and allows for percentage
- color-utils — A collection of lightweight utilities for color identification, conversion, and simple manipulation
License
MIT © ChrisCodesThings
