Skip to content

Commit ba5ae4c

Browse files
committed
Merge pull request #8 from ReactKit/fix/PlusMinus-Num0
Fix Key.PlusMinus + Key.Num0 displaying "-00"
2 parents a1468a7 + faa88b7 commit ba5ae4c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

ReactKitCalculator/Calculator.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,14 @@ public class Calculator
273273

274274
// numKey except `.Point` (NOTE: `case .Point` declared above)
275275
case let numKey where contains(Key.numKeys(), numKey):
276-
if acc == Key.Num0.rawValue {
277-
return newKey.rawValue // e.g. "0" -> "1" will be "1"
276+
if acc == Key.Minus.rawValue + Key.Num0.rawValue {
277+
return Key.Minus.rawValue + newKey.rawValue // e.g. "-0" then "1" will be "-1"
278+
}
279+
else if acc == Key.Num0.rawValue {
280+
return newKey.rawValue // e.g. "0" then "1" will be "1"
278281
}
279282
else {
280-
return (accumulatedString ?? "") + newKey.rawValue // e.g. "12" -> "3" will be "123"
283+
return (accumulatedString ?? "") + newKey.rawValue // e.g. "12" then "3" will be "123"
281284
}
282285

283286
case .PlusMinus:

ReactKitCalculatorTests/OutputSpec.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ class OutputSpec: QuickSpec
139139

140140
}
141141

142+
it("`± 0` should print `-0`") {
143+
144+
p.input = "±"
145+
expect(p.output!).to(equal("-0"))
146+
147+
p.input = "0"
148+
expect(p.output!).to(equal("-0"))
149+
150+
}
151+
152+
it("`± 0 1` should print `-1`") {
153+
154+
p.input = "±"
155+
expect(p.output!).to(equal("-0"))
156+
157+
p.input = "0"
158+
expect(p.output!).to(equal("-0"))
159+
160+
p.input = "1"
161+
expect(p.output!).to(equal("-1"))
162+
163+
}
164+
142165
it("`1 ±` should print `-1`") {
143166

144167
p.input = "1"

0 commit comments

Comments
 (0)