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

ordered-set

v1.0.0

Published

`ordered-set` is a performant ES6 Set subclass that allows control over iteration order.

Downloads

18

Readme

ordered-set

ordered-set is a performant ES6 Set subclass that allows control over iteration order.

Simply provide the set with the ordering function to use and it will do the rest.

const OrderedSet = require('ordered-set')
let orderedSet = new OrderedSet()
orderedSet.use(mySortingFunction)
orderedSet.add(item)
orderedSet.add(item2)
for (let setItem of orderedSet) {
  // iterates in order defined by mySortingFunction
}

Installation

npm install ordered-set

Required ES6 Features

Although the code is compiled and published as ES5, there are some ES6 standard library features required:

  • Symbols
  • Set

How to get ES6 features

Install es6-shim or 6to5. traceur also works well. Unfortunately, did not have luck using the more lightweight & modular es6-set & es6-symbol.

You'll need to install an ES6 polyfill yourself, such as those listed above. There is not one included with the package on purpose – this may seem like malpractice and I normally would advise against any kind of implicit dependencies but after battling with these issues across multiple projects I've concluded npm currently has no suitable workflow for anything that mutates the global environment i.e. must be a singleton.

I feel this should be best-practice for language polyfills – by omitting a transpiler you're free to use this with whatever transpiler you're already using.

This lib could be easily reworked to not require these ES6 features but the intended audience is people already compiling to ES6, or those interested in doing so.

Usage

const OrderedSet = require('ordered-set')

// Default sortFunction is numeric, ascending:
// (a, b) => a - b
let orderedSet = new OrderedSet()
orderedSet.add(2)
orderedSet.add(1)
orderedSet.add(3)

orderedSet.forEach(item => console.log('orderedSet default sortFunction', item))
// orderedSet default sortFunction 1
// orderedSet default sortFunction 2
// orderedSet default sortFunction 3

Replace sorting function with .use(fn)

orderedSet.use((a, b) => b - a) // e.g. reversed ordering

orderedSet.forEach(item => console.log('orderedSet custom sortFunction', item))
// orderedSet custom sortFunction 3
// orderedSet custom sortFunction 2
// orderedSet custom sortFunction 1

Regular ES6 Set for comparison

let regularSet = new Set()
regularSet.add(2)
regularSet.add(1)
regularSet.add(3)

ES6 Sets iterate in insertion order

regularSet.forEach(item => console.log('regular set', item))
// regular set 2
// regular set 1
// regular set 3

OrderedSet Supports all regular ES6 Set operations & usage

orderedSet = new OrderedSet([3,2,1])
orderedSet.add(0)
console.log('orderedSet.size', orderedSet.size) // set.size 4
orderedSet.delete(0)
console.log('orderedSet.size', orderedSet.size) // set.size 3

License

MIT