@utilityjs/comparator
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Comparator

A utility class that compares its comparable arguments.

license npm latest package npm downloads types

npm i @utilityjs/comparator | yarn add @utilityjs/comparator

Usage

interface Item {
  type: string;
  value: number;
}

const comparator = new Comparator((a, b) => {
  if (a.value === b.value) return 0;
  return a.value < b.value ? -1 : 1;
});

// TRUE
comparator.isEqual({ type: "a", value: 0},  { type: "b", value: 0});

API

Comparator(compareFunction?)

declare type CompareFunction<T> = (a: T, b: T) => -1 | 0 | 1;
declare class Comparator<T> {
  private compare;
  static defaultComparatorFunction: <U>(a: U, b: U) => -1 | 0 | 1;
  constructor(compareFunction?: CompareFunction<T>);
  isEqual(a: T, b: T): boolean;
  isLessThan(a: T, b: T): boolean;
  isLessThanOrEqual(a: T, b: T): boolean;
  isGreaterThan(a: T, b: T): boolean;
  isGreaterThanOrEqual(a: T, b: T): boolean;
}

Package Sidebar

Install

npm i @utilityjs/comparator

Weekly Downloads

36

Version

1.0.1

License

MIT

Unpacked Size

5.85 kB

Total Files

8

Last publish

Collaborators

  • mimshins