Skip to content

Commit

Permalink
perf(core): imoprove CountableSet, add tests (#2986)
Browse files Browse the repository at this point in the history
  • Loading branch information
enkot committed Aug 14, 2023
1 parent 2c92b9c commit 7f3f1d8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/utils/countable-set.ts
Expand Up @@ -4,8 +4,6 @@ export class CountableSet<K> extends Set<K> {
constructor(values?: Iterable<K>) {
super(values)
this._map ??= new Map()
for (const value of values ?? [])
this.add(value)
}

add(key: K) {
Expand Down
74 changes: 74 additions & 0 deletions 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,
}
`)
})
})

0 comments on commit 7f3f1d8

Please sign in to comment.