UNPKG

dupes-of-hazard

Version:
26 lines (25 loc) 864 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDupes = void 0; const json_stable_stringify_1 = __importDefault(require("json-stable-stringify")); /** * Get a list of duplicate values from an array. */ function getDupes(arr) { const counts = new Map(); const dupes = new Set(); for (const item of arr) { const stringifiedItem = (0, json_stable_stringify_1.default)(item); const currentCount = counts.get(stringifiedItem); const newCount = currentCount ? currentCount + 1 : 1; counts.set(stringifiedItem, newCount); if (newCount === 2) { dupes.add(item); } } return dupes; } exports.getDupes = getDupes;