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

better-map

v1.2.1

Published

Map class with added sugar

Downloads

10

Readme

better-map

Build Status Coverage Status

Map object extended with extra functional goodness.

Usage

Grab the package and add to your project

npm install --save better-map

Then add it to your code

const BetterMap = require('better-map');

API

Constructor

Takes an optional parameter of an iteratable object otherwise it returns an empty map.

const BetterMap = require('better-map');
const expect = require('code').expect;
const myMap = new BetterMap();
expect(myMap.size).to.equal(0);
const myOtherMap = new BetterMap([
	['one', 1],
	['two', 2]
]);
expect(myOtherMap.size).to.equal(2);

entriesArray

Returns all of the entries as an array in the format of an array of arrays with key then value. Entries are returned in the order that they were added.

const BetterMap = require('better-map');
const expect = require('code').expect;
const test = new BetterMap([ ['one', 1], ['two', 2] ]);
const actual = test.entriesArray();
expect(actual).to.equal([
	['one', 1],
	['two', 2]
]);

keysArray

Returns all of the keys as an array. Keys are returned in the order that the entries were added.

const BetterMap = require('better-map');
const expect = require('code').expect;
const test = new BetterMap([ ['one', 1], ['two', 2] ]);
const actual = test.keysArray();
expect(actual).to.equal(['one', 'two']);

filter

Takes a callback function and a thisArg parameter. It calls the callback function for every entry in the map with the parameter thisArg as the functions context (this) and returns all of the entries where the callback returned true as a new better-map.

The callback function takes value, key, map.

const BetterMap = require('better-map');
const expect = require('code').expect;
const test = new BetterMap([ ['one', 1], ['two', 2] ]);
const actual = test.filter((value, key) => value === 2);
expect(actual).to.be.an.instanceOf(BetterMap);
expect(actual.size).to.equal(1);
expect(actual.has('one')).to.be.false();
expect(actual.has('two')).to.be.true();

map

Takes a callback function and a thisArg parameter. It calls the callback function for every entry in the map with the parameter thisArg as the functions context (this) and returns an array of the results of each call.

The callback function takes value, key, map. The value of the array entry will be the value returned from the callback.

const BetterMap = require('better-map');
const expect = require('code').expect;
const test = new BetterMap([ ['one', 1], ['two', 2] ]);
const actual = test.map((value, key) => `${key} = ${value}`);
expect(actual).to.equal(['one = 1', 'two = 2']);

reduce

Takes a callback function and an initialValue parameter. It calls the callback function for every entry in the map and returns a single reduced value.

The callback function takes previousValue, value, key, map. The value of the array entry will be the value returned from the callback. previousValue will be undefined if an initialValue is not given.

const BetterMap = require('better-map');
const expect = require('code').expect;
const test = new BetterMap([ ['one', 1], ['two', 2] ]);
const actual = test.reduce((pv, cv) => pv + cv, 0);
expect(actual).to.equal(3);

some

Takes a callback function and a thisArg parameter. It calls the callback function for every entry in the map with the parameter thisArg as the functions context (this). Returns a boolean.

The callback function takes value, key, map and must return a boolean value. A value of true signifies that a match has been made and will cause the loop to end straight away.

const BetterMap = require('better-map');
const expect = require('code').expect;
const test = new BetterMap([ ['one', 1], ['two', 2] ]);
expect(test.some((value) => value === 1)).to.equal(true);
expect(test.some((value, key) => key === 'three')).to.equal(false);

stringify

JSON encode the instance. Will also encode child Map and Set objects.

const BetterMap = require('better-map');
const expect = require('code').expect;
const testMap = new BetterMap()
    .set('one', 1)
    .set('two', 'a')
    .set('three', null)
    .set('a', [1, 2, 3]);
const expected = JSON.stringify({
    one: 1,
    two: 'a',
    three: null,
    a: [1, 2, 3]
});

// Act
const actual = testMap.stringify();

// Assert
expect(actual).to.equal(expected);

toObject

Shallowly maps the current object to a object. Asserts that all of the keys for the map are strings.

const BetterMap = require('better-map');
const expect = require('code').expect;
const test = new BetterMap()
    .set('one', 1)
    .set('two', 'a')
    .set('three', null);
expect(test.toObject()).to.equal({
    one: 1,
    two: 'a',
    three: null
});

valuesArray

Returns all of the values as an array. Values are returned in the order that the entries were added.

const BetterMap = require('better-map');
const expect = require('code').expect;
const test = new BetterMap([ ['one', 1], ['two', 2] ]);
const actual = test.valuesArray();
expect(actual).to.equal([1, 2]);