Skip to content

Commit f36afde

Browse files
committed
Distribute package in ES module format exclusively
Changes the default module format of this package to ES module. Import it using `import getReverseIteratingArray from 'get-reverse-iterating-array'`. Removes the `get-reverse-iterating-array/dist/cjs/get-reverse-iterating-array.js` and `get-reverse-iterating-array/dist/esm/get-reverse-iterating-array.mjs` module specifiers. Migrates test suite from Ava to Jest. Introduces Rollup for bundling.
1 parent ad074e6 commit f36afde

14 files changed

+5099
-5797
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
node_modules
2-
.nyc_output
2+
coverage

README.md

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# get-reverse-iterating-array
22

3-
This function reverses an array’s default iteration direction. By default, the [iteration protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) are defined to iterate on an iterable object’s elements according to their insertion order.
3+
A function returning an array whose iteration direction is reversed without affecting the original array or affecting the iteration direction of any other arrays.
44

5-
The function `getReverseIteratingArray` returns a new [`Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) object with the array as its target. The array’s `[Symbol.iterator]` property is trapped in order to define custom iteration behaviour. This has the advantage of not changing the implementation of [`Array.prototype[Symbol.iterator]`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator) directly which would cause the iteration direction of _all_ arrays to be reversed. Using a proxy also avoids unnecessarily copying an array for reverse iteration like [`Array.prototype.reverse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse) would.
5+
By default, the [iteration protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) are defined to iterate on an iterable object’s elements according to their insertion order.
6+
7+
The function `getReverseIteratingArray` returns a new [`Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) object with the array being its target. The array’s `[Symbol.iterator]` property is trapped in order to define custom iteration behaviour. This has the advantage of not changing the implementation of [`Array.prototype[Symbol.iterator]`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator) directly which would cause the iteration direction of _all_ arrays to be reversed. Using a proxy also avoids unnecessarily copying an array for reverse iteration like [`Array.prototype.reverse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse) would.
68

79
If you require to iterate over an array in _both_ forwards and backwards direction, have a look at [`ReverseIterableArray`](https://www.npmjs.com/package/reverse-iterable-array).
810

@@ -21,73 +23,40 @@ See also:
2123

2224
## Table of contents
2325

24-
- [Installation & usage](#installation--usage)
26+
- [Installation](#installation)
27+
- [Usage](#usage)
2528
- [Examples](#examples)
2629
- [Tests](#tests)
2730
- [Documentation](#documentation)
2831

2932

3033

31-
## Installation & usage
32-
33-
### Browser
34-
35-
Download the ES module file …
34+
## Installation
3635

3736
```sh
38-
curl -O https://raw.githubusercontent.com/kleinfreund/get-reverse-iterating-array/main/dist/esm/get-reverse-iterating-array.mjs
37+
npm install get-reverse-iterating-array
3938
```
4039

41-
… and import it like this:
4240

43-
```js
44-
import getReverseIteratingArray from 'get-reverse-iterating-array.mjs';
45-
46-
const array = getReverseIteratingArray([1, 2, 3]);
47-
```
4841

49-
### Node
42+
## Usage
5043

51-
Install the node package as a dependency …
44+
```js
45+
import getReverseIteratingArray from 'get-reverse-iterating-array';
5246

53-
```sh
54-
npm install --save get-reverse-iterating-array
47+
const array = getReverseIteratingArray([1, 2, 3]);
5548
```
5649

57-
… and import it like this:
58-
59-
- CommonJS module
60-
61-
```node
62-
const getReverseIteratingArray = require('get-reverse-iterating-array').default;
63-
64-
const array = getReverseIteratingArray([1, 2, 3]);
65-
```
66-
67-
- ES module
68-
69-
```js
70-
import getReverseIteratingArray from 'get-reverse-iterating-array/dist/esm/get-reverse-iterating-array.mjs';
71-
72-
const array = getReverseIteratingArray([1, 2, 3]);
73-
```
74-
75-
- TypeScript module
76-
77-
```ts
78-
import getReverseIteratingArray from 'get-reverse-iterating-array/src/get-reverse-iterating-array';
79-
80-
const array = getReverseIteratingArray([1, 2, 3]);
81-
```
82-
8350

8451

8552
## Examples
8653

8754
For some live usage examples, clone the repository and run the following:
8855

8956
```sh
90-
npm install && npm run examples
57+
npm install
58+
npm run build
59+
npm start
9160
```
9261

9362
Then, open [localhost:8080/examples](http://127.0.0.1:8080/examples) in a browser.
@@ -99,7 +68,8 @@ Then, open [localhost:8080/examples](http://127.0.0.1:8080/examples) in a browse
9968
In order to run the tests, clone the repository and run the following:
10069

10170
```sh
102-
npm install && npm test
71+
npm install
72+
npm test
10373
```
10474

10575

dist/cjs/get-reverse-iterating-array.js

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

dist/esm/get-reverse-iterating-array.mjs

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

dist/get-reverse-iterating-array.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/examples.mjs renamed to examples/examples.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import getReverseIteratingArray from '../dist/esm/get-reverse-iterating-array.mjs';
1+
import getReverseIteratingArray from '../dist/get-reverse-iterating-array.js';
2+
3+
Object.defineProperty(window, 'getReverseIteratingArray', { value: getReverseIteratingArray });
24

35
/**
4-
* @param {String} command
6+
* @param {string} command
57
*/
68
function printCommand(command) {
79
printCodeBlock(command, 'command');
810
}
911

1012
/**
11-
* @param {Array} args
13+
* @param {any[]} args
1214
*/
1315
function printOutput(...args) {
14-
const output = args.map(arg => stringify(arg));
16+
const output = args.map((arg) => stringify(arg));
1517
printCodeBlock(output.join(' '), 'output');
1618
}
1719

@@ -47,7 +49,7 @@ function printCodeBlock(content, ...classNames) {
4749
*/
4850
function stringify(input) {
4951
if (Array.isArray(input)) {
50-
return `[ ${input.map(element => stringify(element)).join(', ')} ]`;
52+
return `[ ${input.map((element) => stringify(element)).join(', ')} ]`;
5153
} else if (typeof input === 'string') {
5254
return `"${input}"`;
5355
}
@@ -59,23 +61,22 @@ function printExamples() {
5961
printCommand('const regularIteratingArray = [5, 4, 1, 2, 3];')
6062
const regularIteratingArray = [5, 4, 1, 2, 3];
6163
printOutput(regularIteratingArray);
64+
printCommand('Array.from(regularIteratingArray)');
65+
printOutput(Array.from(regularIteratingArray));
6266

6367
printCommand('const reverseIteratingArray = getReverseIteratingArray([5, 4, 1, 2, 3]);')
6468
const reverseIteratingArray = getReverseIteratingArray([5, 4, 1, 2, 3]);
6569
printOutput(reverseIteratingArray);
66-
67-
printCommand('Array.from(regularIteratingArray)');
68-
printOutput(Array.from(regularIteratingArray));
6970
printCommand('Array.from(reverseIteratingArray)');
7071
printOutput(Array.from(reverseIteratingArray));
7172

7273
printCommand('regularIteratingArray.sort((a, b) => b - a)');
7374
printOutput(regularIteratingArray.sort((a, b) => b - a));
74-
printCommand('reverseIteratingArray.sort((a, b) => b - a)');
75-
printOutput(reverseIteratingArray.sort((a, b) => b - a));
76-
7775
printCommand('Array.from(regularIteratingArray)');
7876
printOutput(Array.from(regularIteratingArray));
77+
78+
printCommand('reverseIteratingArray.sort((a, b) => b - a)');
79+
printOutput(reverseIteratingArray.sort((a, b) => b - a));
7980
printCommand('Array.from(reverseIteratingArray)');
8081
printOutput(Array.from(reverseIteratingArray));
8182
}

examples/index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1.0">
67

78
<title>getReverseIteratingArray Examples</title>
89

9-
<script type="module" src="examples.mjs"></script>
10-
1110
<style>
1211
body {
1312
font-size: 1.25em;
@@ -38,7 +37,7 @@
3837
.command code:not(:first-child)::before,
3938
.output code:not(:first-child)::before,
4039
.log code::before {
41-
content: "\A0" "\A0";
40+
content: " ";
4241
}
4342

4443
.command {
@@ -58,6 +57,7 @@
5857
}
5958
</style>
6059
</head>
60+
6161
<body>
6262

6363
<p>Hey, beautiful. :)</p>
@@ -77,5 +77,8 @@
7777

7878
<hr>
7979

80+
<script type="module" src="examples.js"></script>
81+
8082
</body>
83+
8184
</html>

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('@jest/types').Config.InitialOptions} */ const config = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'jsdom',
4+
collectCoverage: true,
5+
collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**'],
6+
}
7+
8+
export default config

0 commit comments

Comments
 (0)