@@ -5,15 +5,12 @@ public enum CandidateType: Int {
5
5
/// Plain text. Examples: iPad, macOS
6
6
case text
7
7
8
+ /// Note that `Candidate.text.count == 1` not always true
8
9
case emoji
9
10
10
- case emojiSequence
11
-
12
- /// Note that `text.count == 1` not always true
11
+ /// Note that `Candidate.text.count == 1` not always true
13
12
case symbol
14
13
15
- case symbolSequence
16
-
17
14
/// macOS Keyboard composed text. Mainly for PunctuationKey.
18
15
case compose
19
16
}
@@ -127,13 +124,19 @@ public struct Candidate: Hashable {
127
124
return self . type == . cantonese
128
125
}
129
126
127
+ /// type != .cantonese
128
+ public var isNotCantonese : Bool {
129
+ return self . type != . cantonese
130
+ }
131
+
130
132
/// Concatenated by multiple lexicons
131
133
public var isCompound : Bool {
132
134
return subNotations. isNotEmpty
133
135
}
134
136
135
137
// Equatable
136
138
public static func == ( lhs: Candidate , rhs: Candidate ) -> Bool {
139
+ guard lhs. type == rhs. type else { return false }
137
140
if lhs. isCantonese && rhs. isCantonese {
138
141
return lhs. text == rhs. text && lhs. romanization == rhs. romanization
139
142
} else {
@@ -151,18 +154,15 @@ public struct Candidate: Hashable {
151
154
hasher. combine ( text)
152
155
case . emoji:
153
156
hasher. combine ( text)
154
- case . emojiSequence:
155
- hasher. combine ( text)
156
157
case . symbol:
157
158
hasher. combine ( text)
158
- case . symbolSequence:
159
- hasher. combine ( text)
160
159
case . compose:
161
160
hasher. combine ( text)
162
161
}
163
162
}
164
163
165
- static func + ( lhs: Candidate , rhs: Candidate ) -> Candidate {
164
+ static func + ( lhs: Candidate , rhs: Candidate ) -> Candidate ? {
165
+ guard lhs. isCantonese && rhs. isCantonese else { return nil }
166
166
let newText : String = lhs. text + rhs. text
167
167
let newLexiconText : String = lhs. lexiconText + rhs. lexiconText
168
168
let newRomanization : String = lhs. romanization + " " + rhs. romanization
@@ -190,7 +190,9 @@ extension Array where Element == Candidate {
190
190
191
191
/// Returns a new Candidate by concatenating this Candidate sequence.
192
192
/// - Returns: Single, concatenated Candidate.
193
- public func joined( ) -> Candidate {
193
+ public func joined( ) -> Candidate ? {
194
+ let isNotAllCantonese : Bool = self . contains ( where: \. isNotCantonese)
195
+ guard !isNotAllCantonese else { return nil }
194
196
let text : String = map ( \. text) . joined ( )
195
197
let lexiconText : String = map ( \. lexiconText) . joined ( )
196
198
let romanization : String = map ( \. romanization) . joined ( separator: " " )
0 commit comments