isSet
Checks if the given value is a Set. Instances from [other realms][1] are also supported. [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms
Usage
isSet(new Set([1, 2, 3])); // => trueisSet(new Map([1, 2, 3])); // => false
Installation
npx atmx add helper is-set
Copy and paste the following method into @/utils/helpers/undefined.ts
:
import { isTagged } from "@/helpers/is-tagged.ts";
import type { StrictExtract } from "@/types/strict-extract.ts";
/*** Checks if the given value is a Set.** Instances from [other realms][1] are also supported.** [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms** @example* isSet(new Set([1, 2, 3])) // => true* isSet(new Map([1, 2, 3])) // => false*/export function isSet<Input>(value: Input): value is ExtractSet<Input> {return isTagged(value, "[object Set]");}
/*** An absurdly complicated but accurate type for extracting Set types.** It's like `Extract<T, Set<any>>` but better with edge cases.*/export type ExtractSet<T> = T extends any? [StrictExtract<T, ReadonlySet<unknown>>] extends [ReadonlySet<unknown>]? Extract<T, ReadonlySet<unknown>>: [StrictExtract<T, Set<unknown>>] extends [Set<unknown>] ? Extract<T, Set<unknown>> : Set<unknown> extends T ? Set<unknown> : never: never;