Skip to content

Commit 2d653fe

Browse files
🚧 progress: Import existing sources and tests.
1 parent 46eefa9 commit 2d653fe

File tree

6 files changed

+10185
-8
lines changed

6 files changed

+10185
-8
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@
5959
"release": "np --message ':hatching_chick: release: Bumping to v%s.'",
6060
"test": "ava"
6161
},
62-
"dependencies": {},
62+
"dependencies": {
63+
"@iterable-iterator/list": "^0.0.2"
64+
},
6365
"devDependencies": {
66+
"@aureooms/js-itertools": "^5.1.1",
6467
"@babel/core": "7.13.16",
6568
"@babel/preset-env": "7.13.15",
6669
"@babel/register": "7.13.16",
6770
"@commitlint/cli": "12.1.1",
6871
"@js-library/commitlint-config": "0.0.4",
72+
"@total-order/primitive": "^0.0.2",
6973
"ava": "3.15.0",
7074
"babel-plugin-transform-remove-console": "6.9.4",
7175
"babel-plugin-unassert": "3.0.1",

src/index.js

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

src/sorted.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {list} from '@iterable-iterator/list';
2+
3+
/**
4+
* Outputs an array containing the elements of the input iterable sorted
5+
* according to a given comparison function.
6+
*
7+
* @param {Function} compare - The comparison function to use. This function
8+
* must be 2-ary. It must return -1, 0, or 1 depending whether the first
9+
* parameter is, respectively, less than, equal to, or greater than the second
10+
* parameter.
11+
* @param {Iterable} iterable - The input iterable.
12+
* @returns {Array} - The input iterable, sorted.
13+
*/
14+
export default function sorted(compare, iterable) {
15+
return list(iterable).sort(compare);
16+
}

test/src/api.js

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

test/src/sorted.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import test from 'ava';
2+
3+
import {sorted} from '../../src/index.js';
4+
import * as primitive from '@total-order/primitive';
5+
import {tee} from '@aureooms/js-itertools';
6+
7+
const increasing = (a, b) => primitive.increasing(a, b);
8+
const decreasing = (a, b) => primitive.decreasing(a, b);
9+
10+
const macro = (t, compare, array) => {
11+
const [reference, input] = tee(array, 2);
12+
13+
const output = sorted(compare, input);
14+
const expected = Array.from(reference).sort(compare);
15+
16+
t.not(output, input, 'check that output is not the same object as input');
17+
t.is(output.length, expected.length, 'length check');
18+
t.deepEqual(output, expected, 'check that output is as expected');
19+
};
20+
21+
macro.title = (title, compare, array) =>
22+
title ?? `sorted(${compare.name}, ${JSON.stringify(array)})`;
23+
24+
for (const compare of [increasing, decreasing]) {
25+
const array = [];
26+
27+
const n = 100;
28+
29+
for (let i = n; i--; ) {
30+
array.push(Math.random());
31+
}
32+
33+
test(macro, compare, array);
34+
}

0 commit comments

Comments
 (0)