@walts81/linq-ts
Version:
Typescript/Javascript LINQ implementation library
30 lines (29 loc) • 874 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.single = void 0;
const _common_1 = require("./_common");
Array.prototype.single = single;
function single(expression) {
const length = this.length;
if (length === 0)
throw new _common_1.EmptyArrayException();
if (!expression)
if (length > 1)
throw new _common_1.MultipleMatchException();
else
return this[0];
let result = undefined;
for (let i = 0; i < length; i++) {
const item = this[i];
if (expression(item, i)) {
if (result === undefined)
result = item;
else
throw new _common_1.MultipleMatchException();
}
}
if (result === undefined)
throw new _common_1.NoMatchException();
return result;
}
exports.single = single;