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

wrapple

v0.4.0

Published

Wrapper for runtime natives to allow stubbing in unit tests.

Downloads

27

Readme

wrapple travis build dependencies dependencies

Dependency free wrapping function for browser natives to allow stubbing in unit tests.

Accessing natives through a thin wrapper makes stubbing possible, where it would otherwise be impossible.

You can use it with pretty much all globals defined on the window object.

Link Seam

In Working Effectively with Legacy Code, Michael Feathers describes Seams. In the vernacular of that book, wrapple would most likely be described as a Link Seam.

Compatibility

wrapple should be able to run in most environments that can execute JavaScript

ES5.1 required

wrapple uses a couple of methods from ES5.1

If you need to support old browsers, you should ensure that these have been polyfilled.

Node.js supported

wrapple can work in Node.js environments just like in browser environments.

Usage

Access browser natives through wrapple

Step 1

// use whatever local name you like
var wrap = require('wrapple');

// ensure that wrapple has wrapped the property you're interested in
// this is idempotent, call it many times with no ill effects
wrap('location');

// directly use the returned global
var hostname = wrap('location').hostname;

Step 2

Now that your application code is using wrapped globals, you can target the wrapper function for stubbing, spying, etc.

var stub = sinon.stub(wrap, 'location', function(){
    return {
        hostname: 'wrapple.example.com'
    };
});

// ...

Step 3

Tidy up your tests

// using sinon
stub.restore();

// or using wrapple.reset
wrap.reset();

Methods

wrap

// wrap - adds a wrapped property method to the wrapple api
// returns a function that returns window.location
// also creates wrap.location method as target for stubbing
wrap('location');

// add and use immediately
var hostname = wrap('location').hostname;

// use via dedicated method
wrap('location');
// ...
var hostname = wrap.location().hostname;

reset

// reset, removes all wrapper methods from wrapple api
wrap.reset();

Unwrappable methods

These globals on the window object are not wrappable, as creating the returning methods on wrapple would interfere with the wrap function.

  • constructor
  • isPrototypeOf
  • length
  • name
  • propertyIsEnumerable
  • toLocaleString
  • toString

That shouldn't be too much of a problem, as they seem unlikely targets for stubbing.

Links

License

MIT: http://mrgnrdrck.mit-license.org

wrapple.jp

In no way affiliated with, but admiring wrapple.jp