Gathering detailed insights and metrics for is-what
Gathering detailed insights and metrics for is-what
Gathering detailed insights and metrics for is-what
Gathering detailed insights and metrics for is-what
JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.
npm install is-what
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
185 Stars
233 Commits
20 Forks
2 Watching
16 Branches
10 Contributors
Updated on 17 Nov 2024
TypeScript (99.27%)
JavaScript (0.73%)
Cumulative downloads
Total Downloads
Last day
-4.4%
1,338,362
Compared to previous day
Last week
1.8%
7,372,158
Compared to previous week
Last month
7.6%
31,134,471
Compared to previous month
Last year
35.7%
309,992,646
Compared to previous year
6
Very simple & small JS type check functions. It's fully TypeScript supported!
npm i is-what
Or for deno available at: "deno.land/x/is_what"
Also check out is-where 🙈
I built is-what because the existing solutions were all too complex or too poorly built.
I was looking for:
{}
or a special object (like a class instance) ‼️And that's exactly what is-what
is! (what a great wordplay 😃)
is-what is really easy to use, and most functions work just like you'd expect.
1// import functions you want to use like so: 2import { isString, isDate, isPlainObject } from 'is-what'
isNumber
and isDate
have special treatment.1// basics 2isBoolean(true) // true 3isBoolean(false) // true 4isUndefined(undefined) // true 5isNull(null) // true 6 7// strings 8isString('') // true 9isEmptyString('') // true 10isFullString('') // false 11 12// numbers 13isNumber(0) // true 14isNumber('0') // false 15isNumber(NaN) // false * 16isPositiveNumber(1) // true 17isNegativeNumber(-1) // true 18// * see below for special NaN use cases! 19 20// arrays 21isArray([]) // true 22isEmptyArray([]) // true 23isFullArray([1]) // true 24 25// objects 26isPlainObject({}) // true * 27isEmptyObject({}) // true 28isFullObject({ a: 1 }) // true 29// * see below for special object (& class instance) use cases! 30 31// functions 32isFunction(function () {}) // true 33isFunction(() => {}) // true 34 35// dates 36isDate(new Date()) // true 37isDate(new Date('invalid date')) // false 38 39// maps & sets 40isMap(new Map()) // true 41isSet(new Set()) // true 42isWeakMap(new WeakMap()) // true 43isWeakSet(new WeakSet()) // true 44 45// others 46isRegExp(/\s/gi) // true 47isSymbol(Symbol()) // true 48isBlob(new Blob()) // true 49isFile(new File([''], '', { type: 'text/html' })) // true 50isError(new Error('')) // true 51isPromise(new Promise((resolve) => {})) // true 52 53// primitives 54isPrimitive('') // true 55// true for any of: boolean, null, undefined, number, string, symbol
isNaN
is a built-in JS Function but it really makes no sense:
1// 1) 2typeof NaN === 'number' // true 3// 🤔 ("not a number" is a "number"...) 4 5// 2) 6isNaN('1') // false 7// 🤔 the string '1' is not-"not a number"... so it's a number?? 8 9// 3) 10isNaN('one') // true 11// 🤔 'one' is NaN but `NaN === 'one'` is false...
With is-what the way we treat NaN makes a little bit more sense:
1import { isNumber, isNaNValue } from 'is-what' 2 3// 1) 4isNumber(NaN) // false! 5// let's not treat NaN as a number 6 7// 2) 8isNaNValue('1') // false 9// if it's not NaN, it's not NaN!! 10 11// 3) 12isNaNValue('one') // false 13// if it's not NaN, it's not NaN!! 14 15isNaNValue(NaN) // true
Checking for a JavaScript object can be really difficult. In JavaScript you can create classes that will behave just like JavaScript objects but might have completely different prototypes. With is-what I went for this classification:
isPlainObject
will only return true
on plain JavaScript objects and not on classes or othersisAnyObject
will be more loose and return true
on regular objects, classes, etc.1// define a plain object
2const plainObject = { hello: 'I am a good old object.' }
3
4// define a special object
5class SpecialObject {
6 constructor(somethingSpecial) {
7 this.speciality = somethingSpecial
8 }
9}
10const specialObject = new SpecialObject('I am a special object! I am a class instance!!!')
11
12// check the plain object
13isPlainObject(plainObject) // returns true
14isAnyObject(plainObject) // returns true
15getType(plainObject) // returns 'Object'
16
17// check the special object
18isPlainObject(specialObject) // returns false !!!!!!!!!
19isAnyObject(specialObject) // returns true
20getType(specialObject) // returns 'Object'
Please note that
isPlainObject
will only returntrue
for normal plain JavaScript objects.
You can check for specific types with getType
and isType
:
1import { getType, isType } from 'is-what' 2 3getType('') // returns 'String' 4// pass a Type as second param: 5isType('', String) // returns true
If you just want to make sure your object inherits from a particular class or
toStringTag
value, you can use isInstanceOf()
like this:
1import { isInstanceOf } from 'is-what' 2 3isInstanceOf(new XMLHttpRequest(), 'EventTarget') 4// returns true 5isInstanceOf(globalThis, ReadableStream) 6// returns false
is-what makes TypeScript know the type during if statements. This means that a check returns the type of the payload for TypeScript users.
1function isNumber(payload: unknown): payload is number { 2 // return boolean 3} 4// As you can see above, all functions return a boolean for JavaScript, but pass the payload type to TypeScript. 5 6// usage example: 7function fn(payload: string | number): number { 8 if (isNumber(payload)) { 9 // ↑ TypeScript already knows payload is a number here! 10 return payload 11 } 12 return 0 13}
isPlainObject
and isAnyObject
with TypeScript will declare the payload to be an object type with any props:
1function isPlainObject(payload: unknown): payload is { [key: string]: unknown } 2function isAnyObject(payload: unknown): payload is { [key: string]: unknown } 3// The reason to return `{[key: string]: unknown}` is to be able to do 4if (isPlainObject(payload) && payload.id) return payload.id 5// if isPlainObject() would return `payload is object` then it would give an error at `payload.id`
If you want more control over what kind of interface/type is casted when checking for objects.
To cast to a specific type while checking for isAnyObject
, can use isObjectLike<T>
:
1import { isObjectLike } from 'is-what' 2 3const payload = { name: 'Mesqueeb' } // current type: `{ name: string }` 4 5// Without casting: 6if (isAnyObject(payload)) { 7 // in here `payload` is casted to: `Record<string | number | symbol, unknown>` 8 // WE LOOSE THE TYPE! 9} 10 11// With casting: 12// you can pass a specific type for TS that will be casted when the function returns 13if (isObjectLike<{ name: string }>(payload)) { 14 // in here `payload` is casted to: `{ name: string }` 15}
Please note: this library will not actually check the shape of the object, you need to do that yourself.
isObjectLike<T>
works like this under the hood:
1function isObjectLike<T extends object>(payload: unknown): payload is T { 2 return isAnyObject(payload) 3}
It's litterally just these functions:
1function getType(payload) { 2 return Object.prototype.toString.call(payload).slice(8, -1) 3} 4function isUndefined(payload) { 5 return getType(payload) === 'Undefined' 6} 7function isString(payload) { 8 return getType(payload) === 'String' 9} 10function isAnyObject(payload) { 11 return getType(payload) === 'Object' 12} 13// etc...
See the full source code here.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
5 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
Found 2/24 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More