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

sortable-names

v2.0.0

Published

A JS class that formats name strings to semi-intelligently put the last name first, making them sortable by last name. Opinionated and English specific, but configurable.

Downloads

7

Readme

sortable-names

A JS class that formats name strings to semi-intelligently put the last name first, making them sortable by last name. Opinionated and English specific, but configurable.

Why?

You might need something like this if you have a list of authors and want to make them sortable by last name. Moving the last part of the name isn't a full solution, because author names may have honorifics or suffixes that break a simple approach like that. A work could be authored by an organization as well, and rearranging the name wouldn't make sense.

What sortable-names does by default

  • Strips extra whitespace
  • Removes honorifics before names
  • Keeps suffixes like Jr, PhD, IV, etc. at the end of the name
  • Leaves organization names mostly alone, only moving articles to the end of the name

What you can configure

  • The articles to move to the end of organization names
  • The words that identify organizations
  • The honorifics to strip from the beginning of names
  • The suffixes to leave at the end of names

Installing

Add the dependency:

npm i sortable-names

Import it and use it somewhere:

import { SortableNames } from "sortable-names";

const sortableNames = new SortableNames();

const formattedName = sortableNames.getSortable(
  "Dr. Martin Luther King Jr., Ph.D."
);

console.log(formattedName);
// "King, Martin Luther, Jr, PhD"

Sort helper

If you have an array of formatted names, sorting them isn't entirely trivial. Things like accented letters and puncuation in names like O'Malley can result in undesirable sorting order. Use the provided helper method to fix this.

import { SortableNames } from "sortable-names";

const compare = SortableNames.compare;

const formattedNames = ["O'Malley, Virgil", "Oliver, John"];

// Compares the ' in O'Malley to the l in Oliver
console.log(formattedNames.toSorted());
// ["O'Malley, Virgil", "Oliver, John"]

// Ignores punctuation
console.log(formattedNames.toSorted(compare));
// ["Oliver, John", "O'Malley, Virgil"]

Examples using defaults

These examples are sorted using the sort helper.

| Formatted | Original | | ------------------------------------- | ------------------------------------- | | Arnim, Elizabeth von | Elizabeth von Arnim | | Bulwer-Lytton, Edward | Edward Bulwer-Lytton | | Burton, Richard Francis | Sir Richard Francis Burton | | Darío, Rubén | Rubén Darío | | Environmental Protection Agency, The | The Environmental Protection Agency | | Homer | Homer | | King, Martin Luther, Jr | Martin Luther King Jr. | | Ólafsdóttir, Auður Ava | Auður Ava Ólafsdóttir | | O'Mahony, Daniel | Daniel O'Mahony | | Parson, Annie-B | Annie-B Parson | | Shakespeare, William | William Shakespeare | | Shelley, Mary Wollstonecraft | Mary Wollstonecraft Shelley | | Smollett, T. | T. Smollett | | U.S. Government Accountability Office | U.S. Government Accountability Office |

Configuring options

If you are unhappy with the defaults or want to extend them, you can pass options into the constructor.

import { SortableNames } from "sortable-names";

/**
 * To extend an option, the defaults are
 * available as static properties
 */
const extendedOrgWords = [...SortableNames.defaultOrgWords, "Circus(?:es)"];

/**
 * Only support org names in Spanish
 */
const replacedArticles = ["El", "Las?", "Los?", "Una?"];

/**
 * Don't strip any honorifics
 */
const noHonorifics = [];

/**
 * Suffixes can also be extended or modified,
 * for example to support LinkedIn Lunatics like
 * Horatio Herbert Kitchener, KG, KP, GCB, OM, GCSI, GCMG, GCIE, PC
 */
const extendedNameSuffixes = [
  ...SortableNames.defaultNameSuffixes,
  "KG",
  "KP",
  "GCB",
  "OM",
  "GCSI",
  "GCMG",
  "GCIE",
  "PC",
];

const configuredSortableNames = new SortableNames({
  articles: replacedArticles,
  orgWords: extendedOrgWords,
  honorifics: noHonorifics,
  nameSuffixes: extendedNameSuffixes,
});

Happy sorting!