MCPcopy Create free account
hub / github.com/blacksmithgu/datacore / distinct

Method distinct

src/api/data-array.ts:365–383  ·  view source on GitHub ↗
(key?: ArrayFunc<T, U>, comparator?: ArrayComparator<U>)

Source from the content-addressed store, hash-verified

363 }
364
365 public distinct<U>(key?: ArrayFunc<T, U>, comparator?: ArrayComparator<U>): DataArray<T> {
366 if (this.values.length == 0) return this;
367 let realKey = key ?? ((x) => x as unknown as U);
368
369 // For similar reasons to groupBy, do a sort and take the first element of each block.
370 let intermediate = this.map((x, index) => {
371 return { key: realKey(x, index, this.values), value: x };
372 }).sort((x) => x.key, "asc", comparator);
373 comparator = comparator ?? this.defaultComparator;
374
375 let result: T[] = [intermediate[0].value];
376 for (let index = 1; index < intermediate.length; index++) {
377 if (comparator(intermediate[index - 1].key, intermediate[index].key) != 0) {
378 result.push(intermediate[index].value);
379 }
380 }
381
382 return this.lwrap(result);
383 }
384
385 public every(f: ArrayFunc<T, boolean>): boolean {
386 return this.values.every(f);

Callers

nothing calls this directly

Calls 3

mapMethod · 0.95
lwrapMethod · 0.95
sortMethod · 0.65

Tested by

no test coverage detected