Skip to content

Commit e5c0c9e

Browse files
🚧 progress: Import existing sources, tests, and code samples.
1 parent 170e1ee commit e5c0c9e

9 files changed

Lines changed: 10231 additions & 10 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
Item repetition for JavaScript.
55
See [docs](https://iterable-iterator.github.io/repeat/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-
107
> :warning: Depending on your environment, the code may require
118
> `regeneratorRuntime` to be defined, for instance by importing
129
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
1310
11+
```js
12+
import {repeat, nrepeat} from '@iterable-iterator/repeat';
13+
repeat("A"); // A A A ...
14+
nrepeat("A", 3); // A A A
15+
```
16+
1417
[![License](https://img.shields.io/github/license/iterable-iterator/repeat.svg)](https://raw.githubusercontent.com/iterable-iterator/repeat/main/LICENSE)
1518
[![Version](https://img.shields.io/npm/v/@iterable-iterator/repeat.svg)](https://www.npmjs.org/package/@iterable-iterator/repeat)
1619
[![Tests](https://img.shields.io/github/workflow/status/iterable-iterator/repeat/ci:test?event=push&label=tests)](https://github.com/iterable-iterator/repeat/actions/workflows/ci:test.yml?query=branch:main)

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
"@babel/preset-env": "7.13.15",
6868
"@babel/register": "7.13.16",
6969
"@commitlint/cli": "12.1.1",
70+
"@iterable-iterator/list": "^0.0.2",
71+
"@iterable-iterator/slice": "^0.0.1",
7072
"@js-library/commitlint-config": "0.0.4",
7173
"ava": "3.15.0",
7274
"babel-plugin-transform-remove-console": "6.9.4",

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 nrepeat} from './nrepeat.js';
2+
export {default as repeat} from './repeat.js';

src/nrepeat.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Repeats the input item a few times. Returns an iterator that yields the input
3+
* item a fixed number of times.
4+
*
5+
* @example
6+
* list( nrepeat( 6 , 3 ) ) ; // returns [ 6 , 6 , 6 ]
7+
*
8+
* @param {any} item - The input item.
9+
* @param {Number} times - The number of times to yield <code>item</code>.
10+
* @returns {IterableIterator}
11+
*/
12+
export default function* nrepeat(item, times) {
13+
while (times-- > 0) {
14+
yield item;
15+
}
16+
}

src/repeat.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Repeats the input item endlessly. Returns an iterator that yields the input
3+
* item over and over again.
4+
*
5+
* @example
6+
* list( take( repeat( 6 ) , 3 ) ) ; // returns [ 6 , 6 , 6 ]
7+
*
8+
* @param {Object} item - The input item.
9+
* @returns {Iterator}
10+
*/
11+
export default function* repeat(item) {
12+
while (true) {
13+
yield item;
14+
}
15+
}

test/src/api.js

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

test/src/nrepeat.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import test from 'ava';
2+
3+
import {list} from '@iterable-iterator/list';
4+
import {nrepeat} from '../../src/index.js';
5+
6+
test('nrepeat', (t) => {
7+
const x = function (item, times, expected) {
8+
t.deepEqual(list(nrepeat(item, times)), expected);
9+
};
10+
11+
for (const item of [
12+
0,
13+
1,
14+
-1,
15+
[],
16+
'A',
17+
'ABC',
18+
['A'],
19+
[0, 1, -1],
20+
['A', 'B', 'C'],
21+
]) {
22+
x(item, 0, []);
23+
x(item, 1, [item]);
24+
x(item, 2, [item, item]);
25+
x(item, 3, [item, item, item]);
26+
}
27+
});

test/src/repeat.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import test from 'ava';
2+
3+
import {list} from '@iterable-iterator/list';
4+
import {head} from '@iterable-iterator/slice';
5+
import {repeat} from '../../src/index.js';
6+
7+
test('repeat', (t) => {
8+
const x = function (item, times, expected) {
9+
t.deepEqual(list(head(repeat(item), times)), expected);
10+
};
11+
12+
for (const item of [
13+
0,
14+
1,
15+
-1,
16+
[],
17+
'A',
18+
'ABC',
19+
['A'],
20+
[0, 1, -1],
21+
['A', 'B', 'C'],
22+
]) {
23+
x(item, 0, []);
24+
x(item, 1, [item]);
25+
x(item, 2, [item, item]);
26+
x(item, 3, [item, item, item]);
27+
}
28+
});

0 commit comments

Comments
 (0)