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

set-order

v0.3.5

Published

Tiny module for sorting by a set order, using a custom sort function for omitting explicits.

Downloads

10

Readme

Set Order

Tiny module for sorting by a set order, using a custom sort function for omitting explicits. npm i set-order --save Useful for when you have an array of dynamic data, but you need to sort by a set order, rather than a natural sort order, such as alphabetically or numerically.

Travis   Coveralls   npm   License MIT

Getting Started

Take a scenario where you have a list of bedroom counts where if the count is zero then it's labelled as Studio — those labelled as such should appear at the beginning of the array.

Using the native sort with a simple comparator function would yield an unsatisfactory result.

const bedrooms = [4, 2, 'Studio', 1, 3];
bedrooms.sort((a, b) => a - b);

// [2, 4, 'Studio', 1, 3]

Instead by utilising set-order you're able to be explicit about which items should be grouped together in the sorting process, as well as where they reside in the array — at the beginning or at the end.

import { exact } from 'set-order';

const bedrooms = [4, 2, 'Studio', 1, 3];
bedrooms.sort(exact(['Studio', 1, 2, 3, 4]));

// ['Studio', 1, 2, 3, 4]

Sort

Taking the previous approach, its downside is immediately obvious in that you're required to specify all of the possible bedroom counts. Instead we'll specify only where Studio should appear since the other values can be numerically sorted using a - b.

import { exact } from 'set-order';

const bedrooms = [4, 2, 'Studio', 1, 3];
bedrooms.sort(exact(['Studio'], (a, b) => a - b));

// ['Studio', 1, 2, 3, 4]

It's worth noting that if we didn't pass a comparator function that items that weren't mentioned explicitly will not be repositioned.

Position

Building on the previous example we'll add an additional item entitled etc... which should appear at the end of the array: ['Studio', ..., 'etc...'] which we can easily achieve by using the position property that takes two possible values: head and tail where the default is head.

import { exact, tail } from 'set-order';

const bedrooms = [4, 'etc...', 2, 'Studio', 1, 3];
bedrooms.sort(exact([
    { value: 'Studio' },
    { value: 'etc...', position: tail }
], (a, b) => a - b));

// ['Studio', 1, 2, 3, 4, 'etc...']

Associative

Although the above is almost what we want, in real-life scenarios we're likely to be faced with an array of objects, rather than an array of primitives. For this set-order uses the fantastic object-path module which allows you to specify nested keys in the format a.b.c which would select c from { a: { b: { c: '!' }}}.

import { exact, tail } from 'set-order';
import by              from 'sort-by';

const bedrooms = [
    { id: 1, bedrooms: 4 },
    { id: 2, bedrooms: 'etc...' },
    { id: 3, bedrooms: 2 },
    { id: 4, bedrooms: 'Studio' },
    { id: 5, bedrooms: 1 },
    { id: 6, bedrooms: 3 }
];

bedrooms.sort(exact([
    { property: 'bedrooms', value: 'Studio' },
    { property: 'bedrooms', value: 'etc...', position: tail }
], by('bedrooms')));

// [{ id: 4, bedrooms: 'Studio' }, { id: 5, bedrooms: 1' }] etc...

Shorthand

Using the associative approach we're successfully sorting an array of objects on the key bedrooms, but being explicit in saying that Studio should appear first — regardless of how many instances of Studio there may be in the array — and etc... should appear at the very end.

However memorising the { value, property, position } interface may be somewhat difficult, nor is it too elegant. Instead we can be more succinct and chic by using head and tail as functions which take two parameters each: value and property where property is optional for an array of primitives.

import { exact, head, tail } from 'set-order';

const bedrooms = [4, 'etc...', 2, 'Studio', 1, 3];
bedrooms.sort(exact([head('Studio'), tail('etc...')], (a, b) => a - b));

// ['Studio', 1, 2, 3, 4, 'etc...']