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

sorted

v0.1.1

Published

sorted arrays

Downloads

1,071

Readme

sorted

A sorted array data structure

testling badge

build status

example

sorted

var sorted = require('sorted');
var xs = sorted([ 3, 1, 2, 0 ]);
console.log(xs);
xs.push(2.5);
console.log(xs);

output:

<Sorted [0,1,2,3]>
<Sorted [0,1,2,2.5,3]>

neighbors

var sorted = require('sorted');

var xs = 'bhdajemfcngiklop'.split('');
var s = sorted(xs);
var ix = s.findIndex('h');
console.log('current:', s.get(ix));
console.log('prev:', s.get(ix-1));
console.log('next:', s.get(ix+1));

output:

current: h
prev: g
next: i

range

var sorted = require('sorted');

var xs = 'bhdajemfcngiklop'.split('');
var s = sorted(xs);
console.log(s.range('e', 'j').join(''));

output:

efghij

methods

var sorted = require('sorted');

var s = sorted(xs, cmp)

Create a new sorted array s given an array xs which may or may not be sorted and a comparison function cmp. If cmp isn't specified it defaults to:

function cmp (a, b) {
    if (a == b) return 0
    else if (a > b) return 1
    else if (a < b) return -1
    else throw new RangeError('Unstable comparison: ' + a + ' cmp ' + b)
}

cmp should return 0 for equality, < 0 for less than, and > 0 for greater than. If cmp returns a value for which comparisons fail such as NaN it will throw a RangeError.

s.isSorted(xs, cmp)

Return whether xs is sorted using cmp. xs can be an array or a sorted instance, which always return true.

s.fromSorted(xs, cmp)

Create a new sorted structure from a pre-sorted array or sorted object xs using a comparison cmp which falls back to the default if unspecified.

s.push(x, ...), s.unshift(x, ...)

Insert the element or elements x, ... into the data structure, maintaining the sorted order.

s.splice(i, len, ...)

Remove len elements starting at index i. Any additional arguments will be pushed to the structure maintaining the sorted order.

s.findIndex(x, start=0, end=s.length)

Search for the index of the value x starting at index start and ending at end using a binary search.

If the value lies outside the given range, return either bound depending.

s.toArray()

Return a copy of the underlying sorted data as an array.

s.sort(cmp)

Return a new sorted structure using the comparison cmp. If the comparison is the same as s.compare, returns a sorted copy.

s.concat(x, ...)

Return a new sorted which concatenates the items, arrays, and sorteds in the arguments list.

s.insert(xs)

Insert the array, element, or sorted xs into the structure. If xs is a sorted, this fact will be observed to make the insertion go faster.

s.get(i)

Get the element at index i.

s.set(i, x)

Set the element at index i, preserving the ordering.

s.slice(i, j)

Return a new sorted containing elements between i and j using s.compare.

s.map(fn)

Return a new sorted mapping over the elements of s with fn.

s.filter(fn)

Return a new sorted filtering over the elements of s with fn.

s.shift()

Return and remove the least item in the structure.

s.pop()

Return and remove the greatest item in the structure.

s.range(a, b)

Return the range of all elements from a through b, inclusive.

attributes

s.length

Number of elements in the structure

s.compare

Comparison function being used

s.elements

The raw elements. If you mess with these directly you risk invalidating the ordering.

array methods

These methods work exactly like in a regular array:

  • indexOf
  • forEach
  • reduce
  • reduceRight
  • every
  • some
  • join