-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0abeaf9
commit b476299
Showing
6 changed files
with
81 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,88 @@ | ||
# Compear <sup>🍐</sup>🤷<sup>🍐</sup> | ||
|
||
_Flexible utilities for comparing and sorting anything in JavaScript_ | ||
_Flexible utilities for comparing and sorting in JavaScript_ | ||
|
||
Compear is inspired by Kotlin's [comparisons](kotlin.comparisons) package, but tailored for JavaScript. | ||
|
||
## Features | ||
|
||
* Create highly flexible sorting/comparing logic | ||
* First-class TypeScript support with strong type safety | ||
* Tiny source with zero dependencies | ||
**[Documentation](https://camsteffen.github.io/compear/index.html)** | ||
|
||
## Installation | ||
|
||
```shell | ||
npm install compear | ||
``` | ||
|
||
### Functions | ||
## What is it? | ||
|
||
Let's look at an example. | ||
Suppose you have a list of objects (`people`)... | ||
|
||
```javascript | ||
const people = [ | ||
{ name: "Tina" }, | ||
{ name: "Bob" }, | ||
{ name: "Lewis" }, | ||
]; | ||
``` | ||
|
||
...and you want to sort them by the `name` property. | ||
|
||
Without Compear: | ||
|
||
```javascript | ||
const peopleSorted = people.toSorted((personA, personB) => { | ||
if (personA.name < personB.name) return -1; | ||
if (personA.name > personB.name) return 1; | ||
return 0; | ||
}); | ||
``` | ||
|
||
With Compear: | ||
```javascript | ||
const peopleSorted = people.toSorted(compareBy((person) => person.name)); | ||
``` | ||
|
||
* [compareBy] | ||
Notice in both examples we are using the same `array.toSorted(..)` function that is built-in to JavaScript. | ||
The function that is passed as an argument to `array.toSorted(..)` is called a _comparator_. | ||
The `compareBy` function from Compear helps to create a comparator. | ||
Oftentimes `compareBy` is all you need, | ||
but there are other utils for more advanced cases. | ||
|
||
[compareBy]: localhost:1234 | ||
Compear takes some inspiration from Kotlin's [kotlin.comparisons](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.comparisons/) package, but is designed for JavaScript. | ||
|
||
#### `compareBy` | ||
Compear is written in TypeScript and provides strong type safety. | ||
|
||
asdfasdlk | ||
## Usage | ||
|
||
## Examples | ||
Here are some more examples of what you can do with Compear: | ||
|
||
```javascript | ||
import { compareBy, compareByDesc, compareWith, minWith } from "compear"; | ||
|
||
const heros = [ | ||
{ | ||
name: "Stupendous Man", | ||
favoriteColor: "red", | ||
}, | ||
{ | ||
name: "Frozone", | ||
age: 37, | ||
favoriteColor: "blue", | ||
}, | ||
{ | ||
name: "Stupendous Man", | ||
age: 9, | ||
favoriteColor: "red", | ||
}, | ||
{ | ||
name: "Iron Man", | ||
age: 44, | ||
favoriteColor: "gold", | ||
}, | ||
] | ||
``` | ||
|
||
[kotlin.comparisons]: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.comparisons/ | ||
|
||
|
||
]; | ||
|
||
const youngestHero = minWith(heros, compareBy("age")) | ||
|
||
const sortedByName = heros.sort(compareBy("name")); | ||
|
||
const sortedByNameAndGoldFirst = heros.toSorted( | ||
compareWith( | ||
compareByDesc("favoriteColor", (color) => color === "gold"), | ||
compareBy("name"), | ||
) | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
|
||
name orderly, ofsorts, mete, finical, scrutiny, compadre, sorta, minymoe, dico | ||
raedme | ||
drop equals? equals(anyObject, anyObject) == true :( | ||
structural equals? | ||
structural compare? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters