Created
January 27, 2026 12:45
-
-
Save Amzd/67d53943ed8d2f25341f7c105c33fbcb to your computer and use it in GitHub Desktop.
This is the same as Set but supports objects. Like https://github.com/szikszail/object-set-type but fast.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This is the same as Set but supports objects. | |
| * Like https://github.com/szikszail/object-set-type but fast. | |
| */ | |
| export class ObjectSet<T> { | |
| _set: Set<string> | |
| constructor(arr: T[] = []) { | |
| this._set = new Set(arr.map(x => JSON.stringify(x))) | |
| } | |
| add(value: any): this { | |
| this._set.add(JSON.stringify(value)) | |
| return this | |
| } | |
| has(value: any): boolean { | |
| return this._set.has(JSON.stringify(value)) | |
| } | |
| *[Symbol.iterator]() { | |
| for (const str of this._set) { | |
| yield JSON.parse(str) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment