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

betwixt

v0.3.0

Published

String rank for re-orderable lists

Downloads

8

Readme

Betwixt

Betwixt implements three operations on (a subset of) JavaScript strings:

  • Betwixt.before(s) returns a string less than s
  • Betwixt.after(s) returns a string greater than s
  • Betwixt.between(s,t) returns
    • a string greater than s and less than t if s < t,
    • a string greater than t and less than s if t < s, and
    • s if s and t are equal.

(We say a subset of JavaScript strings because Betwixt strings are nonempty and have no trailing null (\u0000) characters.)

The use case is this: suppose we have a reorderable list — an ordered list where the order can be manually changed by the user. Furthermore, each list item is an independent database record. We want to be able to change the position of an item by changing its order field, without changing any other records in the database (as well as to add an item to the beginning or end of the list, or between any two items in the list).

If we could order the list using real numbers we could do this easily. For instance, suppose the list is sorted by a field called order, and there are two records a and z with a.order < z.order. If we have a fresh record call new, we can put it at the start of the list by setting new.order = a.order-1. We can put it at the end of the list by setting new.order = z.order+1. And we can put it between a and z by setting new.order = (a.order+z.order)/2. If we have a list with elements a, b, and c in that order, we can move c between a and b by changing c.order to (a.order+b.order)/2.

Unfortunately, we can't get our hands on actual real numbers, and JavaScript's Number type (IEEE 64-bit floating point) can run out of precision relatively quickly: Suppose a.order is 1 and b.order is 2, and we insert new1 between a and z, new2 between a and new1, new3 between a and new2, and so forth, using the method of averaging given above. By the time we get to new53, floating point precision limitations will cause new53.order to be 1 — the same as a.order. We'll no longer be able to guarantee that our list is ordered as the user wishes.

But we don't need actual real numbers -- we need what mathematicians call a dense linear order (without endpoints). Betwixt implements that — see the annotated CoffeeScript source for information about how it's done. The specs may also be informative.

Other Methods

  • Betwixt.trim(s) returns s with trailing null characters removed
  • Betwixt.validated(s) is like Betwixt.trim(s), but throws an error if the result would be the empty string
  • Betwixt.toHex(s) returns a hexadecimal representation of s.
  • Betwixt.midpoint() returns a string that's a good initial value for the rank of the item in a one-item list — representations of the values above and below the midpoint are equally space-efficient.

Warning

One caveat: using Betwixt strings, we can do almost unlimited numbers of the insertions described above; but if we do, say a million of them, then the new1000000.order string will be something like 125,000 bytes long. Things will probably not be that bad in practice, since changes and insertions are likely to be more random in real-world applications. And Betwixt tries to minimize string explosion where it can. In particular, the before and after methods optimize the common cases where an item is inserted at the beginning or end of a list.

See also

Dominic Tarr's between npm module is very similar to Betwixt, though less space-efficient.