UNPKG

sequency

Version:

Functional sequences for processing iterable data in JavaScript

30 lines 1.03 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.SingleOrNull = void 0; var SingleOrNull = /** @class */ (function () { function SingleOrNull() { } /** * Returns the single element of the sequence or `null` 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 `null` if more or less than one element match the predicate. * * @param {(value: T) => boolean} predicate * @returns {T} */ SingleOrNull.prototype.singleOrNull = function (predicate) { if (predicate != null) { return this.filter(predicate).singleOrNull(); } var first = this.iterator.next(); if (first.done) { return null; } if (!this.iterator.next().done) { return null; } return first.value; }; return SingleOrNull; }()); exports.SingleOrNull = SingleOrNull; //# sourceMappingURL=singleOrNull.js.map