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

es6-set-and-map

v1.0.5

Published

ES6 Set and Map polyfill and improvements.

Downloads

2,031

Readme

JavaScript ES6 Map and Set polyfill and improvements.

JavaScript introduced Map and Set objects with new ES6.

Map provides functionality to use objects as object key.
Set is a unique collection of items.

This implementation uses hash tables to identify items in Set an Map and gives extra features.

Now available with bower with name 'es6-set-and-map'

Usage.

To use with NodeJS.

npm install es6-set-and-map.

Map

var Map = require( 'es6-set-and-map' ).map, 
    obj = new Map(), 
    blank_obj = {}, 
    span_el = document.createElement('span');

//  Sets values.
//  Set method returns it's map, so this call can be chained.
//  You can use anything as key.
obj
    .set( blank_obj, 'can be any value' )
    .set( 'some-key', 'some value' )
    .set( NaN, 'nan value' )
    //  You can specify a new item's index too.
    .set( undefined, 'undefined value', 1 )
    .set( null, 'null value' )
    .set( Object.create(null), 'null value' );

//  Adds values.
//  Will work only if key does not exist in the map.
//  Returns true if values added.
obj.add( span_el, 'qwerty' ) === true;

//  blank_obj is already used, so this will return false.
//  Position argument can be used with add too.
obj.add( blank_obj, 'qwerty', 2 ) === false;

//  Increments numeric value.
//  If value is not defined, creates with initial value 0, than increments it.
//  Works like i++, so will return previous value.
obj['++']( 'something' ) === 0;

//  Decrements numeric value.
//  Same as ++, but works like --i, so will return the new value.
obj.set( 'something', 12 );
obj['--']( 'something' ) === 11;

//  Array manipulation methods.
//  If value is not array, returns false.
//  If value is not defined, creates empty array and does operations on it.
//  Returns result of the operation.
obj.push( 'anything', 'item0', 'item1', 'item2' ) === 3;
obj.unshift( 'anything', 'firstitem' ) === 4; 
obj.pop( 'anything' ) == 'item2';
obj.shift( 'anything' ) == 'firstitem';
//  This two returns changed value.
obj.concat( 'anything', ['value1', 'value2'] );
obj.splice( 'anything', 2, 2 );

//  Object property manipulation methods.
//  If value is not an object, returns false.
//  If value is not defined, creates empty object and does operations on it.
obj.addProp( 'otherthing', 'objkey', 'objvalue' ) === true;
obj.setProp( 'otherthing', 'objkey', 'changed value' ) === true;
obj.getProp( 'otherthing', 'objkey' ) === 'changed value';
obj.deleteProp( 'otherthing', 'objkey' ) === true;
obj.hasProp( 'otherthing', 'objkey' ) === false;

//  Gives value for given key.
console.log( obj.get( span_el ) );

//  Checks if given key is used.
console.log( obj.has( blank_obj ) );

//  Deletes value of this key.
//  Returns true if value existed.
console.log( obj.delete( span_el ) );

//  Elements count.
console.log( obj.length );

//  Array of keys and objects.
console.log( obj.keys );
console.log( obj.values );

//  Iterating.
obj.start();
while( obj.next() ) {
    //  use obj.key, obj.value
}

//  Iterating back.
obj.end();
while( obj.prev() ) {
    //  use obj.key, obj.value
}

//  ES6 "for of" iteration
for( let [key, value] of obj ) {
    //  ...
}

Set

var Set = require( 'es6-set-and-map' ).set, 
    obj = new Set(), 
    blank_obj = {}, 
    span_el = document.createElement('span');

//  Adds values.
obj.add( span_el ) === true;
obj.add( blank_obj, 1 ) === true;
obj.add( span_el ) === false;

//  Adds multiple values and returns added items count.
obj.addMulti( [span_el, blank_obj, 'qwer'], 1 );

//  Checks if item exists.
console.log( obj.has( blank_obj ) );

//  Removes item.
//  Returns true if item existed.
console.log( obj.delete( span_el ) );

//  Elements count.
console.log( obj.length );

//  Array of items.
console.log( obj.values );

//  Iterating.
obj.start();
while( obj.next() ) {
    //  use obj.value
}

//  Iterating back.
obj.end();
while( obj.prev() ) {
    //  use obj.value
}

//  ES6 "for of" iteration
for( let value of obj ) {
    //  ...
}

Browser support

|Firefox|Chrome |IE |Opera |Safari | |:-----:|:-----:|:-:|:-----:|:-----:| |5 |5 |9 |11.60 |5.1 |