sequency
Version:
Functional sequences for processing iterable data in JavaScript
30 lines • 1.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Single = void 0;
var Single = /** @class */ (function () {
function Single() {
}
/**
* Returns the single element of the sequence or throws error if the sequence has more than
* one element or none at all. If a `predicate` is passed returns the single element matching
* the predicate or throws an error if more or less than one element match the predicate.
*
* @param {(value: T) => boolean} predicate
* @returns {T}
*/
Single.prototype.single = function (predicate) {
if (predicate != null) {
return this.filter(predicate).single();
}
var first = this.iterator.next();
if (first.done) {
throw new Error("No such element");
}
if (!this.iterator.next().done) {
throw new Error("Expect single element");
}
return first.value;
};
return Single;
}());
exports.Single = Single;
//# sourceMappingURL=single.js.map