UNPKG

@walts81/linq-ts

Version:

Typescript/Javascript LINQ implementation library

22 lines (21 loc) 735 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.distinct = exports.distinctByKey = void 0; Array.prototype.distinctByKey = distinctByKey; Array.prototype.distinct = distinct; function distinctByKey(expression = x => x) { const keys = []; return this.filter((x, i) => { const key = expression(x, i); const notExists = keys.indexOf(key) < 0; if (notExists) keys.push(key); return notExists; }); } exports.distinctByKey = distinctByKey; function distinct(compare) { const useCompare = !!compare; return this.filter((a, i, arr) => arr.indexOf(arr.first(b => (useCompare ? compare(a, b) : a === b))) === i); } exports.distinct = distinct;