Skip to content

Commit

Permalink
♻️ refactor: Use default exports everywhere.
Browse files Browse the repository at this point in the history
Fix some function signatures, remove some circular dependencies.
  • Loading branch information
make-github-pseudonymous-again committed Mar 18, 2021
1 parent 66fc104 commit b63b59f
Show file tree
Hide file tree
Showing 73 changed files with 358 additions and 274 deletions.
2 changes: 1 addition & 1 deletion src/base/StopIteration.js
Expand Up @@ -2,4 +2,4 @@
* Error thrown by {@link next} when the input iterator is exhausted.
* @class
*/
export {StopIteration} from '@aureooms/js-error';
export {StopIteration as default} from '@aureooms/js-error';
4 changes: 2 additions & 2 deletions src/base/_count.js
Expand Up @@ -12,9 +12,9 @@
*
* @param {Number} start - The starting value.
* @param {Number} step - The step between the values.
* @returns {Iterator}
* @returns {IterableIterator}
*/
export function* _count(start, step) {
export default function* _count(start, step) {
while (true) {
yield start;

Expand Down
21 changes: 21 additions & 0 deletions src/base/_drop.js
@@ -0,0 +1,21 @@
import assert from 'assert';
import iter from './iter.js';
import consume from './consume.js';

/**
* Drops the first <code>n</code> values of the input iterable.
*
* @example
* // returns [ 3 , 4 ]
* list( _drop( range( 5 ) , 3 ) ) ;
*
* @param {Iterable} iterable - The input iterable.
* @param {Number} n - The nonnegative number of values to drop.
* @returns {IterableIterator} - The remaining values of the input iterable.
*/
export default function* _drop(iterable, n) {
assert(Number.isInteger(n) && n >= 0);
const iterator = iter(iterable);
consume(iterator, n);
yield* iterator;
}
2 changes: 1 addition & 1 deletion src/base/_next.js
Expand Up @@ -4,6 +4,6 @@
* @param {Iterator} iterator - The input iterator.
* @returns {{done: Boolean, value: Object}}
*/
export function _next(iterator) {
export default function _next(iterator) {
return iterator.next();
}
4 changes: 2 additions & 2 deletions src/base/_range.js
Expand Up @@ -6,9 +6,9 @@
* @param {Number} start - The starting value.
* @param {Number} stop - The stopping value.
* @param {Number} step - The step value.
* @returns {Iterator}
* @returns {IterableIterator}
*/
export function* _range(start, stop, step) {
export default function* _range(start, stop, step) {
if (step < 0) {
for (; start > stop; start += step) {
yield start;
Expand Down
18 changes: 18 additions & 0 deletions src/base/_tail.js
@@ -0,0 +1,18 @@
import assert from 'assert';
import deque from '@aureooms/js-collections-deque';

/**
* Returns the last <code>n</code> values of the input iterable in an array.
*
* @example
* // returns [ 3 , 4 ]
* list( _tail( range( 5 ) , 2 ) ) ;
*
* @param {Iterable} iterable - The input iterable.
* @param {Number} n - The nonnegative number of values to include in the output.
* @returns {IterableIterator} - The last <code>n</code> values of the input iterable.
*/
export default function* _tail(iterable, n) {
assert(Number.isInteger(n) && n >= 0);
yield* deque(iterable, n);
}
29 changes: 29 additions & 0 deletions src/base/_take.js
@@ -0,0 +1,29 @@
import assert from 'assert';
import iter from './iter.js';

/**
* Yields the first <code>n</code> elements of the input iterable.
*
* @example
* // returns [ 0 , 1 , 2 ]
* list( _take( range( 5 ) , 3 ) ) ;
*
* @param {Iterable} iterable - The input iterable.
* @param {Number} n - The nonnegative number of elements to include in the output.
* @returns {IterableIterator} - The first <code>n</code> elements of the input iterable.
*/
export default function* _take(iterable, n) {
assert(Number.isInteger(n) && n >= 0);

const iterator = iter(iterable);

while (n-- > 0) {
const current = iterator.next();

if (current.done) {
return;
}

yield current.value;
}
}
42 changes: 42 additions & 0 deletions src/base/_trunc.js
@@ -0,0 +1,42 @@
import assert from 'assert';
import deque from '@aureooms/js-collections-deque';

import iter from './iter.js';

/**
* Yields all elements of the iterable except the last <code>n</code> ones.
*
* @example
* // returns [ 0 , 1 , 2 ]
* list( _trunc( range( 5 ) , 2 ) ) ;
*
* @param {Iterable} iterable - The input iterable.
* @param {Number} n - The nonnegative number of elements to exclude from the output.
* @returns {IterableIterator}
*/
export default function* _trunc(iterable, n) {
assert(Number.isInteger(n) && n >= 0);

if (n === 0) {
yield* iterable;
return;
}

const iterator = iter(iterable);

const buffer = deque(null, n);

while (n-- > 0) {
const event = iterator.next();
if (event.done) {
return;
}

buffer.append(event.value);
}

for (const value of iterator) {
yield buffer.popleft();
buffer.append(value);
}
}
6 changes: 3 additions & 3 deletions src/base/by.js
@@ -1,5 +1,5 @@
import {iter} from './iter.js';
import {range} from './range.js';
import iter from './iter.js';
import range from './range.js';

/**
* Yields elements of the input iterable by grouping them into tuples of a
Expand All @@ -9,7 +9,7 @@ import {range} from './range.js';
* @param {Number} n - The size of the yielded tuples.
* @returns {Iterator}
*/
export function* by(iterable, n) {
export default function* by(iterable, n) {
const iterator = iter(iterable);

while (true) {
Expand Down
2 changes: 1 addition & 1 deletion src/base/consume.js
Expand Up @@ -5,7 +5,7 @@
* @param {Number} n - The number of iterations to consume.
*
*/
export function consume(iterator, n) {
export default function consume(iterator, n) {
// eslint-disable-next-line no-empty
while (n-- > 0 && !iterator.next().done) {}
}
4 changes: 2 additions & 2 deletions src/base/count.js
@@ -1,4 +1,4 @@
import {_count} from './_count.js';
import _count from './_count.js';

/**
* Yields increasing or decreasing sequences of numbers. The starting value
Expand All @@ -18,6 +18,6 @@ import {_count} from './_count.js';
* @param {Number} [step=1] - The step between the values.
* @returns {Iterator}
*/
export function count(start = 0, step = 1) {
export default function count(start = 0, step = 1) {
return _count(start, step);
}
4 changes: 2 additions & 2 deletions src/base/cycle.js
Expand Up @@ -6,10 +6,10 @@
* list(head(cycle(range(2)),7)) ;
*
* @param {Iterable} iterable - The input iterable.
* @returns {Iterator}
* @returns {IterableIterator}
*
*/
export function* cycle(iterable) {
export default function* cycle(iterable) {
const buffer = [];

for (const item of iterable) {
Expand Down
18 changes: 5 additions & 13 deletions src/base/drop.js
@@ -1,6 +1,5 @@
import {iter} from './iter.js';
import {consume} from './consume.js';
import {tail} from './tail.js';
import _drop from './_drop.js';
import _tail from './_tail.js';

/**
* Drops the first <code>n</code> values of the input iterable.
Expand All @@ -13,15 +12,8 @@ import {tail} from './tail.js';
*
* @param {Iterable} iterable - The input iterable.
* @param {Number} n - The number of values to drop.
* @returns {Iterator} - The remaining values of the input iterable.
* @returns {IterableIterator} - The remaining values of the input iterable.
*/
export function* drop(iterable, n) {
if (n < 0) {
yield* tail(iterable, -n);
return;
}

const iterator = iter(iterable);
consume(iterator, n);
yield* iterator;
export default function drop(iterable, n) {
return n < 0 ? _tail(iterable, -n) : _drop(iterable, n);
}
2 changes: 1 addition & 1 deletion src/base/exhaust.js
Expand Up @@ -9,7 +9,7 @@
* @param {Iterator} iterator - The input iterator.
*
*/
export function exhaust(iterator) {
export default function exhaust(iterator) {
// eslint-disable-next-line no-empty,no-unused-vars,prettier/prettier
for (const item of iterator) {}
}
5 changes: 3 additions & 2 deletions src/base/first.js
@@ -1,7 +1,8 @@
import {next} from './next.js';
import next from './next.js';

/**
* Same as {@link next}.
* @function first
*/
export const first = next;
const first = next;
export default first;
6 changes: 3 additions & 3 deletions src/base/frame.js
@@ -1,5 +1,5 @@
import {iter} from './iter.js';
import {range} from './range.js';
import iter from './iter.js';
import range from './range.js';

/**
* Yields tuples that contain the current element of the input iterable and the
Expand All @@ -15,7 +15,7 @@ import {range} from './range.js';
* @returns {Iterator}
*
*/
export function* frame(iterable, n) {
export default function* frame(iterable, n) {
// Could have an implementation using a deque
// that doesn't slice (and thus allocate a new
// vector everytime). Though the yield object
Expand Down
5 changes: 3 additions & 2 deletions src/base/head.js
@@ -1,7 +1,8 @@
import {take} from './take.js';
import take from './take.js';

/**
* Same as {@link take}.
* @function head
*/
export const head = take;
const head = take;
export default head;
25 changes: 0 additions & 25 deletions src/base/index.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/base/iter.js
Expand Up @@ -5,6 +5,6 @@
* @returns {Iterator}
*
*/
export function iter(iterable) {
export default function iter(iterable) {
return iterable[Symbol.iterator]();
}
8 changes: 4 additions & 4 deletions src/base/last.js
@@ -1,5 +1,5 @@
import {next} from './next.js';
import {tail} from './tail.js';
import next from './next.js';
import _tail from './_tail.js';

/**
* Returns the last value of the input iterable. If the iterable is
Expand All @@ -8,6 +8,6 @@ import {tail} from './tail.js';
* @param {Iterable} iterable - The input iterable.
* @returns {Object} The last value of the input iterable.
*/
export function last(iterable) {
return next(tail(iterable, 1));
export default function last(iterable) {
return next(_tail(iterable, 1));
}
2 changes: 1 addition & 1 deletion src/base/len.js
Expand Up @@ -9,6 +9,6 @@
* @returns {Number}
*
*/
export function len(array) {
export default function len(array) {
return array.length;
}
2 changes: 1 addition & 1 deletion src/base/list.js
Expand Up @@ -9,6 +9,6 @@
* @returns {Array}
*
*/
export function list(iterable) {
export default function list(iterable) {
return Array.from(iterable);
}
2 changes: 1 addition & 1 deletion src/base/ncycle.js
Expand Up @@ -10,7 +10,7 @@
* @returns {Iterator}
*
*/
export function* ncycle(iterable, n) {
export default function* ncycle(iterable, n) {
const buffer = [];

for (const item of iterable) {
Expand Down
4 changes: 2 additions & 2 deletions src/base/next.js
@@ -1,4 +1,4 @@
import {StopIteration} from './StopIteration.js';
import StopIteration from './StopIteration.js';

/**
* Returns the next value of the input iterator. If the iterator is exhausted,
Expand All @@ -10,7 +10,7 @@ import {StopIteration} from './StopIteration.js';
* that the input iterator is exhausted.
* @returns {Object} The next value of the input iterator.
*/
export function next(iterator, dflt = undefined) {
export default function next(iterator, dflt = undefined) {
const x = iterator.next();

if (x.done) {
Expand Down
4 changes: 2 additions & 2 deletions src/base/pick.js
Expand Up @@ -8,9 +8,9 @@
*
* @param {Object} object - The input object.
* @param {Iterable} iterable - The input iterable.
* @returns {Iterator}
* @returns {IterableIterator}
*/
export function* pick(object, iterable) {
export default function* pick(object, iterable) {
for (const key of iterable) {
yield object[key];
}
Expand Down

0 comments on commit b63b59f

Please sign in to comment.