1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 18x 11x 4x 7x 7x 7x 7x 2x 5x 5x 3x 2x | import Iterator from '../iterators/Iterator'; import '../where'; Iterator.prototype.single = function single(condition) { if (condition) { return this.where(condition).single(); } else { let iterator = this[Symbol.iterator](); let first = iterator.next(); let result = first.value; if (first.done) { throw new Error('No elements in the iterable'); } let second = iterator.next(); if (!second.done) { throw new Error('More than one element found.'); } return result; } }; |