data-structure-algebra/circularly-linked-list

View on GitHub
src/from.js

Summary

Maintainability
A
0 mins
Test Coverage
import Node from './Node.js';
import _append from './_append.js';

/**
 * Creates a list from an input iterable.
 *
 * @param {Iterable} iterable The input iterable.
 * @return {Node} First node of the newly created list (or null if empty list).
 */
export default function from(iterable) {
    const it = iterable[Symbol.iterator]();
    const event = it.next();

    if (event.done) return null;

    const first = new Node(event.value);

    for (const value of it) _append(first, new Node(value));

    return first;
}