Skip to content

Commit 50177a2

Browse files
✨ feat: Add reverse.
1 parent c065ca8 commit 50177a2

File tree

8 files changed

+9575
-27
lines changed

8 files changed

+9575
-27
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
ArrayLike reversing for JavaScript.
55
See [docs](https://array-like.github.io/reverse/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.
7+
```js
8+
import {reverse} from '@array-like/reverse';
99

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).
10+
const a = Int32Array.from([1, 2, 3]);
11+
a; 1 2 3
12+
reverse(a, 0, 3);
13+
a; 3 2 1
14+
reverse(a, 1, 3);
15+
a; 3 1 2
16+
```
1317

1418
[![License](https://img.shields.io/github/license/array-like/reverse.svg)](https://raw.githubusercontent.com/array-like/reverse/main/LICENSE)
1519
[![Version](https://img.shields.io/npm/v/@array-like/reverse.svg)](https://www.npmjs.org/package/@array-like/reverse)

doc/manual/usage.md

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
# Usage
22

3-
> :warning: Depending on your environment, the code may require
4-
> `regeneratorRuntime` to be defined, for instance by importing
5-
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
6-
7-
First, require the polyfill at the entry point of your application
8-
```js
9-
await import('regenerator-runtime/runtime.js');
10-
// or
11-
import 'regenerator-runtime/runtime.js';
12-
```
13-
14-
Then, import the library where needed
3+
Import the library where needed
154
```js
16-
const reverse = await import('@array-like/reverse');
5+
const {reverse} = await import('@array-like/reverse');
176
// or
18-
import * as reverse from '@array-like/reverse';
7+
import {reverse} from '@array-like/reverse';
198
```

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@
5959
"release": "np --message ':hatching_chick: release: Bumping to v%s.'",
6060
"test": "ava"
6161
},
62-
"dependencies": {},
62+
"dependencies": {
63+
"@array-like/swap": "^0.0.1"
64+
},
6365
"devDependencies": {
66+
"@array-like/alloc": "^0.0.1",
67+
"@array-like/copy": "^0.0.1",
6468
"@babel/core": "7.14.8",
6569
"@babel/preset-env": "7.14.8",
6670
"@babel/register": "7.14.5",

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 reverse} from './reverse.js';

src/reverse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {swap} from '@array-like/swap';
2+
3+
/**
4+
* Reverse.
5+
*
6+
* @param {{[x:number]: any}} a
7+
* @param {number} i
8+
* @param {number} j
9+
*/
10+
const reverse = (a, i, j) => {
11+
while (i < j) swap(a, i++, --j);
12+
};
13+
14+
export default reverse;

test/src/api.js

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

test/src/reverse.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import test from 'ava';
2+
3+
import {alloc} from '@array-like/alloc';
4+
import {copy} from '@array-like/copy';
5+
6+
import {reverse} from '../../src/index.js';
7+
8+
const macro = (t, a, i, j, expected) => {
9+
const reference = alloc(a.length);
10+
copy(a, 0, a.length, reference, 0);
11+
12+
reverse(a, i, j);
13+
t.deepEqual(a, expected, 'reverse x 1');
14+
15+
reverse(a, i, j);
16+
t.deepEqual(a, reference, 'reverse x 2');
17+
};
18+
19+
const repr = JSON.stringify;
20+
21+
macro.title = (title, a, i, j, expected) =>
22+
title ?? `reverse(${repr(a)}, ${i}, ${j}) is ${repr(expected)}`;
23+
24+
const immutable = (t, a, i, j) => macro(t, a, i, j, a.slice());
25+
immutable.title = (title, a, i, j) => macro.title(title, a, i, j, a.slice());
26+
27+
const whole = (t, a, expected) => macro(t, a, 0, a.length, expected);
28+
whole.title = (title, a, expected) =>
29+
macro.title(title, a, 0, a.length, expected);
30+
31+
test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 4, 4);
32+
test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 5);
33+
test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 1);
34+
test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0, -1);
35+
test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 1, 0);
36+
test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 9, 0);
37+
test(immutable, [1, 2, 3, 4, 5, 6, 7, 8, 9], 27, -32);
38+
39+
test(whole, [], []);
40+
test(whole, [1], [1]);
41+
test(whole, [1, 2, 3], [3, 2, 1]);
42+
test(whole, [1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1]);
43+
test(whole, [1, 2, 3, 4, 5, 6, 7, 8, 9], [9, 8, 7, 6, 5, 4, 3, 2, 1]);
44+
45+
test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 7, [1, 2, 3, 7, 6, 5, 4, 8, 9]);
46+
test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 8, [1, 2, 3, 8, 7, 6, 5, 4, 9]);
47+
test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 9, [1, 2, 3, 9, 8, 7, 6, 5, 4]);
48+
test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 6, [6, 5, 4, 3, 2, 1, 7, 8, 9]);
49+
test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 5, [5, 4, 3, 2, 1, 6, 7, 8, 9]);
50+
test(macro, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 4, [4, 3, 2, 1, 5, 6, 7, 8, 9]);

0 commit comments

Comments
 (0)