1
1
import { describe , it , expect , xit } from '@jest/globals'
2
2
import { Anagram } from './anagram.ts'
3
3
4
+ const areSetsEqual = < T > ( setA : Set < T > , setB : Set < T > ) : boolean =>
5
+ setA . size === setB . size && [ ...setA ] . every ( ( val ) => setB . has ( val ) )
6
+
4
7
describe ( 'Anagram' , ( ) => {
5
8
it ( 'no matches' , ( ) => {
6
9
const subject = new Anagram ( 'diaper' )
7
10
const matches = subject . matches ( 'hello' , 'world' , 'zombies' , 'pants' )
11
+ const expected = [ ]
8
12
9
- expect ( matches ) . toEqual ( [ ] )
13
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
10
14
} )
11
15
12
16
xit ( 'detects two anagrams' , ( ) => {
13
17
const subject = new Anagram ( 'solemn' )
14
18
const matches = subject . matches ( 'lemons' , 'cherry' , 'melons' )
19
+ const expected = [ 'lemons' , 'melons' ]
15
20
16
- expect ( matches ) . toEqual ( [ 'lemons' , 'melons' ] )
21
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
17
22
} )
18
23
19
24
xit ( 'does not detect anagram subsets' , ( ) => {
20
25
const subject = new Anagram ( 'good' )
21
26
const matches = subject . matches ( 'dog' , 'goody' )
27
+ const expected = [ ]
22
28
23
- expect ( matches ) . toEqual ( [ ] )
29
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
24
30
} )
25
31
26
32
xit ( 'detects anagram' , ( ) => {
27
33
const subject = new Anagram ( 'listen' )
28
34
const matches = subject . matches ( 'enlists' , 'google' , 'inlets' , 'banana' )
35
+ const expected = [ 'inlets' ]
29
36
30
- expect ( matches ) . toEqual ( [ 'inlets' ] )
37
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
31
38
} )
32
39
33
40
xit ( 'detects three anagrams' , ( ) => {
@@ -40,98 +47,112 @@ describe('Anagram', () => {
40
47
'largely' ,
41
48
'leading'
42
49
)
50
+ const expected = [ 'gallery' , 'regally' , 'largely' ]
43
51
44
- expect ( matches ) . toEqual ( [ 'gallery' , 'regally' , 'largely' ] )
52
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
45
53
} )
46
54
47
55
xit ( 'detects multiple anagrams with different case' , ( ) => {
48
56
const subject = new Anagram ( 'nose' )
49
57
const matches = subject . matches ( 'Eons' , 'ONES' )
58
+ const expected = [ 'Eons' , 'ONES' ]
50
59
51
- expect ( matches ) . toEqual ( [ 'Eons' , 'ONES' ] )
60
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
52
61
} )
53
62
54
63
xit ( 'does not detect non-anagrams with identical checksum' , ( ) => {
55
64
const subject = new Anagram ( 'mass' )
56
65
const matches = subject . matches ( 'last' )
66
+ const expected = [ ]
57
67
58
- expect ( matches ) . toEqual ( [ ] )
68
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
59
69
} )
60
70
61
71
xit ( 'detects anagrams case-insensitively' , ( ) => {
62
72
const subject = new Anagram ( 'Orchestra' )
63
73
const matches = subject . matches ( 'cashregister' , 'Carthorse' , 'radishes' )
74
+ const expected = [ 'Carthorse' ]
64
75
65
- expect ( matches ) . toEqual ( [ 'Carthorse' ] )
76
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
66
77
} )
67
78
68
79
xit ( 'detects anagrams using case-insensitive subject' , ( ) => {
69
80
const subject = new Anagram ( 'Orchestra' )
70
81
const matches = subject . matches ( 'cashregister' , 'carthorse' , 'radishes' )
82
+ const expected = [ 'carthorse' ]
71
83
72
- expect ( matches ) . toEqual ( [ 'carthorse' ] )
84
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
73
85
} )
74
86
75
87
xit ( 'detects anagrams using case-insensitive possible matches' , ( ) => {
76
88
const subject = new Anagram ( 'orchestra' )
77
89
const matches = subject . matches ( 'cashregister' , 'Carthorse' , 'radishes' )
90
+ const expected = [ 'Carthorse' ]
78
91
79
- expect ( matches ) . toEqual ( [ 'Carthorse' ] )
92
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
80
93
} )
81
94
82
95
xit ( 'does not detect an anagram if the original word is repeated' , ( ) => {
83
96
const subject = new Anagram ( 'go' )
84
97
const matches = subject . matches ( 'go Go GO' )
98
+ const expected = [ ]
85
99
86
- expect ( matches ) . toEqual ( [ ] )
100
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
87
101
} )
88
102
89
103
xit ( 'anagrams must use all letters exactly once' , ( ) => {
90
104
const subject = new Anagram ( 'tapper' )
91
105
const matches = subject . matches ( 'patter' )
106
+ const expected = [ ]
92
107
93
- expect ( matches ) . toEqual ( [ ] )
108
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
94
109
} )
95
110
96
111
xit ( 'words are not anagrams of themselves' , ( ) => {
97
112
const subject = new Anagram ( 'BANANA' )
98
113
const matches = subject . matches ( 'BANANA' )
114
+ const expected = [ ]
99
115
100
- expect ( matches ) . toEqual ( [ ] )
116
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
101
117
} )
102
118
103
119
xit ( 'words are not anagrams of themselves even if letter case is partially different' , ( ) => {
104
120
const subject = new Anagram ( 'BANANA' )
105
121
const matches = subject . matches ( 'Banana' )
122
+ const expected = [ ]
106
123
107
- expect ( matches ) . toEqual ( [ ] )
124
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
108
125
} )
109
126
110
127
xit ( 'words are not anagrams of themselves even if letter case is completely different' , ( ) => {
111
128
const subject = new Anagram ( 'BANANA' )
112
129
const matches = subject . matches ( 'Banana' )
130
+ const expected = [ ]
113
131
114
- expect ( matches ) . toEqual ( [ ] )
132
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
115
133
} )
116
134
117
135
xit ( 'words other than themselves can be anagrams' , ( ) => {
118
136
const subject = new Anagram ( 'LISTEN' )
119
137
const matches = subject . matches ( 'LISTEN' , 'Silent' )
138
+ const expected = [ 'Silent' ]
120
139
121
- expect ( matches ) . toEqual ( [ 'Silent' ] )
140
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
122
141
} )
123
142
124
143
xit ( 'matches() accepts string arguments' , ( ) => {
125
144
const subject = new Anagram ( 'ant' )
126
145
const matches = subject . matches ( 'stand' , 'tan' , 'at' )
146
+ const expected = [ 'tan' ]
127
147
128
- expect ( matches ) . toEqual ( [ 'tan' ] )
148
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
129
149
} )
130
150
131
151
xit ( 'matches() accepts single string argument' , ( ) => {
132
152
const subject = new Anagram ( 'ant' )
133
153
const matches = subject . matches ( 'tan' )
154
+ const expected = [ 'tan' ]
134
155
135
- expect ( matches ) . toEqual ( [ 'tan' ] )
156
+ expect ( areSetsEqual ( new Set ( expected ) , new Set ( matches ) ) ) . toEqual ( true )
136
157
} )
137
158
} )
0 commit comments