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

virtual-prototype

v2.0.0

Published

Create utility methods without adding them directly to prototypes

Downloads

13

Readme

About

The temptation of extending prototypes is always there, lurking in the shadows when writing javascript, but it is never a good idea. Its abuse has prevented the development of the web (it's the reason why Array.prototype.contains won't exist any time soon).

I present to you a much safer solution. A wrapper that enables you to define methods on top of any type (except null and undefined), so that you can keep things nice and object oriented!

Usage

My recommended way to use this module is to create a extensions module for your project that exports an instance of VirtualPrototype. Then you can import that module throughout your project.

Another way to use the module would be to create an npm package containing a frozen singleton instance of set of utility (akin to lodash). After all methods have been added you can call finalize on the instance to prevent editing. This would also prevent monkey patching, so whether to do that is up to you.

Virtually extending types

var VirtualPrototype = require('virtual-prototype')
var vp = VirtualPrototype();

// define the types you wish to "extend"..

vp.defineType('string');       // checks type with typeof
vp.defineType('array', Array); // checks type with instanceof

// ..define the methods on the virtual prototypes.

vp.string.define('secondCharacter', function () {
    if (this.length < 2)
        return '';
    return this.charAt(1);
});

vp.array.define('last', function () {
    if (! this.length) {
        return null;
    }

    return this[this.length - 1];
});

vp('hello').secondCharacter(); // 'e'

vp([1, 2, 3]).last();          // 3

Creating methods for all types

var VirtualPrototype = require('virtual-prototype');
var vp = VirtualPrototype();

vp.appendString = function (str) {
    return this + str;
};

vp(9).appendString(' times');       // '9 times'
vp('Hello').appendString(' world'); // 'Hello world'

API documentation

vp.defineType

Arguments

typeAndIdentifier

Itentifier, constructor

Example

// Type checks using typeof (+ instanceof String if identifier is 'string')
vp.defineType('string');

// Type checks using instanceof
vp.defineType('array', Array);

vp.define

Arguments:

name, handler

Example

vp.define('toUppercaseString', function () {
    return this.toString().toUpperCase();
})

vp({}).toUppercaseString(); // '[OBJECT OBJECT]'

vp.<type>.define

Define a method on the virtual prototype for the given type. <type> must be defined using defineType and must match the first parameter given to defineType.

Example


// Using just type (matched with typeof)
vp.defineType('string');

vp.string.define('reverse', function () {
    var result = '';

    for (var i = this.length - 1; i >= 0; i--) {
        result += this[i];
    }

    return result;
});

// Using constructor function as second argument (matched with instanceof)
vp.defineType('array', Array);

vp.array.define('reverse', function () {
    for (var i = 0, j = this.length - 1; i < this.length/2; i++, j--) {
        var tmp = this[i];
        this[i] = this[j];
        this[j] = tmp;
    }
    return this;
});

vp.finalize

Prevent further changes to the instance of VirtualPrototype. After this, no new methods can be registered and no methods can be altered/deleted.

Testing

Node

npm test

Browser

  • npm install --global browserify
  • npm run browserify
  • open browsers-test.html (and copy the url to the browser you wish to test in)

Tested in

  • Node v4.2.0
  • Chrome 50.0.2661.102
  • Safari 9.1.1
  • Firefox 46.0.1
  • IE11

Notes

  • Overriding functions using define(<existing-name>, <handler>) for a given type is illegal and will throw a TypeError.
  • null/undefined can not be captured by vp() as they can not be referenced by this. Throws a TypeError.