Skip to content

Commit 7f2267d

Browse files
🚧 progress: First draft.
1 parent b7e54cd commit 7f2267d

File tree

9 files changed

+9652
-13
lines changed

9 files changed

+9652
-13
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
Order validation for JavaScript.
55
See [docs](https://comparison-sorting.github.io/is-sorted/index.html).
66

7-
> :building_construction: Caveat emptor! This is work in progress. Code may be
8-
> working. Documentation may be present. Coherence may be. Maybe.
9-
10-
> :warning: Depending on your environment, the code may require
11-
> `regeneratorRuntime` to be defined, for instance by importing
12-
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
7+
```js
8+
import {isSorted} from '@comparison-sorting/is-sorted';
9+
import {increasing, decreasing} from '@total-order/primitive';
10+
isSorted(increasing, [1, 2, 3], 0, 3); // true
11+
isSorted(decreasing, [1, 2, 3], 0, 3); // false
12+
```
1313

1414
[![License](https://img.shields.io/github/license/comparison-sorting/is-sorted.svg)](https://raw.githubusercontent.com/comparison-sorting/is-sorted/main/LICENSE)
1515
[![Version](https://img.shields.io/npm/v/@comparison-sorting/is-sorted.svg)](https://www.npmjs.org/package/@comparison-sorting/is-sorted)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"@babel/register": "7.13.16",
7474
"@commitlint/cli": "12.1.4",
7575
"@js-library/commitlint-config": "0.0.4",
76+
"@total-order/primitive": "^1.0.1",
7677
"ava": "3.15.0",
7778
"babel-plugin-transform-remove-console": "6.9.4",
7879
"babel-plugin-unassert": "3.1.0",

src/firstInversion.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Returns k <= right such that [left,k[ is sorted. If k < right, then
3+
* compare( array[k-1] , array[k] ) > 0.
4+
*
5+
* @param {Function} compare
6+
* @param {ArrayLike} array
7+
* @param {number} left
8+
* @param {number} right
9+
* @return {number}
10+
*/
11+
export default function firstInversion(compare, array, left, right) {
12+
if (left >= right) return right;
13+
14+
while (++left < right) {
15+
if (compare(array[left - 1], array[left]) > 0) {
16+
break;
17+
}
18+
}
19+
20+
return left;
21+
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
const answer = 42;
2-
export default answer;
1+
export {default as firstInversion} from './firstInversion.js';
2+
export {default as isSorted} from './isSorted.js';

src/isSorted.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import firstInversion from './firstInversion.js';
2+
3+
/**
4+
* Returns whether range [left,right[ of array is sorted.
5+
*
6+
* @param {Function} compare
7+
* @param {ArrayLike} array
8+
* @param {number} left
9+
* @param {number} right
10+
* @return {boolean}
11+
*/
12+
export default function isSorted(compare, array, left, right) {
13+
return firstInversion(compare, array, left, right) === right;
14+
}

test/src/api.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/src/firstInversion.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import test from 'ava';
2+
3+
import {increasing, decreasing} from '@total-order/primitive';
4+
5+
import {firstInversion} from '../../src/index.js';
6+
7+
function macro(t, array, left, right, k1, k2) {
8+
const n = array.length;
9+
10+
t.is(k1, firstInversion(increasing, array, left, right));
11+
t.is(k2, firstInversion(decreasing, array, left, right));
12+
13+
t.is(n, array.length);
14+
}
15+
16+
macro.title = (_, ...args) => args.join(' , ');
17+
18+
test(macro, [], 0, 0, 0, 0);
19+
test(macro, [0, 1, 2], 1, 1, 1, 1);
20+
test(macro, [1, 1, 1], 0, 3, 3, 3);
21+
test(macro, [1, 2, 3], 0, 3, 3, 1);
22+
test(macro, [1, 2, 4, 3], 0, 4, 3, 1);
23+
test(macro, [1, 0, 1, 1, 2, 3, 1, 0, 1], 3, 6, 6, 4);
24+
test(macro, [1, 0, 1, 1, 2, 3, 1, 0, 1], 0, 9, 1, 2);

test/src/isSorted.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import test from 'ava';
2+
3+
import {increasing, decreasing} from '@total-order/primitive';
4+
5+
import {isSorted} from '../../src/index.js';
6+
7+
function macro(t, array, left, right, k1, k2) {
8+
const n = array.length;
9+
10+
t.is(k1, isSorted(increasing, array, left, right));
11+
t.is(k2, isSorted(decreasing, array, left, right));
12+
13+
t.is(n, array.length);
14+
}
15+
16+
macro.title = (_, ...args) => args.join(' , ');
17+
18+
test(macro, [], 0, 0, true, true);
19+
test(macro, [0, 1, 2], 1, 1, true, true);
20+
test(macro, [1, 1, 1], 0, 3, true, true);
21+
test(macro, [1, 2, 3], 0, 3, true, false);
22+
test(macro, [1, 2, 4, 3], 0, 4, false, false);
23+
test(macro, [1, 0, 1, 1, 2, 3, 1, 0, 1], 3, 6, true, false);
24+
test(macro, [1, 0, 1, 1, 2, 3, 1, 0, 1], 0, 9, false, false);

0 commit comments

Comments
 (0)