npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dis

v0.3.7

Published

Tiny tiny duck typing utility

Downloads

37

Readme

Dis

Tiny tiny Library to check object types. Works in node and your browser.

Use it like this:

first require it:

var is = require('dis');

or in browser:

<script type="text/javascript" src="dis"></script>

You'll find the browser version in /dist;

Then check:

is.object(<obj>)

OR

is(<obj>,"object")

OR

is(<obj>, Object)

all are equally valid, but note that the function call is.<type>(<object>) will do a thorough check whereas is(<object>,<type>) will do a shallow check (simply calling Object.prototype.toString on the object);

You can also do things like:

	 is(<obj>) // returns type as a string
	 is(<var1>,<var2>,<var3>,...,Array) // checks that all vars are of type 'array'

note: I use "is" because it's shorter and more explicative. This is how I had named the plugin originally, but the name was not available on npm, so I had to revert to something else. the original "is" plugin is here. It's sorta similar, but has more functions, and a different API. See if you like it better.


Require only what you need

All functions are available separately. If you know you'll only need to check for 'empty', for example, then you can just include var isEmpty = require('dis/lib/isEmpty).

To do this in the browser, you'll need to whip up your own build, through gulp, webpack, or whatever tickles your fancy.


API

is.type(obj)

Returns a string that represents the object's type. That string might be any of:

  • arguments
  • array
  • boolean
  • date
  • error
  • function
  • html collection (depending on the browser)
  • null
  • number
  • object
  • regexp
  • string
  • undefined

is.checkType(obj,[type])

If called without the type arguments, yields same results as is.type, but has more thorough checks. Whereas is.type simply calls Object.prototype.toString on the object, is.check goes through all the checks available to determine the type. Of course, this is much slower.

If called with the type argument, checks the object against the type provided, where type is:

  • a string ('array', 'string', ...)
  • a native function (Array, String)
  • a named function function NamedFunction(){}
  • true
  • false

If type is not a known type, throws an error.

You can add your own tests by overloading the is.check.tests object.
Example:

function CustomObject(){}
is.check.tests.customObj = function(obj){
	return obj instanceof CustomObject;
}
var obj = new CustomObject();
console.log(is.checkType(obj)) //returns 'customObj'
console.log(is.checkType(obj,'customObj'))//return `true`

is.test(obj,type)

Tests if obj is of type, where obj is anything, and type is:

  • a string ('array', 'string', ...)
  • a native function (Array, String)
  • a named function function NamedFunction(){}
  • true
  • false

is.test makes use of is.type, so it won't check for custom objects like is.checkType would.

is.testMultiple(obj[,...obj],type)

Same as is.test, but will test all provided objects against the type.

is.same(obj,obj[,...obj])

Checks that all provided objects are of the same type. If not, returns false. If they are, returns the type as a string. Aliases as is.sameType.

is(obj)

Alias for is.type(obj)

is(obj,type)

Alias for is.test(obj,type)

is(obj,obj[,...obj],type)

Alias for is.testMultiple


Complete list of tests

is.arguments(obj)

Checks if obj is an arguments object. Please be aware than passing the arguments around stops optimization.
Alias: is.args.

is.array(obj)

Checks if obj is an Array.
Aliases: is.arr

is.boolean(obj)

Checks if obj is strictly true or false.
Alias: is.bool

is.date(obj)

Checks if obj is a date object.

is.defined(obj)

Checks if obj is defined.
Alias: is.def

is.empty(obj)

Checks if obj is an empty array, an object with no properties, false, or an empty string. In essence, checks if the object is 'falsy'.
Aliases: is.false, is.falsy

is.error(obj)

Checks if obj is of type Error.
Alias: is.err

is.function(obj)

Checks if obj is a Function. Aliases: is.fun,is.fn,is.func

is.HTMLCollection(obj)

Checks if obj is an HTML nodes collection.
Aliases: is.htmlCollection, is.htmlcollection

is.integer(obj)

Checks if obj is an integer.
Alias: is.int

is.native(obj)

Checks if obj is a native function.

is.node(obj)

Checks if obj is an HTML node.

is.null(obj)

Checks if obj is strictly null.

is.number(obj)

Checks if obj is strictly a number.

is.numeric(obj)

Checks if obj can be cast to a number. Works on strings etc.

is.object(obj)

Checks if obj is a plain object, that is, if its prototype is Object.
Alias: is.obj

is.reference(obj)

Checks if obj would be passed by reference. Anything else than string or number returns true.
Alias: is.ref

is.regexp(obj)

Checks if obj is of RegExp type.
Aliases: is.regExp, is.regularExpression

is.string(obj)

Checks if obj is a String.
Aliases: is.str

is.true(obj)

Checks if obj is truthy. Empty arrays, empty objects and the like return false.
Alias: is.truthy

is.undefined(obj)

Checks if obj is undefined.
Alias: is.undef


Tests, build

test:

npm test

continuous testing

npom run dev

build:

npm run build

License

MIT