import { inspect } from 'util' import type { Assert, Primitive } from './prelude.js' type Mapping = (value: A) => B /** Asserts unique array using given `f` mapping from array elements to `===` comparable values (defaults to identity). */ const unique = (f: Mapping): Assert => values => { if (!Array.isArray(values)) { throw new TypeError(`Expected array, got ${inspect(values)}.`) } const set = new Set for (const value of values) { const value_ = f(value) if (set.has(value_)) { throw new TypeError(`Expected unique values, got duplicate ${inspect(value)}.`) } set.add(value_) } return values } export default unique