JSPM

cmpby

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 23
  • Score
    100M100P100Q48728F
  • License MIT

Create comparator from key or less function

Package Exports

  • cmpby

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cmpby) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

cmpby Build Status Dependency Status

npm

Make comparator from key function, less function, or both.

Comparator is a function that given two arguments a and b returns a number that is:

  • negative if a precedes b;
  • zero if a and b are effectively equal;
  • positive if b precedes a.

Comparators are used in such functions as Array.prototype.sort to set up object ordering.

Unfortunately, it is not very pleasant to define such functions, hence this module.

Example

> ['aaa', 'zz', 'd'].sort(cmpby({
    less: function (a, b) { return a.length < b.length; }
  }))
[ 'd', 'zz', 'aaa' ]

API

cmpby([keyfn], [options])

keyfn maps objects to keys.

options.less maps pairs of objects to boolean values.

options.asc controls in what order does comparator order objects (ascending or descending).

Type Required? Default
keyfn obj -> key No identity
options.less (key1, key2) -> boolean No operator
options.asc boolean No true

Install

npm install cmpby

License

MIT