Git Product home page Git Product logo

comparators.js's Introduction

Comparators.js

Java8-style chainable comparators for Javascript

Raison d'être

In working with JS, I've run across situations where I need multi-key sorting in my Backbone collections -- which can sort themselves using the same type of comparator function as Array.prototype.sort expects.

When Java 8 rolled out, it brought with it some useful enhancements to the existing Comparator interface to allow chaining comparators for multi-attribute sort like this:

people.sort(
    Comparator.comparing(Person::getLastName)
        .thenComparing(Person::getFirstName)
);

I really liked that approach, so I brought that into Javascript with the same(ish) syntax. That's this library.

Usage and examples

Directly translating our above Java 8 code, we could do the following:

/* Demo data */
var people = [
  {lastName: "Baggins", firstName: "Frodo"},
  {lastName: "Gamgee",  firstName: "Samwise"},
  {lastName: "Baggins", firstName: "Bilbo"}
];

sortedPeople = people.sort(Comparators.comparing("lastName").thenComparing("firstName"));

/* sortedPeople is now:
[
  {lastName: "Baggins", firstName: "Bilbo"},
  {lastName: "Baggins", firstName: "Frodo"},
  {lastName: "Gamgee",  firstName: "Samwise"},
]; 
*/

For more examples, see the tests in test/comparators.spec.js.

Where and how can I use it?

It works in the browser without a module system, as a CommonJS module, and as an AMD module.

The simplest (but global-namespace-polluting) way to use it is to include comparators.js in a script tag:

<script type="text/javascript" src="comparators.min.js"></script>

In node/CommonJS loaders, just require it (it's available on NPM as comparators):

var Comparators = require("comparators").default; // the .default is necessary because of how Typescript compiles to commonjs

It works similarly in AMD loaders (require.js used in the below example):

require(['comparators.min'], function(Comparators){
  /* Do a thing! */
});

License

Comparators.js is made available under the MIT License (Quick summary of it here)

comparators.js's People

Contributors

dependabot[bot] avatar jared314 avatar spencerwi avatar squiddle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

comparators.js's Issues

Warning when importing via webpack

I'm getting this warning for a while in my project:

client:148 ./node_modules/comparators/dist/comparators.js 3:24-31
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

I love your lib a lot, it helps me to write way prettier code. Is there any reason or fix for this message?

I'm importing via webpack, babel and npm :)

Nitpick

I like this, and thank you for implementing something like this. More than a handful of times I have rolled my own comparisons (usually by doing bit-shifting for precedence - the more left-shifted the comparison value, the more important it was and adding the values up). This way is far more readable and would have made it easier for less senior team members understand and troubleshoot the logic more easily.

I do want to nitpick the Readme a bit. It mentions:

When Java 8 rolled out, it brought with it a Comparator interface...

I suggest slightly changing this to

When Java 8 rolled out, it brought with it several enhancements to the Comparator interface...

While Java 8 did bring a lot of enhancements to java.util.Comparator, the interface has existed since (at least) Java 1.2 (this is the earliest reference I could find). As I said, just a nit, not a huge issue.

The fact that you made roughly equivalent code in uncompressed, unminified 2.2kb is damn impressive, with zero dependencies to boot. I will be keeping this in my tool-belt when I have some complex sorting I need to do in a front end in a future project.

Again, thank you for this and great work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.