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

utils-type

v0.16.1

Published

robust type checking for js

Downloads

232

Readme

utils-type NPM version

documentation - install - notes - todo

build

simple

var type = require('utils-type');

type(42);              // -> { number: 42 }
type(NaN);             // -> { nan: true }
type(null);            // -> { null: true }
type(true);            // -> { boolean: true }
type(false);           // -> { boolean: true }
type(Infinity);        // -> { infinity: Infinity }
type(undefined);       // -> { undefined: true }
type('a string');       // -> { string: 'a string' }
type(/a regex/);       // -> { object: /a regex/, regexp: /a regex/ } }
type(function(){ });   // -> { object: [Function], function: [Function] }
type({ type : 'toy' });  // -> { object: { type: 'toy' } }
type(new Date());      // -> { object: Mon Sep 08 2014 19:10:32,  date: Mon Sep 08 2014 19:10:32 GMT+0200 (CEST) }
type(new Error());     // -> { object: [Error], error: [Error] }
type(new Stream());
// -> {
//   object: { domain: null, _events: {}, _maxListeners: 10 },
//   stream: { domain: null, _events: {}, _maxListeners: 10 },
//   eventemitter: { domain: null, _events: {}, _maxListeners: 10 }
// }

composition

The function returns an object. The type matched by that type returns itself.

type(1).number                      // -> 1 (that is truthy)
type([1,2,3]).array                 // -> [1,2,3] (truthy)
type(type([1,2,3]).array[1]).number // -> 1 (truthy)

comprehensive

type(-1).number            // -> -1
type(-1).integer           // -> -1
type(NaN).nan              // -> true
type(0.4).float            // -> 0.4
type(Infinity).infinity    // -> Infinity
type(new EventEmitter())
// -> {
//   object: { domain: null, _events: {}, _maxListeners: 10 },
//   eventemitter: { domain: null, _events: {}, _maxListeners: 10 }
// }

falsy values maintain the value if it makes sense

type(0).number             // -> true
type(0).integer            // -> 0
type('').string            // -> ' '
type(null).null            // -> true
type(NaN).number           // -> undefined
type(false).boolean        // -> true
type(undefined).undefined  // -> true

Why:

  • NaN is not a number
  • false is a boolean, returning it will be misleading
  • 0 is a number, but for integer its value its maintained
  • the empty string is changed to an space so is truthy and operations can be made on it
  • null and undefined are self explanatory

The module.exports a function

var type = require('utils-type');

that takes one argument.

type

function type(value)

arguments

  • value type any, from where types will be obtained

returns

  • object with as many properties as types the argument has
var type = require('utils-type');
var items = type([1,2,3]);
Object.keys(items); // =>
// ['object', 'array']

install

With npm

$ npm install utils-type

test

$ npm test

notes

The module uses Objec.create in order to not produce false positives when checking properties on the returned object. So it basically needs support for object create. Polyfill it, for more info:

http://kangax.github.io/compat-table/es5/

todo

  • [ ] Include browser tests

license

The MIT License (MIT)

Copyright (c) 2014-present Javier Carrillo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.