diff --git a/packages/core/src/utils/countable-set.ts b/packages/core/src/utils/countable-set.ts index 7eab83c0b3..6b7e144b7a 100644 --- a/packages/core/src/utils/countable-set.ts +++ b/packages/core/src/utils/countable-set.ts @@ -4,8 +4,6 @@ export class CountableSet extends Set { constructor(values?: Iterable) { super(values) this._map ??= new Map() - for (const value of values ?? []) - this.add(value) } add(key: K) { diff --git a/test/countable-set.test.ts b/test/countable-set.test.ts new file mode 100644 index 0000000000..83571182ba --- /dev/null +++ b/test/countable-set.test.ts @@ -0,0 +1,74 @@ +import { describe, expect, test } from 'vitest' +import { CountableSet } from '../packages/unocss' + +describe('CountableSet', () => { + test('constructor', () => { + const s = new CountableSet(['bar1', 'bar2', 'bar3', 'bar2']) + + expect(s).toMatchInlineSnapshot(` + Set { + "bar1", + "bar2", + "bar3", + } + `) + + expect(s._map).toMatchInlineSnapshot(` + Map { + "bar1" => 1, + "bar2" => 2, + "bar3" => 1, + } + `) + }) + + test('add', () => { + const s = new CountableSet() + + s.add('bar1') + s.add('bar2') + s.add('bar2') + + expect(s).toMatchInlineSnapshot(` + Set { + "bar1", + "bar2", + } + `) + + expect(s._map).toMatchInlineSnapshot(` + Map { + "bar1" => 1, + "bar2" => 2, + } + `) + }) + + test('getCount', () => { + const s = new CountableSet(['bar1', 'bar2', 'bar2']) + + expect(s.getCount('bar1')).toBe(1) + expect(s.getCount('bar2')).toBe(2) + }) + + test('setCount', () => { + const s = new CountableSet() + + s.setCount('bar1', 2) + s.setCount('bar2', 1) + + expect(s).toMatchInlineSnapshot(` + Set { + "bar1", + "bar2", + } + `) + + expect(s._map).toMatchInlineSnapshot(` + Map { + "bar1" => 2, + "bar2" => 1, + } + `) + }) +})