Package Exports
- @phun-ky/typeof
- @phun-ky/typeof/dist/typeof.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@phun-ky/typeof) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@phun-ky/typeof
About
A set of JavaScript helper functions to check for types.
Table of Contents
Installation
npm i --save @phun-ky/typeofUsage
Either import and run the required functions:
import { isString } from '@phun-ky/typeof';
isString('asd'); // true;API
isBoolean()
function isBoolean(variable): boolean;Defined in: main.ts:42
Checks if the given variable is a boolean.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable |
unknown |
The variable to check. |
Returns
boolean
True if the variable is a boolean, false otherwise.
isBuiltInConstructor()
function isBuiltInConstructor(value): boolean;Defined in: main.ts:232
Checks if a given value is a built-in JavaScript constructor.
This function verifies whether the provided value is a function and matches
one of JavaScript's built-in constructors, such as Object, Array, Function, etc.
Parameters
| Parameter | Type | Description |
|---|---|---|
value |
unknown |
The value to check. |
Returns
boolean
true if the value is a built-in constructor, otherwise false.
Example
console.log(isBuiltInConstructor(Object)); // Output: true
console.log(isBuiltInConstructor(Array)); // Output: true
console.log(isBuiltInConstructor(class MyClass {})); // Output: false
console.log(isBuiltInConstructor(() => {})); // Output: false
console.log(isBuiltInConstructor(123)); // Output: falseisClass()
function isClass(value): boolean;Defined in: main.ts:199
Checks if a given value is a class constructor.
This function determines whether the provided value is a class by verifying if it is a function and checking its prototype descriptor. Class constructors always have a non-writeable prototype, while regular functions do not.
Will always return false on built in constructors like Date or Array.
Parameters
| Parameter | Type | Description |
|---|---|---|
value |
unknown |
The value to check. |
Returns
boolean
true if the value is a class constructor, otherwise false.
Example
class MyClass {}
console.log(isClass(MyClass)); // Output: true
function regularFunction() {}
console.log(isClass(regularFunction)); // Output: false
console.log(isClass(() => {})); // Output: false
console.log(isClass(null)); // Output: falseisInstanceOfUnknownClass()
function isInstanceOfUnknownClass(value): boolean;Defined in: main.ts:283
Checks if a given value is an instance of a non-standard (unknown) class.
This function determines whether the provided value is an object and has a prototype
that is neither Object.prototype (standard object) nor null (no prototype).
It helps differentiate between instances of custom classes and plain objects.
Parameters
| Parameter | Type | Description |
|---|---|---|
value |
unknown |
The value to check. |
Returns
boolean
true if the value is an instance of a non-standard class, otherwise false.
Example
class MyClass {}
console.log(isInstanceOfUnknownClass(new MyClass())); // Output: true
console.log(isInstanceOfUnknownClass({})); // Output: false
console.log(isInstanceOfUnknownClass(Object.create(null))); // Output: false
console.log(isInstanceOfUnknownClass([])); // Output: trueisNotBoolean()
function isNotBoolean(variable): boolean;Defined in: main.ts:51
Checks if the given variable is not a boolean.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable |
unknown |
The variable to check. |
Returns
boolean
True if the variable is not a boolean, false otherwise.
isNotNumber()
function isNotNumber(variable): boolean;Defined in: main.ts:34
Checks if the given variable is not a number.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable |
unknown |
The variable to check. |
Returns
boolean
True if the variable is not a number, false otherwise.
isNotString()
function isNotString(variable): boolean;Defined in: main.ts:17
Checks if the given variable is not a string.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable |
unknown |
The variable to check. |
Returns
boolean
True if the variable is not a string, false otherwise.
isNotUndefined()
function isNotUndefined(variable): boolean;Defined in: main.ts:69
Checks if the given variable is not undefined.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable |
unknown |
The variable to check. |
Returns
boolean
True if the variable is not undefined, false otherwise.
isNumber()
function isNumber(variable): boolean;Defined in: main.ts:25
Checks if the given variable is a number.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable |
unknown |
The variable to check. |
Returns
boolean
True if the variable is a number, false otherwise.
isObjectLoose()
function isObjectLoose(value): boolean;Defined in: main.ts:169
Checks if a given value is an object or a function.
This function verifies whether the provided value is of type 'object' or 'function'
while ensuring that null is excluded.
Parameters
| Parameter | Type | Description |
|---|---|---|
value |
unknown |
The value to check. |
Returns
boolean
true if the value is an object or function, otherwise false.
Example
console.log(isObjectLoose({})); // Output: true
console.log(isObjectLoose([])); // Output: true
console.log(isObjectLoose(() => {})); // Output: true
console.log(isObjectLoose(null)); // Output: false
console.log(isObjectLoose(42)); // Output: falseFeatures
- ✅ Recognizes all objects (plain objects, arrays, functions, dates, etc.).
- ✅ Recognizes functions as objects (since functions are technically objects in JavaScript).
- ❌ Does not differentiate between plain objects and special objects (like arrays, functions, DOM nodes, etc.).
Behaviour
- ✅
isObjectLoose({})→true - ✅
isObjectLoose([])→true - ✅
isObjectLoose(() => {})→true - ❌
isObjectLoose(null)→false
When to use
- Use
isObjectStrictwhen you need a strict check for plain objects. - Use
isObjectLooseif you need to check if a value is an object-like structure, including functions.
Comparison
| Feature | Strict Check (isObjectStrict) |
Loose Check (isObjectLoose) |
|---|---|---|
| Recognizes plain objects | ✅ Yes | ✅ Yes |
| Recognizes functions | ❌ No | ✅ Yes |
| Recognizes arrays | ❌ No | ✅ Yes |
Recognizes Object.create(null) objects |
✅ Yes | ✅ Yes |
| Recognizes class instances | ❌ No | ✅ Yes |
| Recognizes DOM elements | ❌ No | ✅ Yes |
| Complexity | 🔴 High | 🟢 Low |
isObjectStrict()
function isObjectStrict(value): boolean;Defined in: main.ts:105
Checks if a given value is a plain object.
A plain object is an object created by the {} syntax, Object.create(null),
or using new Object(). This function ensures that the value is an object
and does not have an unusual prototype chain.
Parameters
| Parameter | Type | Description |
|---|---|---|
value |
unknown |
The value to check. |
Returns
boolean
true if the value is a plain object, otherwise false.
Example
console.log(isObjectStrict({})); // Output: true
console.log(isObjectStrict(Object.create(null))); // Output: true
console.log(isObjectStrict([])); // Output: false
console.log(isObjectStrict(new Date())); // Output: false
console.log(isObjectStrict(null)); // Output: falseFeatures
- ✅ Recognizes only plain objects (created via
{},new Object(),Object.create(null), etc.). - ❌ Rejects arrays, functions, DOM elements, class instances, and custom objects with modified constructors.
Behaviour
- ✅
isObjectStrict({})→true - ❌
isObjectStrict([])→false - ❌
isObjectStrict(() => {})→false - ✅
isObjectStrict(Object.create(null))→true
When to use
- Use
isObjectStrictwhen you need a strict check for plain objects. - Use
isObjectLooseif you need to check if a value is an object-like structure, including functions.
isString()
function isString(variable): boolean;Defined in: main.ts:8
Checks if the given variable is a string.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable |
unknown |
The variable to check. |
Returns
boolean
True if the variable is a string, false otherwise.
isUndefined()
function isUndefined(variable): boolean;Defined in: main.ts:60
Checks if the given variable is undefined.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable |
unknown |
The variable to check. |
Returns
boolean
True if the variable is undefined, false otherwise.
Development
// Build
$ npm run build
// Run dev
$ npm run dev
// Test
$ npm testContributing
Want to contribute? Please read the CONTRIBUTING.md and CODE_OF_CONDUCT.md
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See the CHANGELOG.md for details on the latest updates.
Sponsor me
I'm an Open Source evangelist, creating stuff that does not exist yet to help get rid of secondary activities and to enhance systems already in place, be it documentation or web sites.
The sponsorship is an unique opportunity to alleviate more hours for me to maintain my projects, create new ones and contribute to the large community we're all part of :)
Support me on GitHub Sponsors.
p.s. Ukraine is still under brutal Russian invasion. A lot of Ukrainian people are hurt, without shelter and need help. You can help in various ways, for instance, directly helping refugees, spreading awareness, putting pressure on your local government or companies. You can also support Ukraine by donating e.g. to Red Cross, Ukraine humanitarian organisation or donate Ambulances for Ukraine.