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

sort-lib

v1.0.2

Published

A small set of utilities for writing more readable sort functions

Downloads

166

Readme

sort-lib

A small set of utilities for writing more readable sort functions.

  • Tiny - almost fits in a tweet, tree-shakable.
  • Typescript
  • ES modules

If you're like me, you probably always need to look up how to sort an array in JS. ("How do you sort strings again?", "Is it first argument minus second argument for descending, or the other way around?") This module is meant to solve that problem, while making the code more readable in the process.

Installation

npm install sort-lib

Usage

import { desc, byNum } from 'sort-lib';

[
  { age: 25 },
  { age: 10 },
  { age: 42 }
]
  // Sort descending by number `.age`
  .sort(desc(byNum(value => value.age)))

/*
[
  { age: 42 },
  { age: 25 },
  { age: 10 }
]
*/

Functions

asc / desc

Wrap the sort function with asc or desc to set the sort direction. If you do not use this function, sorting will always happen in the ascending direction.

byNum(numberGetter)

Creates a sort function that can be used to sort a number. You have to pass in a function that returns a number given a value in the array - this number is what the sorting is performed on. byNum returns a sort function.

byDate(dateGetter)

Like byNum, but for date objects. Pass in a function that returns a date given a value in the array. Returns a sort function.

Internally, compares dates by calling .getTime() on the date objects.

[
  { dateOfBirth: new Date('2000-01-01') },
  { dateOfBirth: new Date('2020-01-01') },
  { dateOfBirth: new Date('1970-01-01') }
]
  // Sort ascending by date `.dateOfBirth`
  .sort(asc(byDate(x => x.dateOfBirth)))

/*
[
  { dateOfBirth: new Date('1970-01-01') },
  { dateOfBirth: new Date('2000-01-01') },
  { dateOfBirth: new Date('2020-01-01') }
]
*/

byString(stringGetter)

Like byNum and byDate above, but for strings. Pass in a function that returns a string given a value in the array. Returns a sort function.

Internally, compares strings using the .localeCompare method.

[
  { name: 'Bob' },
  { name: 'Alice' },
  { name: 'Charlie' }
]
  // Sort descending by string `.name`
  .sort(desc(byString(x => x.name)))

/*
[
  { name: 'Charlie' },
  { name: 'Bob' },
  { name: 'Alice' }
]
*/

Tips

  • Define a function called prop, or use one from a library like ramda for even more point-free code:

    const prop = <T>(key: keyof T) => (obj: T) => obj[key];
    
    [{ age: 42 }, { age: 10 }]
      .sort(desc(byNum(prop('age'))))
  • If your array only contains primitives, you might want to define a function called identity (or use the one from ramda)

    const identity = <T>(x: T) => x;
    
    ['Alice', 'Bob'].sort(desc(byString(identity)))