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

@binaris/ts-comparators

v1.0.1

Published

A set of comparators written in TypeScript

Downloads

4

Readme

ts-comparators

This is a comparators package written in TypeScript

Build Status dependencies Status devDependencies Status codecov

Usage

Reversing

Reversing a comparator is as simple as calling its reverse method. The reverse method will return a new ChainableComparator that does the same thing as the original one in the reverse order.

let values = [2, 3, 1];
let comparator = new BasicComparator<number>().reverse();
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [3, 2, 1]

Chaining

Chaining comparators is what makes this package really powerful. You can combine multiple comparators to create subsorts in an efficient manner.

interface Obj {
    prop1: number,
    prop2: string
}
let values = [
    { prop1: 1, prop2: 'xylophone' },
    { prop1: 1, prop2: 'baseball' },
    { prop1: 2, prop2: 'hello' }
];
let comparator = new PropertyComparator<Obj, 'prop1'>('prop1', new BasicComparator<number>())
    .reverse()
    .then(new ValueComparator<Obj, string>(obj => obj.prop2, new StringComparator()));
values.sort((value1, value2) => comparator.compare(value1, value2));
//values = [,
//    { prop1: 2, prop2: 'hello' },
//    { prop1: 1, prop2: 'baseball' },
//    { prop1: 1, prop2: 'xylophone' }
//];

Comparators

ChainableComparator

The ChainableComparator is an abstract class that adds the ability to do chaining and reversing of the other comparators.

BasicComparator

The BasicComparator is the simplest of all the comparators and doesn't do anything special except that it can be used internally by other comparators and possesses the ChainableComparator functionality.

let values = [3, 2, 1];
let comparator = new BasicComparator<number>();
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [1, 2, 3]

StringComparator

The StringComparator is basically a wrapper around Intl.collator for it to work with the rest of the comparators in this package.

let values = ['one', 'two', 'three'];
let comparator = new StringComparator();
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = ['one', 'three', 'two']

PropertyComparator

The PropertyComparator is used to compare objects by a specific property in them. It takes the name of the property to use for the comparison as the first parameter and comparator to perform the comparison as the second parameter.

interface Obj {
    prop: number;
}
let values: Obj[] = [{prop: 2}, {prop: 3}, {prop: 1}];
let comparator = new PropertyComparator<Obj, 'prop'>('prop', new BasicComparator<number>());
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [{prop: 1}, {prop: 2}, {prop: 3}]

ValueComparator

The ValueComparator is one of the most powerful comparators because it allows you to transform the value prior to comparison or it can simply by used to pluck values out of an object. It takes function that transforms the value to use for the comparison as the first parameter and comparator to perform the comparison as the second parameter.

interface Obj {
    prop: number;
}
let values: Obj[] = [{prop: 2}, {prop: 3}, {prop: 1}];
let comparator = new ValueComparator<Obj, number>(obj => obj.prop, new BasicComparator<number>());
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [{prop: 1}, {prop: 2}, {prop: 3}]

CustomComparator

The CustomComparator is a utility comparator to convert a regular compare function in to a ChainableComparator.

let values = [2, 3, 1];
let comparator = new CustomComparator<number>((value1, value2) => value1 - value2);
values.sort((value1, value2) => comparator.compare(value1, value2));
// values = [1, 2, 3]