Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit f6e4ade

Browse files
committed
Add static from method and tests
1 parent 16cc2af commit f6e4ade

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/__tests__/SortedSet.test.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { SortedSet } from '..'
22

33
describe('SortedSet', () => {
4+
const numberCompare = (a: number, b: number): number => a - b
5+
46
describe('get', () => {
57
it('Should return undefined for invalid indicies', () => {
6-
const arr = new SortedSet<number>((a, b) => a - b)
8+
const arr = new SortedSet(numberCompare)
79

810
arr.add(1)
911
arr.add(5)
@@ -17,7 +19,7 @@ describe('SortedSet', () => {
1719

1820
describe('size', () => {
1921
it('Should return the number of values in the tree', () => {
20-
const arr = new SortedSet<number>((a, b) => a - b)
22+
const arr = new SortedSet(numberCompare)
2123

2224
expect(arr.size).toBe(0)
2325

@@ -35,7 +37,7 @@ describe('SortedSet', () => {
3537

3638
describe('iterator', () => {
3739
it('Should iterate the right values and in order', () => {
38-
const arr = new SortedSet<number>((a, b) => a - b)
40+
const arr = new SortedSet(numberCompare)
3941
arr.add(2)
4042
arr.add(3)
4143
arr.add(1)
@@ -50,7 +52,7 @@ describe('SortedSet', () => {
5052

5153
describe('add', () => {
5254
it('Should insert values in order', () => {
53-
const arr = new SortedSet<number>((a, b) => a - b)
55+
const arr = new SortedSet(numberCompare)
5456

5557
arr.add(1)
5658
arr.add(5)
@@ -65,7 +67,7 @@ describe('SortedSet', () => {
6567

6668
describe('remove', () => {
6769
it('Should remove a value from a tree.', () => {
68-
const s = new SortedSet<number>((a, b) => a - b)
70+
const s = new SortedSet(numberCompare)
6971
const vals = []
7072
for (let i = 0; i < 10; i++) {
7173
s.add(i)
@@ -78,4 +80,23 @@ describe('SortedSet', () => {
7880
expect([...s]).toMatchObject(vals)
7981
})
8082
})
83+
84+
describe('from', () => {
85+
const iterable = [1, 1, 2, 3, 6, -100]
86+
const result = SortedSet.from(iterable, numberCompare)
87+
88+
it('Should contain the correct values', () => {
89+
for (const value of iterable) {
90+
expect(result.has(value)).toBe(true)
91+
}
92+
})
93+
94+
it('Should be the right size', () => {
95+
expect(result.size).toBe(new Set(iterable).size)
96+
})
97+
98+
it('Should be in-order', () => {
99+
expect([...result]).toMatchObject([...result].sort())
100+
})
101+
})
81102
})

src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,24 @@ export class SortedSet<T> implements Set<T> {
191191
values(): IterableIterator<T> {
192192
return this[Symbol.iterator]()
193193
}
194+
195+
/**
196+
* Creates a sorted set from an iterable.
197+
*
198+
* @param iterable The itereable to turn into a sorted set.
199+
* @param compare The compare method for the resulting Sorted Set
200+
* @returns A new sorted set whose elements are from the passed iterable.
201+
*/
202+
static from<T>(
203+
iterable: Iterable<T>,
204+
compare?: (a: T, b: T) => number
205+
): SortedSet<T> {
206+
const result = new SortedSet<T>(compare)
207+
208+
for (const value of iterable) {
209+
result.add(value)
210+
}
211+
212+
return result
213+
}
194214
}

0 commit comments

Comments
 (0)