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

type-iterator

v0.1.1

Published

run a callback function against specific js types

Downloads

119

Readme

type-iterator

Build Status Coverage Status

About | Installation | API | Examples | Gotchas | License

About

A Node.js module that runs a callback function against multiple basic JS types. Envisioned as a testing utility to ensure that functions and methods respond appropriately to specific types. Requires Node.js 4.0 or newer.

Supports the following types/entities:

  • undefined
  • null
  • String
  • Boolean
  • Number
  • Function
  • Array
  • Object

Note that the new ES6 types are not included (Symbol, Map, Set, WeakMap, WeakSet...). I'm not opposed to adding them; I just didn't need them for my intended use of this module (yet).

Installation

Install and require in typical Node.js fashion.

Using in module: $ npm install --save type-iterator

Using in tests: $ npm install --save-dev type-iterator

API

The following object is the heart of this module:

const types = {
  'undefined': undefined,
  'null': null,
  'string': 'abc',
  'boolean': true,
  'number': 1,
  'function': new Function(),
  'array': [],
  'object': {}
}

Each value is a type literal for the type named in its key. type-iterator provides three ways to iterate over these types.

typeIterator(cb)

Iterates over all types. If cb is provided, iterate and return array of cb results. Else return unmodified types object.

typeIterator.exclude(types, cb)

Iterates over all types except specified exclusion(s). types can be a string or array of strings. If cb is provided, iterate over types except exclusions and return results. Else return modified types object.

typeIterator.include(types, cb)

Iterates over only specified type(s). types can be a string or array of strings. If cb is provided, iterate over specified types and return results. Else return modified types object.

Examples

Contrived

//----------------------------------------------------------
// setup
//----------------------------------------------------------
const typeIterator = require('type-iterator')

function exampleCb(val, key) {
  console.log(key, ':', val)
}

//----------------------------------------------------------
// allTypes
//----------------------------------------------------------
typeIterator() // returns types object with all types

typeIterator(exampleCb) // console logs each key: val pair

//----------------------------------------------------------
// exclude
//----------------------------------------------------------
// get types object sans 'undefined' type
// note: could also use an array of strings as param (for multiple types)
typeIterator.exclude('undefined')

// log all key: val type pairs except undefined
typeIterator.exclude('undefined', exampleCb)

//----------------------------------------------------------
// include
//----------------------------------------------------------
const typesToInclude = ['object', 'array']

// get types object with only object and array key: val pairs
// note: could also use a single string as param (for 1 type)
typeIterator.include(typesToInclude)

// log object and array key: val pairs
typeIterator.include(typesToInclude, exampleCb)

Semi-Realistic

// index.js
const kindOf = require('kind-of')

module.exports = function(text) {
  // explicitly throw if text isn't a string
  if (kindOf(text) !== 'string') {
    throw new Error('expected text param to be a String')
  }

  // do cool jazz here
}
// test.js
const assert       = require('chai').assert
const main         = require('./')
const typeIterator = require('type-iterator')

describe('main', () => {
  it('throws when text is not a string', () => {
    typeIterator.exclude('string', (val) => {
      assert.throws(() => main(val))
    })
  })
})

Gotchas

Keep in mind that the undefined and null type literals evaluate to false in simple conditional expressions. Consider the following contrived example with an optional parameter and a type restriction using kind-of:

function awesomeFunc(opt) {
  if (opt && kindOf(opt) === 'array' || kindOf(opt) === 'object') {
    console.log('hooray! opt provided')
  } else if (opt) {
    throw new Error('you dun goofed! wrong opt type')
  }
}

So, we can use type-iterator to write a quick test that makes sure awesomeFunc throws for all types other than array and object.

// WARNING THIS FAILS
// mocha-style test
describe('awesomeFunc', () => {
  it('throws for all types other than object and array', () => {
    typeIterator.exclude(['object', 'array'], (val) => {
      assert.throws(() => awesomeFunc(val))
    })
  })
})

This test fails. Why? Because awesomeFunc checks for the optional param with if (opt). if(null) and and if(undefined) are both false, so the throw never gets called for those values. The fix is either to make awesomeFunc's optional param checking more robust or also exclude 'null' and 'undefined' in the test case.

License

MIT