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

anysort

v2.0.0

Published

Sorting and matching utility using configurable string, glob, regular expression, and/or function matchers

Downloads

44,983

Readme

anysort Build Status Coverage Status

Javascript module to sort arrays of strings using flexible arrays of matchers. Regular expressions, globs, plain strings, or functions may be used as matchers (see anymatch).

NPM NPM

Usage

npm install anysort --save

anysort ([a, b,] [matchers])

Intended for use in an Array.sort callback. matchers is an array of anymatch compatible matchers.a and b are two values to be compared. If called with only matchers, returns a function (the Array.sort callback). If matchers is omitted, the array will be sorted naturally (alphabetically). Natural sort will also be used in case of a tie (multiple members matching the same matcher).

var anysort = require('anysort');

var unsorted = [
	'path/to/foo.js',
	'path/to/bar.js',
	'bar.js',
	'path/anyjs/baz.js',
	'path/anyjs/aaz.js',
	'path/to/file.js'
	'path/anyjs/caz.js',
];

var matchers = [
	'path/to/file.js',
	'path/anyjs/**/*.js',
	/foo.js$/,
	function (string) {
		return string.indexOf('bar') !== -1 && string.length > 10
	}
];

// the following two are equivalent
unsorted.sort(anysort(matchers));

unsorted.sort(function (a, b){
	// except there is an opportunity to run your own
	// operations/mutations on a and b here if needed
	return anysort(a, b, matchers);
});
/*
[ 'path/to/file.js',
	'path/anyjs/aaz.js',
	'path/anyjs/baz.js',
	'path/anyjs/caz.js',
	'path/to/foo.js',
	'path/to/bar.js',
	'bar.js' ]
*/

anysort.splice (list, [matchers], [tieBreakers])

Sorts the whole array. Returns an object with sorted, matched, and unmatched properties. matched is a sorted array of the list members that matched any of the matchers. unmatched is an array of the list members that didn't match any matchers, sorted natively. sorted is a concatenation of matched and unmatched. tieBreakers can optionally be specified as a second set of matchers which will not cause inclusion in the matched set, but will be used for fallback sorting in case of ties caused by multiple list array members matching the same matcher. tieBreakers must be an array.

anysort.splice(unsorted, matchers);
/*
{ matched:
	 [ 'path/to/file.js',
		 'path/anyjs/aaz.js',
		 'path/anyjs/baz.js',
		 'path/anyjs/caz.js',
		 'path/to/foo.js',
		 'path/to/bar.js' ],
	unmatched: [ 'bar.js' ],
	sorted:
	 [ 'path/to/file.js',
		 'path/anyjs/aaz.js',
		 'path/anyjs/baz.js',
		 'path/anyjs/caz.js',
		 'path/to/foo.js',
		 'path/to/bar.js',
		 'bar.js' ] }
*/

// quick access to just the sorted array
anysort.splice(unsorted, matchers).sorted;

anysort.grouped (list, [groupedMatchers], [order])

Allows use of an array of matcher arrays and arbitrary placement of the unmatched list members, which is useful if you want to define some to definitely go at the bottom. Also, can be used to create exclusion sets.

groupedMatchers should be put in order of priority (in case a list member might match multiple). Include the string 'unmatched' as a top-level member of groupedMatchers to set the position of any members that do not match any matchers, otherwise it is assumed to belong at the end. groupedMatchers also sets the order of results, unless an order array is defined to override it. If an order is provided that omits any of the indexes from groupedMatchers, the corresponding matches will be excluded from the output.

var before = /to/;
var after = ['path/anyjs/baz.js', 'path/anyjs/aaz.js'];
anysort.grouped(unsorted, [before, 'unmatched', after]);
/*
[ 'path/to/bar.js',
	'path/to/file.js',
	'path/to/foo.js',
	'bar.js',
	'path/anyjs/caz.js',
	'path/anyjs/baz.js',
	'path/anyjs/aaz.js' ]
*/

var exclusions = /anyjs/;
// 2 is the index for unmatched list members
anysort.grouped(unsorted, [exclusions, matchers], [2, 1]);
/*
[ 'bar.js',
	'path/to/file.js',
	'path/to/foo.js',
	'path/to/bar.js' ]
*/

Change Log

See release notes page on GitHub

License

ISC