Skip to content

Commit

Permalink
🚧 progress: Import existing sources and tests from js-compare.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Apr 29, 2021
1 parent 49f0aba commit d1a91bb
Show file tree
Hide file tree
Showing 8 changed files with 10,023 additions and 26 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
Comparison function reversion for JavaScript.
See [docs](https://total-order.github.io/reversed/index.html).

> :building_construction: Caveat emptor! This is work in progress. Code may be
> working. Documentation may be present. Coherence may be. Maybe.
```js
import {increasing} from '@total-order/primitive';
import {reversed} from '@total-order/reversed';

> :warning: Depending on your environment, the code may require
> `regeneratorRuntime` to be defined, for instance by importing
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
let decreasing = reversed(increasing);
```

[![License](https://img.shields.io/github/license/total-order/reversed.svg)](https://raw.githubusercontent.com/total-order/reversed/main/LICENSE)
[![Version](https://img.shields.io/npm/v/@total-order/reversed.svg)](https://www.npmjs.org/package/@total-order/reversed)
Expand Down
17 changes: 3 additions & 14 deletions doc/manual/usage.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
# Usage

> :warning: Depending on your environment, the code may require
> `regeneratorRuntime` to be defined, for instance by importing
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
First, require the polyfill at the entry point of your application
```js
require( 'regenerator-runtime/runtime' ) ;
// or
import 'regenerator-runtime/runtime.js' ;
```

Then, import the library where needed
Import the library where needed
```js
const reversed = require( '@total-order/reversed' ) ;
const {reversed} = require( '@total-order/reversed' ) ;
// or
import * as reversed from '@total-order/reversed' ;
import {reversed} from '@total-order/reversed' ;
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@babel/register": "7.13.14",
"@commitlint/cli": "12.1.1",
"@js-library/commitlint-config": "0.0.4",
"@total-order/primitive": "^0.0.2",
"ava": "3.15.0",
"babel-plugin-transform-remove-console": "6.9.4",
"babel-plugin-unassert": "3.0.1",
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
const answer = 42;
export default answer;
export {default as reversed} from './reversed.js';
9 changes: 9 additions & 0 deletions src/reversed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Compile a new comparison function whose underlying total order is the
* reverse of the input comparison function's underlying total order.
*
* @param {Function} compare The comparison function to reverse.
* @return {Function} A function f such that compare(a,b) === f(b,a) for all a,b.
*/
const reversed = (compare) => (a, b) => compare(b, a);
export default reversed;
5 changes: 0 additions & 5 deletions test/src/api.js

This file was deleted.

33 changes: 33 additions & 0 deletions test/src/reversed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import test from 'ava';
import {increasing, decreasing} from '@total-order/primitive';
import {reversed} from '../../src/index.js';

const increasing2 = reversed(decreasing);
const decreasing2 = reversed(increasing);

const macro = (t, a, b) => {
const i = increasing(a, b);
const d = decreasing(a, b);
const i2 = increasing2(a, b);
const d2 = decreasing2(a, b);
const ri = increasing(b, a);
const rd = decreasing(b, a);
const ri2 = increasing2(b, a);
const rd2 = decreasing2(b, a);

t.deepEqual(i, i2, `i i2 (${a}, ${b})`);
t.deepEqual(-i, ri, `-i ri (${a}, ${b})`);
t.deepEqual(-i, ri2, `-i ri2 (${a}, ${b})`);
t.deepEqual(-i, d, `-i d (${a}, ${b})`);
t.deepEqual(-i, d2, `-i d2 (${a}, ${b})`);
t.deepEqual(i, rd, `i rd (${a}, ${b})`);
t.deepEqual(i, rd2, `i rd2 (${a}, ${b})`);
};

macro.title = (title, a, b) => title ?? `(${a}, ${b})`;

const random = () => Math.random() - 0.5;

for (let n = 0; n < 100; ++n) {
test(macro, random(), random());
}
9,971 changes: 9,971 additions & 0 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit d1a91bb

Please sign in to comment.