mirror of
https://github.com/Terncode/pixel.horse.git
synced 2026-09-24 13:55:04 +02:00
Add util function uniqById
This commit is contained in:
@@ -1870,3 +1870,5 @@ if (typeof window !== 'undefined') {
|
||||
const nodeLoadTime = getNanoSeconds() - process.uptime() * 1e9;
|
||||
counterNow = () => (getNanoSeconds() - nodeLoadTime) / 1e6;
|
||||
}
|
||||
|
||||
export type PrimitiveValues = string | number | symbol | boolean;
|
||||
|
||||
+11
-1
@@ -1,6 +1,6 @@
|
||||
import { HttpErrorResponse } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Point, Rect, Entity, Dict, Holiday, Season } from './interfaces';
|
||||
import { Point, Rect, Entity, Dict, Holiday, Season, PrimitiveValues } from './interfaces';
|
||||
import { tileWidth, tileHeight, SECOND, MINUTE, HOUR, DAY } from './constants';
|
||||
import { ACCESS_ERROR, NOT_FOUND_ERROR, OFFLINE_ERROR, PROTECTION_ERROR } from './errors';
|
||||
|
||||
@@ -562,3 +562,13 @@ export function getY(e: AnyEvent): number {
|
||||
export function isKeyEventInvalid(e: KeyboardEvent) {
|
||||
return e.target && /^(input|textarea|select)$/i.test((<any>e.target).tagName);
|
||||
}
|
||||
|
||||
export function uniqById<T>(array: T[], uniqueKey: (self: T) => PrimitiveValues) {
|
||||
const seen = new Set<PrimitiveValues>();
|
||||
return array.filter(item => {
|
||||
const id = uniqueKey(item);
|
||||
const isDuplicate = seen.has(id);
|
||||
seen.add(id);
|
||||
return !isDuplicate;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user