What is @thi.ng/compare?
@thi.ng/compare is a utility library for various comparison operations in JavaScript. It provides a collection of comparator functions for different data types and use cases, making it easier to perform sorting, equality checks, and other comparison-based operations.
What are @thi.ng/compare's main functionalities?
Basic Comparators
Provides basic comparator functions for general use, including ascending and descending numerical comparisons.
const { compare, compareNumAsc, compareNumDesc } = require('@thi.ng/compare');
console.log(compare(5, 10)); // -1
console.log(compareNumAsc(5, 10)); // -1
console.log(compareNumDesc(5, 10)); // 1
String Comparators
Includes specialized comparators for string values, with options for case-sensitive and case-insensitive comparisons.
const { compareStr, compareStrCaseInsensitive } = require('@thi.ng/compare');
console.log(compareStr('apple', 'banana')); // -1
console.log(compareStrCaseInsensitive('Apple', 'apple')); // 0
Array Comparators
Allows for comparison of arrays, element by element, to determine their order or equality.
const { compareArrays } = require('@thi.ng/compare');
console.log(compareArrays([1, 2, 3], [1, 2, 4])); // -1
console.log(compareArrays([1, 2, 3], [1, 2, 3])); // 0
Object Comparators
Provides comparators for objects based on specific keys, useful for sorting arrays of objects.
const { compareByKey } = require('@thi.ng/compare');
const obj1 = { id: 1, name: 'Alice' };
const obj2 = { id: 2, name: 'Bob' };
console.log(compareByKey('id')(obj1, obj2)); // -1
Other packages similar to @thi.ng/compare
lodash
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including comparison operations. It offers functions like _.isEqual for deep equality checks and _.sortBy for sorting collections. Compared to @thi.ng/compare, Lodash is more comprehensive but also larger in size.
fast-deep-equal
fast-deep-equal is a small and fast library for deep equality checks. It is highly optimized for performance and is often used when deep comparison of objects or arrays is needed. Unlike @thi.ng/compare, it focuses solely on deep equality and does not provide other comparison utilities.
ramda
Ramda is a functional programming library for JavaScript that includes a variety of utility functions, including comparison functions like R.equals and R.sort. It emphasizes immutability and functional programming principles. While it offers similar comparison functionalities, it is part of a broader functional programming toolkit.
[!NOTE]
This is one of 189 standalone projects, maintained as part
of the @thi.ng/umbrella monorepo
and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me on
GitHub. Thank you! ❤️
About
Comparators with optional support for types implementing the @thi.ng/api
ICompare
interface.
Generic comparison
Additional comparators
Operators
Status
STABLE - used in production
Search or submit any issues for this package
Installation
yarn add @thi.ng/compare
ESM import:
import * as cmp from "@thi.ng/compare";
Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/compare"></script>
JSDelivr documentation
For Node.js REPL:
const cmp = await import("@thi.ng/compare");
Package sizes (brotli'd, pre-treeshake): ESM: 629 bytes
Dependencies
Usage examples
Four projects in this repo's
/examples
directory are using this package:
Screenshot | Description | Live demo | Source |
---|
| Color palette generation via dominant color extraction from uploaded images | Demo | Source |
| Piechart visualization of CSV data | Demo | Source |
| Full umbrella repo doc string search w/ paginated results | Demo | Source |
| Triple store query results & sortable table | Demo | Source |
API
Generated API docs
ICompare support
import { ICompare } from "@thi.ng/api";
import { compare } from "@thi.ng/compare";
class Foo implements ICompare<Foo> {
x: number;
constructor(x: number) {
this.x = x;
}
compare(o: Foo) {
return compare(this.x, o.x);
}
}
compare(new Foo(1), new Foo(2));
Cluster sort w/ multiple sort keys
Key-based object comparison is supported for 1 - 4 keys / dimensions.
import * as cmp from "@thi.ng/compare";
const src = [
{ id: "charlie", age: 66 },
{ id: "bart", age: 42 },
{ id: "alice", age: 23 },
{ id: "dora", age: 11 },
];
console.log(
[...src].sort(cmp.compareByKeys2("id", "age"))
);
console.log(
[...src].sort(cmp.compareByKeys2("age", "id"))
);
console.log(
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compareNumDesc))
);
console.log(
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compare, cmp.reverse(cmp.compare)))
);
Authors
If this project contributes to an academic publication, please cite it as:
@misc{thing-compare,
title = "@thi.ng/compare",
author = "Karsten Schmidt",
note = "https://thi.ng/compare",
year = 2016
}
License
© 2016 - 2024 Karsten Schmidt // Apache License 2.0