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

js-prototype-lib

v1.29.21

Published

A library of prototypes for Javascript

Downloads

26

Readme

npm version

Javascript Prototype Library

This collection of native-type prototype extensions in Javascript helps make expressions more terse and improves legibility. The argument could be made that its use is unadvisable within browsers as your mileage may vary due to the instability of these implementations, but in NodeJs it works (and comes with a test suite)

Install / Import

The project is published on NPM so install is easy:

npm install js-prototype-lib

...and to use in your projects, simply import it:

// import the module
const jsp = require('js-prototype-lib');

// install classes of extensions
jsp.install();                      // installs all extensions
jsp.install('object');              // installs object prototypes (see warnings)
jsp.install('array', 'string');     // installs array and string extensions

// and use them
console.log([1,2,3].last());        // should print 3

// if the extensions are no longer needed
// they can be uninstalled

jsp.uninstall('object');            // just object extensions
jsp.uninstall();                    // or all

Extensions

The prototype extensions provided are documented separately by root object:

Errors

Errors are not easily examinable, thus attempting to create a Json string from one will fail. By installing the error module it is possible to extract the object within an error:

const jsp = require('js-prototype-lib')
jsp.install('error')
try {
   throw new Error('test')
}
catch(e) {
    console.log(JSON.stringify(e))        // produces '{}', but
    console.log(JSON.stringify(e.obj()))  // generates {"message": "error", "stack": "Error: test\n at Context.it..."}
    console.log(e.json())                 // more easily
}

Common

The objects Array, String Object and Number have also been marked up with properties that facilitate determining their type. This short-circuits the need to do typeof x == '...' comparisons, which is inconsistent with arrays where the result is 'object'. Additionally, the .typeof property is also defined for each object

Below are the properties available:

var x = '';
console.log(x.isStr);             // prints true
console.log(x.isArr);             // prints false
console.log(x.isObj);             // prints false
console.log(x.isNbr);             // prints false
console.log(x.typeof);            // prints 'string'
x = [];
console.log(x.isStr);             // prints false
console.log(x.isArr);             // prints true
console.log(x.isObj);             // prints false
console.log(x.isNbr);             // prints false
console.log(x.typeof);            // prints 'array'
x = {};
console.log(x.isStr);             // prints false
console.log(x.isArr);             // prints false
console.log(x.isObj);             // prints true
console.log(x.isNbr);             // prints false
console.log(x.typeof);            // prints 'object'
x = 0;
console.log(x.isStr);             // prints false
console.log(x.isArr);             // prints false
console.log(x.isObj);             // prints false
console.log(x.isNbr);             // prints true
console.log(x.typeof);            // prints 'number'

Notes

As this module may be included at multiple levels of a project (a project includes it but a dependency of the project also includes it), and the versions at each level differ, the codebase makes sure that only the latest version available is installed.

To accomplish this the Array, Object and String objects are marked up with the attributes library and version, thus to check what version is loaded you can:

const jsp = require('js-prototype-lib')
console.log(jsp.version().SemVer)

// or load some functions and check the objects
jsp.install('string')
console.log(String.version)

both of which could show:

1.0.11

Contribute

If you have an extension you're fond of, post an issue on Github. We're happy to add new functionality. If you clone this repo and make changes, make sure to add tests for your changes and generate a Pull Request only when they pass. You can run like this:

npm test

If you decide to publish on NPM this command should do it:

npm version <patch|minor|major>

Licence

MIT