1.0.0
is.js
(any)
something to check
is
:
is instance
Extend is.prototype
(PluginFunctionMap)
plugins object
is.extend({
uppercase: function () {
return this.actual.split('').every(x => x.match(/[A-Z]/));
}
});
is('WORD').uppercase() // true
is('Word').uppercase() // false
is('Word').not.uppercase() // true
Validates with type predicate if object
is array
(any)
The object to check
const x: unknown = ...;
if (is.array(x)) {
// x here is any[]
}
Get the string representation of object
and optionaly compare it against expected
(any)
The object to check
Representation
:
boolean
:
is.representation(null) // '[object Null]'
is.representation(true, '[object Boolean]') // true
(any)
(any)
How it works?
If only
object
parameter is provided, return it's specific type
is.type([] | "[]"); // 'array'
is.type({} | "{}"); // 'object'
is.type(1 | "2"); // 'number'
is.type(true | "false"); // 'boolean'
is.type("something"); // 'string'
is.type(/[0-9]/g); // 'regexp'
If
object
andexpected
are provides, returns boolean with a type predicate
const arg = process.argv[2];
if ( is.type(arg, 'array') ) {
// true and arg is any[]
}
if ( is.type(arg, 'symbol') ) {
// true and arg is symbol
}
But wait, actually ( "{}", "[]", "2" ) are strings
const arg = '{a: 1}'; // 'string'
// If `object` is of type 'string'
// with `expected` set to 'string'
// returns true, regardless it's content
is.type(arg, 'string') // true, with 'string' type predicate
// via `is.prototype.str`
is(arg).string() // true, but no type predicate
What the specific type is
For cli inputs and types wrapped in a string, try to extract and define it's type
"unknown"
)(any)
The object whose type to be checked
(Expected)
The
object
expected type
(Specific | "unknown"
)
:
(any)
(any)
Check with type predicate if object
is typeof expected
(any)
The object whose type to be checked
(Expected)
The
object
expected type
is.typeOf(['a'], 'object') // true
is.typeOf({a: 0}, 'object') // true
is.typeOf(null, 'object') // true
is.typeOf('xyz', 'string') // true
is.typeOf(0, 'number') // true
is.typeOf(false, 'boolean') // true
The actual
property is the parameter passed to is
The not
property has the negated version of is.prototype
Type:
Omit<is, ("not"
| "actual"
)>
const x = '';
is(x).string() // true
is(x).not.string() // false
is(x).not.string({empty: false}) // true
Checks if is.prototype.actual
is array,
with optional behavior for string array
boolean
:
is(['A']).array() // true
is('[1]').array({string: true}) // true
Checks if is.prototype.actual
is boolean (true | false)
with optional behavior for string boolean
boolean
:
is(true).boolean() // true
is('false').boolean({string: true}) // true
Determines if is.prototype.actual
in object
or array
,
with optional strict check for ownProperties
(any)
Object to search in
(object?
= {mode:'all'}
)
Optional configration object
Name | Description |
---|---|
options.mode (
|
If 'own' check only the object direct properties (ownProperties) not inherited, defaults to 'all' |
boolean
:
const a = {one: 1};
const b = Object.create(a);
b.tow = 2;
is('a').in([1, 'a', true]) // true
is('one').in(b) // true
is('tow').in(b) // true
is('one').in(b, {mode: 'own'}) // false
is('tow').in(b, {mode: 'own'}) // true
Checks if is.prototype.actual
is NaN, by default coerces the input to a number first
with optional behavior for skips coercion (exact NaN check)
boolean
:
is('a').nan() // true
is(NaN).nan({strict: true}) // true
is('a').nan({strict: true}) // false
Checks if is.prototype.actual
is null,
with optional behavior for string null
boolean
:
is(null).null() // true
is('null').null({string: true}) // true
Checks if is.prototype.actual
is number,
with optional behavior for string number
boolean
:
is(1).number() // true
is('2').number({string: true}) // true
Checks if is.prototype.actual
is object,
with optional behavior for string object
boolean
:
is({a: 1}).object() // true
is('{a: 1}').object({string: true}) // true
is('{"a": "1"}').object({string: true}) // true
Returns either the string representation of is.prototype.actual
(if expected is omitted)
or a boolean value indicating whether the string representations of is.prototype.actual
and expected
are equal
Representation
:
boolean
:
is('abc').representation() // '[object String]'
is(null).representation("[object Null]") // true
is([12]).representation("[object Null]") // false
Checks if is.prototype.actual
is string,
with optional behavior for empty strings
boolean
:
is('value').string() // true
is('').string({empty: false}) // false
Type: Function
(any?)
Optional options parameter
(any?)
Optional config parameter
boolean
:
Type: Object<string, ExtendCallBack>
Type:
("[object Type]"
)
Type:
("string"
| "number"
| "boolean"
| "array"
| "object"
| "function"
| "symbol"
| "null"
| "undefined"
| "regexp"
)
Type:
("bigint"
| "boolean"
| "function"
| "number"
| "object"
| "string"
| "symbol"
| "undefined"
)