Comparing version 1.5.1 to 2.0.0
120
index.d.ts
declare namespace is { | ||
export type typeOf = (value: any) => boolean; | ||
export type typeOf = (value: any) => boolean; | ||
// Primitives | ||
export function string(value: any): boolean; | ||
export function number(value: any): boolean; | ||
export function undefined(value: any): boolean; | ||
export function boolean(value: any): boolean; | ||
export function bool(value: any): boolean; | ||
export function nullValue(value: any): boolean; | ||
export function symbol(value: any): boolean; | ||
export function bigint(value: any): boolean; | ||
export function nullOrUndefined(value: any): boolean; | ||
export function primitive(value: any): boolean; | ||
// Primitives | ||
export function string(value: any): boolean; | ||
export function number(value: any): boolean; | ||
export function undefined(value: any): boolean; | ||
export function boolean(value: any): boolean; | ||
export function bool(value: any): boolean; | ||
export function nullValue(value: any): boolean; | ||
export function symbol(value: any): boolean; | ||
export function bigint(value: any): boolean; | ||
export function nullOrUndefined(value: any): boolean; | ||
export function primitive(value: any): boolean; | ||
// Functions | ||
export function func(value: any): boolean; | ||
export function generatorFunction(value: any): boolean; | ||
export function asyncFunction(value: any): boolean; | ||
export function boundFunction(value: any): boolean; | ||
// Functions | ||
export function func(value: any): boolean; | ||
export function generatorFunction(value: any): boolean; | ||
export function asyncFunction(value: any): boolean; | ||
export function boundFunction(value: any): boolean; | ||
// Iterable & Generator | ||
export function iterable(value: any): boolean; | ||
export function asyncIterable(value: any): boolean; | ||
export function generator(value: any): boolean; | ||
// Iterable & Generator | ||
export function iterable(value: any): boolean; | ||
export function asyncIterable(value: any): boolean; | ||
export function generator(value: any): boolean; | ||
// Objects | ||
export function promise(value: any): boolean; | ||
export function classObject(value: any): boolean; | ||
export function array(value: any): boolean; | ||
export function object(value: any): boolean; | ||
export function plainObject(value: any): boolean; | ||
export function set(value: any): boolean; | ||
export function map(value: any): boolean; | ||
export function set(value: any): boolean; | ||
export function weakMap(value: any): boolean; | ||
export function weakSet(value: any): boolean; | ||
export function error(value: any): boolean; | ||
export function date(value: any): boolean; | ||
export function regExp(value: any): boolean; | ||
// Objects | ||
export function promise(value: any): boolean; | ||
export function classObject(value: any): boolean; | ||
export function array(value: any): boolean; | ||
export function object(value: any): boolean; | ||
export function plainObject(value: any): boolean; | ||
export function set(value: any): boolean; | ||
export function map(value: any): boolean; | ||
export function set(value: any): boolean; | ||
export function weakMap(value: any): boolean; | ||
export function weakSet(value: any): boolean; | ||
export function error(value: any): boolean; | ||
export function date(value: any): boolean; | ||
export function regExp(value: any): boolean; | ||
// TypedArray & Buffers | ||
export function typedArray(value: any): boolean; | ||
export function buffer(value: any): boolean; | ||
export function int8Array(value: any): boolean; | ||
export function uint8Array(value: any): boolean; | ||
export function uint8ClampedArray(value: any): boolean; | ||
export function int16Array(value: any): boolean; | ||
export function uint16Array(value: any): boolean; | ||
export function int32Array(value: any): boolean; | ||
export function uint32Array(value: any): boolean; | ||
export function float32Array(value: any): boolean; | ||
export function float64Array(value: any): boolean; | ||
export function arrayBuffer(value: any): boolean; | ||
export function sharedArrayBuffer(value: any): boolean; | ||
export function dataView(value: any): boolean; | ||
// TypedArray & Buffers | ||
export function typedArray(value: any): boolean; | ||
export function buffer(value: any): boolean; | ||
export function int8Array(value: any): boolean; | ||
export function uint8Array(value: any): boolean; | ||
export function uint8ClampedArray(value: any): boolean; | ||
export function int16Array(value: any): boolean; | ||
export function uint16Array(value: any): boolean; | ||
export function int32Array(value: any): boolean; | ||
export function uint32Array(value: any): boolean; | ||
export function float32Array(value: any): boolean; | ||
export function float64Array(value: any): boolean; | ||
export function arrayBuffer(value: any): boolean; | ||
export function sharedArrayBuffer(value: any): boolean; | ||
export function dataView(value: any): boolean; | ||
// Misc | ||
export function falsy(value: any): boolean; | ||
export function truthy(value: any): boolean; | ||
export function nan(value: any): boolean; | ||
export function integer(value: any): boolean; | ||
export function directInstanceOf(instance: any, focus: any): boolean; | ||
export function emptyString(value: string): boolean; | ||
// Misc | ||
export function falsy(value: any): boolean; | ||
export function truthy(value: any): boolean; | ||
export function nan(value: any): boolean; | ||
export function integer(value: any): boolean; | ||
export function directInstanceOf(instance: any, focus: any): boolean; | ||
export function emptyString(value: string): boolean; | ||
export namespace utils { | ||
export function getObjectType(value: any): string; | ||
} | ||
export namespace utils { | ||
export function getObjectType(value: any): string; | ||
} | ||
} | ||
@@ -70,0 +70,0 @@ |
199
index.js
@@ -5,5 +5,5 @@ // Require Internal Dependencies | ||
/** | ||
* @const Primitives | ||
* @desc All JavaScript Primitives | ||
* @type {Set<String>} | ||
* @constant Primitives | ||
* @description All JavaScript Primitives | ||
* @type {Set<string>} | ||
*/ | ||
@@ -13,20 +13,25 @@ const Primitives = new Set(["string", "number", "boolean", "undefined", "symbol", "bigint"]); | ||
/** | ||
* @const TypedArrayTypes | ||
* @desc All JavaScript Typed Array Types | ||
* @type {Set<String>} | ||
* @constant TypedArrayTypes | ||
* @description All JavaScript Typed Array Types | ||
* @type {Set<string>} | ||
*/ | ||
const TypedArrayTypes = new Set([ | ||
"Int8Array", | ||
"Uint8Array", | ||
"Uint8ClampedArray", | ||
"Int16Array", | ||
"Uint16Array", | ||
"Int32Array", | ||
"Uint32Array", | ||
"Float32Array", | ||
"Float64Array" | ||
"Int8Array", | ||
"Uint8Array", | ||
"Uint8ClampedArray", | ||
"Int16Array", | ||
"Uint16Array", | ||
"Int32Array", | ||
"Uint32Array", | ||
"Float32Array", | ||
"Float64Array" | ||
]); | ||
/** | ||
* @function nullOrUndefined | ||
* @param {number} value | ||
* @returns {number} | ||
*/ | ||
function nullOrUndefined(value) { | ||
return value === null || typeof value === "undefined"; | ||
return value === null || typeof value === "undefined"; | ||
} | ||
@@ -36,85 +41,85 @@ | ||
module.exports = { | ||
undefined: isTypeOf("undefined"), | ||
void: isTypeOf("undefined"), | ||
string: isTypeOf("string"), | ||
number(value) { | ||
return !Number.isNaN(value) && isTypeOf("number")(value); | ||
}, | ||
boolean: isTypeOf("boolean"), | ||
bool: isTypeOf("boolean"), | ||
symbol: isTypeOf("symbol"), | ||
bigint: isTypeOf("bigint"), | ||
func: isTypeOf("function"), | ||
nullValue: (value) => value === null, | ||
nullOrUndefined, | ||
array: Array.isArray, | ||
buffer: Buffer.isBuffer, | ||
primitive(value) { | ||
return value === null || Primitives.has(typeof value); | ||
}, | ||
promise: isObjectOfType("Promise"), | ||
generatorFunction: isObjectOfType("GeneratorFunction"), | ||
asyncFunction: isObjectOfType("AsyncFunction"), | ||
boundFunction(value) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
return isTypeOf("function")(value) && !value.hasOwnProperty("prototype"); | ||
}, | ||
regExp: isObjectOfType("RegExp"), | ||
date: isObjectOfType("Date"), | ||
error: isObjectOfType("Error"), | ||
map: isObjectOfType("Map"), | ||
set: isObjectOfType("Set"), | ||
weakMap: isObjectOfType("WeakMap"), | ||
weakSet: isObjectOfType("WeakSet"), | ||
int8Array: isObjectOfType("Int8Array"), | ||
uint8Array: isObjectOfType("Uint8Array"), | ||
uint8ClampedArray: isObjectOfType("uint8ClampedArray"), | ||
int16Array: isObjectOfType("int16Array"), | ||
uint16Array: isObjectOfType("uint16Array"), | ||
int32Array: isObjectOfType("int32Array"), | ||
uint32Array: isObjectOfType("uint32Array"), | ||
float32Array: isObjectOfType("float32Array"), | ||
float64Array: isObjectOfType("float64Array"), | ||
arrayBuffer: isObjectOfType("ArrayBuffer"), | ||
sharedArrayBuffer: isObjectOfType("SharedArrayBuffer"), | ||
dataView: isObjectOfType("DataView"), | ||
nan: (value) => Number.isNaN(value), | ||
integer: (value) => Number.isInteger(value), | ||
truthy: (value) => Boolean(value), | ||
falsy: (value) => !value, | ||
emptyString(value) { | ||
return typeof value === "string" && value === ""; | ||
}, | ||
plainObject(value) { | ||
if (getObjectType(value) !== "Object") { | ||
return false; | ||
} | ||
const prototype = Object.getPrototypeOf(value); | ||
undefined: isTypeOf("undefined"), | ||
void: isTypeOf("undefined"), | ||
string: isTypeOf("string"), | ||
number(value) { | ||
return !Number.isNaN(value) && isTypeOf("number")(value); | ||
}, | ||
boolean: isTypeOf("boolean"), | ||
bool: isTypeOf("boolean"), | ||
symbol: isTypeOf("symbol"), | ||
bigint: isTypeOf("bigint"), | ||
func: isTypeOf("function"), | ||
nullValue: (value) => value === null, | ||
nullOrUndefined, | ||
array: Array.isArray, | ||
buffer: Buffer.isBuffer, | ||
primitive(value) { | ||
return value === null || Primitives.has(typeof value); | ||
}, | ||
promise: isObjectOfType("Promise"), | ||
generatorFunction: isObjectOfType("GeneratorFunction"), | ||
asyncFunction: isObjectOfType("AsyncFunction"), | ||
boundFunction(value) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
return isTypeOf("function")(value) && !value.hasOwnProperty("prototype"); | ||
}, | ||
regExp: isObjectOfType("RegExp"), | ||
date: isObjectOfType("Date"), | ||
error: isObjectOfType("Error"), | ||
map: isObjectOfType("Map"), | ||
set: isObjectOfType("Set"), | ||
weakMap: isObjectOfType("WeakMap"), | ||
weakSet: isObjectOfType("WeakSet"), | ||
int8Array: isObjectOfType("Int8Array"), | ||
uint8Array: isObjectOfType("Uint8Array"), | ||
uint8ClampedArray: isObjectOfType("uint8ClampedArray"), | ||
int16Array: isObjectOfType("int16Array"), | ||
uint16Array: isObjectOfType("uint16Array"), | ||
int32Array: isObjectOfType("int32Array"), | ||
uint32Array: isObjectOfType("uint32Array"), | ||
float32Array: isObjectOfType("float32Array"), | ||
float64Array: isObjectOfType("float64Array"), | ||
arrayBuffer: isObjectOfType("ArrayBuffer"), | ||
sharedArrayBuffer: isObjectOfType("SharedArrayBuffer"), | ||
dataView: isObjectOfType("DataView"), | ||
nan: (value) => Number.isNaN(value), | ||
integer: (value) => Number.isInteger(value), | ||
truthy: (value) => Boolean(value), | ||
falsy: (value) => !value, | ||
emptyString(value) { | ||
return typeof value === "string" && value === ""; | ||
}, | ||
plainObject(value) { | ||
if (getObjectType(value) !== "Object") { | ||
return false; | ||
} | ||
const prototype = Object.getPrototypeOf(value); | ||
return prototype === null || prototype === Object.getPrototypeOf({}); | ||
}, | ||
typedArray(value) { | ||
return TypedArrayTypes.has(getObjectType(value)); | ||
}, | ||
directInstanceOf(instance, focusClass) { | ||
return Object.getPrototypeOf(instance) === focusClass.prototype; | ||
}, | ||
classObject(value) { | ||
return isTypeOf("function")(value) && value.toString().startsWith("class "); | ||
}, | ||
object(value) { | ||
return !nullOrUndefined(value) && (isTypeOf("function")(value) || typeof value === "object"); | ||
}, | ||
iterable(value) { | ||
return !nullOrUndefined(value) && isTypeOf("function")(value[Symbol.iterator]); | ||
}, | ||
asyncIterable(value) { | ||
return !nullOrUndefined(value) && isTypeOf("function")(value[Symbol.asyncIterator]); | ||
}, | ||
generator(value) { | ||
const isFn = isTypeOf("function"); | ||
return prototype === null || prototype === Object.getPrototypeOf({}); | ||
}, | ||
typedArray(value) { | ||
return TypedArrayTypes.has(getObjectType(value)); | ||
}, | ||
directInstanceOf(instance, focusClass) { | ||
return Object.getPrototypeOf(instance) === focusClass.prototype; | ||
}, | ||
classObject(value) { | ||
return isTypeOf("function")(value) && value.toString().startsWith("class "); | ||
}, | ||
object(value) { | ||
return !nullOrUndefined(value) && (isTypeOf("function")(value) || typeof value === "object"); | ||
}, | ||
iterable(value) { | ||
return !nullOrUndefined(value) && isTypeOf("function")(value[Symbol.iterator]); | ||
}, | ||
asyncIterable(value) { | ||
return !nullOrUndefined(value) && isTypeOf("function")(value[Symbol.asyncIterator]); | ||
}, | ||
generator(value) { | ||
const isFn = isTypeOf("function"); | ||
return !nullOrUndefined(value) && isFn(value[Symbol.iterator]) && isFn(value.next) && isFn(value.throw); | ||
}, | ||
utils: { getObjectType } | ||
return !nullOrUndefined(value) && isFn(value[Symbol.iterator]) && isFn(value.next) && isFn(value.throw); | ||
}, | ||
utils: { getObjectType } | ||
}; |
{ | ||
"name": "@slimio/is", | ||
"version": "1.5.1", | ||
"version": "2.0.0", | ||
"description": "SlimIO is (JavaScript Primitives & Objects type checker)", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=16" | ||
}, | ||
@@ -12,21 +12,4 @@ "private": false, | ||
"prepublishOnly": "pkg-ok", | ||
"test": "cross-env psp && ava --verbose", | ||
"doc": "jsdoc -c ./jsdoc.json -r -R ./README.md -P ./package.json --verbose", | ||
"coverage": "nyc npm test", | ||
"report": "nyc report --reporter=html" | ||
"test": "c8 -r html ava --verbose" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-push": "cross-env npm test && eslint index.js src/**", | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" | ||
} | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"per-file": false, | ||
"lines": 98, | ||
"statements": 98, | ||
"functions": 98, | ||
"branches": 90 | ||
}, | ||
"repository": { | ||
@@ -49,2 +32,7 @@ "type": "git", | ||
], | ||
"files": [ | ||
"index.js", | ||
"index.d.ts", | ||
"src" | ||
], | ||
"author": "SlimIO", | ||
@@ -57,16 +45,10 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@commitlint/cli": "^8.0.0", | ||
"@commitlint/config-conventional": "^8.0.0", | ||
"@escommunity/minami": "^1.0.0", | ||
"@slimio/eslint-config": "^2.0.4", | ||
"@slimio/psp": "^0.4.0", | ||
"ava": "^2.1.0", | ||
"cross-env": "^5.2.0", | ||
"eslint": "^5.13.0", | ||
"husky": "^2.4.0", | ||
"jsdoc": "^3.6.2", | ||
"nyc": "^14.1.1", | ||
"@nodesecure/eslint-config": "^1.6.0", | ||
"@types/node": "^18.11.18", | ||
"ava": "^3.8.2", | ||
"c8": "^7.12.0", | ||
"cross-env": "^7.0.2", | ||
"eslint": "^7.0.0", | ||
"pkg-ok": "^2.3.1" | ||
}, | ||
"dependencies": {} | ||
} | ||
} |
# SlimIO IS | ||
 | ||
[](https://github.com/SlimIO/is/commit-activity) | ||
 | ||
 | ||
[](https://snyk.io/test/github/SlimIO/is?targetFile=package.json) | ||
 | ||
[](https://greenkeeper.io/) | ||
[](https://travis-ci.com/SlimIO/is) | ||
 | ||
[](https://github.com/SlimIO/is/commit-activity) | ||
 | ||
 | ||
 | ||
@@ -23,3 +20,3 @@ Node.js JavaScript Type checker (Primitives, Objects, etc..) | ||
## Requirements | ||
- Node.js v10 or higher. | ||
- [Node.js](https://nodejs.org/en/) v16 or higher. | ||
@@ -26,0 +23,0 @@ ## Getting Started |
@@ -5,3 +5,3 @@ // These methods are inspired of the work done on @sindresorhus/is: https://github.com/sindresorhus/is | ||
* @namespace utils | ||
* @desc utils methods | ||
* @description utils methods | ||
*/ | ||
@@ -12,5 +12,5 @@ | ||
* @memberof utils# | ||
* @desc Known the name of a given JavaScript Object | ||
* @description Known the name of a given JavaScript Object | ||
* @param {*} value any Object value | ||
* @returns {String | null} | ||
* @returns {string | null} | ||
* | ||
@@ -23,5 +23,5 @@ * @example | ||
function getObjectType(value) { | ||
// Object.prototype.toString.call will return object like [object Map], [object Set] etc | ||
// Slice from index 8 to value.length - 1 | ||
return Object.prototype.toString.call(value).slice(8, -1); | ||
// Object.prototype.toString.call will return object like [object Map], [object Set] etc | ||
// Slice from index 8 to value.length - 1 | ||
return Object.prototype.toString.call(value).slice(8, -1); | ||
} | ||
@@ -32,4 +32,4 @@ | ||
* @memberof utils# | ||
* @desc Known if a value if equal to the given Primitive type | ||
* @param {!String} type Primitive type | ||
* @description Known if a value if equal to the given Primitive type | ||
* @param {!string} type Primitive type | ||
* @returns {is.typeOf} | ||
@@ -43,4 +43,4 @@ * | ||
function isTypeOf(type) { | ||
// eslint-disable-next-line valid-typeof | ||
return (value) => typeof value === type; | ||
// eslint-disable-next-line valid-typeof | ||
return (value) => typeof value === type; | ||
} | ||
@@ -51,4 +51,4 @@ | ||
* @memberof utils# | ||
* @desc Known if an Object name if equal to the closure Object name | ||
* @param {!String} type JavaScript Object | ||
* @description Known if an Object name if equal to the closure Object name | ||
* @param {!string} type JavaScript Object | ||
* @returns {is.typeOf} | ||
@@ -62,10 +62,10 @@ * | ||
function isObjectOfType(type) { | ||
return (value) => getObjectType(value) === type; | ||
return (value) => getObjectType(value) === type; | ||
} | ||
module.exports = { | ||
getObjectType, | ||
isTypeOf, | ||
isObjectOfType | ||
getObjectType, | ||
isTypeOf, | ||
isObjectOfType | ||
}; | ||
Sorry, the diff of this file is not supported yet
7
238
14644
6
136