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-array-async

v0.0.7

Published

an async self sorting array

Downloads

368

Readme

SortedArrayAsync

Installation

npm install --save sorted-array-async

Description

An asynchronous, lazy, self sorting array. Use it to keep things in order. Like me, the SortedArray object is lazy, meaning it won't sort until it absolutely has to. This will allow you to throw all of your junk into the array and then worry about sorting last minute. This might help your performance, but it also might not, that's for you to figure out.

Like the name would imply, the important functions are async through the use of Promises. See usage below if you need clarification on what that means or how to use it.

API

The API contains the basic functions you would see in any other array, the difference being that when you try to retrieve objects they will have been sorted.

require

var SortedArray = require('sorted-array-async');

functions

| Function | Description | Returns | | -------------- | ------------- | :--------: | |new SortedArray(array, comparer);| Constructor. Can pass existing array and custom comparator. comparer is type function(a, b). It should return a number. Less than 0 if a < b, 0 if a == b, or greater than 0 if a > b | A new instance of SortedArray | |SortedArray.push(item);| Add a new item to the array. | Promise(void) | |SortedArray.pushSync(item);| Add a new item to the array synchronously. | void | |SortedArray.set(index, item);| Sets the specific index to an item.| Promise(void); | |SortedArray.length();| Get the current number of elements in the array.| int | |SortedArray.clear();| Deletes all items in the array.| void | |SortedArray.get(index);| Get the item at the specific index. Triggers sorting.| Promise(item) | |SortedArray.getArray();| Gets a sorted copy of the inner array. Triggers sorting.| Promise(Array) | |SortedArray.pop(count);| Pops last n elements off of array. count is optional, leaving it blank pops 1. Triggers sorting.| Promise(Array) or Promise(obj) | |SortedArray.last(count);| Gets the last element from array. Triggers sorting.| Promise(obj) |

Usage

Basic


var SortedArray = require('sorted-array-async');

var instance = new SortedArray();

instance.push(56).then(function(){
	//Maybe you want to wait until the item was added before doing something??

	instance.get(0).then(function(item){
		//item is the value at index 0;
	});

});

Synchronous Push


var SortedArray = require('sorted-array-async');

var instance = new SortedArray();

instance.pushSync(90);
instance.pushSync(83);
instance.pushSync(2);


instance.get(0).then(function(item){
	//...
});

With Objects and Custom Comparer


var SortedArray = require('sorted-array-async');

var instance = new SortedArray(null, function(a, b){
	return a.val - b.val;
});

var promises = [];
promises.push(instance.push({ name: 'foo', val: 15}));
promises.push(instance.push({ name: 'bar', val: 10}));
promises.push(instance.push({ name: 'baz', val: 11}));

Promise.all(promises).then(function(){
  instance.get(0).then(function(item){
  	 //remember the array gets sorted so item
     //at the 0th index is { name: 'bar', val: 10 }
  });
});

Use It to Sort an Existing Array!


var SortedArray = require('sorted-array-async');

var uglyUnsortedArray = [5,4,8,9,1];

var instance = new SortedArray(uglyUnsortedArray);

var shinySortedArray;

instance.getArray().then(function(arr){
	//arr is your array but sorted.
    //it is a new array so your uglyUnsortedArray remains the same.
    shinySortedArray = arr;

    //shinySortedArray = [1,4,5,8,9]
});

Pop a Few Items


var SortedArray = require('sorted-array-async');

var instance = new SortedArray([3,2,0,1,6,7,9,8]);

instance.pop(3).then(function(arr){
	//arr == [7,8,9]

    instance.pop().then(function(i){
   		//i == 6;
    });
});

for live examples click here

RoadMap

  • Add synchronous get and getArray functions. (it kind of defeats the purpose of an async sorted array but might be needed in certain cases).
  • Add dequeue and first functions.
  • Become a dependency of many large projects.
  • Angrily remove package from NPM causing widespread havoc.
  • ?????
  • Profit!!! ($$$$$$$$$$$$$$$$$)