Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
A proper typeof that works with primitives, built-in value objects and those from other execution contexts.
Kindof.js provides a single kindof
function that does what you'd expect
from typeof
— gives you the proper semantic type regardless if the variable
was a primitive ("Hello"
), a built-in value object
like (new Date(2000, 5, 18)
or /.*/
) or came from another execution
context (e.g. an array from another <iframe>
).
When and why should you use kindof
over typeof
?
"null"
given the null
value.typeof
says null
is an object.{name: "John"}
) and built-in value objects (new Date(2000, 5, 18)
).kindof(obj) == "date"
check makes that easy.<frame>
.obj instanceof Date
safely.Boolean
, Number
and String
) to of the same type as their primitive counterparts. See below
for why boxed objects are very error prone and should be avoided.Kindof.js supports all ECMAScript built-in types and primitives:
undefined
, null
, Boolean
, Number
, String
, Symbol
, RegExp
, Date
,
Array
, Function
and plain old Object
. Others, e.g. Math
and JSON
, are
considered just objects. In general, objects that behave like value objects
(dates, regular expressions etc.) or proper arrays have a kind other than
object
.
Please see the table below for the full list of kinds.
You might know, JavaScript has both primitive types and boxed object types for
booleans, numbers and strings. Primitives are what you get from code literals
(true
, 42
, "Hello"
) and from JSON.parse
. Boxed objects tend to only
appear when someone explicitly calls their constructor (new Boolean(false)
).
Boxed objects wouldn't be so bad, except JavaScript's equivalence operator
(==
), for all its type coercions, doesn't handle them transparently. While you
can't compare other value types like dates and regular expressions with ==
either, you won't make that mistake that easily. The following is a small
example of problems with boxed objects:
new String("a") == new String("a") // => false
new Boolean(true) == new Boolean(true) // => false
Boolean(new Boolean(false)) // => true
!!(new Boolean(false)) // => true
If you still wish Kindof to consider boxed Boolean, Number and String types like
primitives (returning "boolean"
, "number"
and "string"
respectively), feel
free to use Kindof.js's v1 branch with npm install kindof@1
.
Note: Kindof.js follows semantic versioning.
Take the kindof.js
file and source it at will.
Install with npm install kindof
.
And require with var kindof = require("kindof")
.
Pass any object to kindof
and compare its output to what you expect:
kindof("Hello") // => "string"
kindof(new Date(2000, 5, 18)) // => "date"
A switch statement might help:
switch (kindof(obj)) {
case "null": this.name = "Alfred"; break
case "string": this.name = obj; break
case "date": this.birthdate = obj; break
default: throw new TypeError("Pardon, sir, came upon an unexpected type.")
}
The pattern is simple and follows typeof
: besides primitives, built-in objects
that are value objects (dates, regular expressions etc.) or real arrays
are of a kind other than object
. The arguments
object, for example, is not
a proper array and is therefore an object
.
Value | Kindof |
---|---|
undefined | undefined |
null | null |
true | boolean |
false | boolean |
42 | number |
NaN | number |
Infinity | number |
"Hello" | string |
Symbol() | symbol |
Symbol("forEach") | symbol |
Symbol.iterator | symbol |
/.*/ | regexp |
new RegExp(".*") | regexp |
new Date | date |
[42, 69] | array |
function() {} | function |
{} | object |
arguments | object |
new Boolean(true) | object |
new Number(42) | object |
new String("Hello") | object |
new MyClass | object |
new Error | object |
Math | object |
JSON | object |
Subclassed objects, such as subclassed arrays, are considered to be object
unless their internal [[Class]]
property remains that of the original. For
ways to subclass properly, please see further reading below.
typeof
operator in ECMAScript 5.1.typeof
operator as implemented by Firefox.Array
by Juriy Zaytsev.Kindof.js is released under a Lesser GNU Affero General Public License, which in summary means:
For more convoluted language, see the LICENSE
file.
Andri Möll typed this and the code.
Monday Calendar supported the engineering work.
If you find Kindof.js needs improving, please don't hesitate to type to me now at andri@dot.ee or create an issue online.
2.0.0 (May 25, 2015)
Removes support for boxed objects (new Boolean
, new Number
, new String
)
and considers them just as objects.
Boxed objects tend to be very error prone and it's best you stick to primitives only. The following is a small example of problems with boxed objects:
new String("a") == new String("a") // => false
new Boolean(true) == new Boolean(true) // => false
Boolean(new Boolean(false)) // => true
!!(new Boolean(false)) // => true
You can convert boxed objects to primitives by calling their valueOf
function:
new String("Hello").valueOf() // => "Hello"
Adds support for Symbol
.
FAQs
A proper typeof that works with primitives, built-in value objects and those from other execution contexts.
We found that kindof demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.