Skip to content

Commit d8f791f

Browse files
feature: Adds special Number static vars
1 parent df50dfb commit d8f791f

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

Sources/Haystack/IO/ZincReader.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public class ZincReader {
6464
case "M": return marker
6565
case "R": return remove
6666
case "NA": return na
67-
case "NaN": return Number(.nan)
68-
case "INF": return Number(.infinity)
67+
case "NaN": return Number.nan
68+
case "INF": return Number.infinity
6969
default:
7070
throw ZincReaderError.UnexpectedId(id)
7171
}
@@ -79,7 +79,7 @@ public class ZincReader {
7979
if cur == .minus, peek == .id, peekVal.equals("INF") {
8080
try consume(.minus)
8181
try consume(.id)
82-
return Number(-1.0 * .infinity)
82+
return Number.negativeInfinity
8383
}
8484

8585
if cur == .lbracket {

Sources/Haystack/Number.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import Foundation
33
public struct Number: Val {
44
public static var valType: ValType { .Number }
55

6+
public static var infinity: Self {
7+
return Number(.infinity)
8+
}
9+
public static var negativeInfinity: Self {
10+
return Number(-1 * .infinity)
11+
}
12+
public static var nan: Self {
13+
return Number(.nan)
14+
}
15+
616
public let val: Double
717
public let unit: String?
818

Tests/HaystackTests/IO/ZincReaderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ final class ZincReaderTests: XCTestCase {
107107
[Uri("path"), Ref("12cbb082-0c02ae73"), Number(4, unit: "s"), Number(-2.5, unit: "min")],
108108
[marker, remove, null, null], // Bin not supported.
109109
[Date(year: 2009, month: 12, day: 31), Time(hour: 23, minute: 59, second: 1), Time(hour: 1, minute: 2, second: 3, millisecond: 123), DateTime(year: 2009, month: 2, day: 3, hour: 4, minute: 5, second: 6)],
110-
[Number(.infinity), Number(-1 * .infinity), "", Number(.nan)],
110+
[Number.infinity, Number.negativeInfinity, "", Number.nan],
111111
[Coord(lat: 12, lng: -34), Coord(lat: 0.123, lng: -0.789), Coord(lat: 84.5, lng: -77.45), Coord(lat: -90, lng: 180)],
112112
]
113113
)

Tests/HaystackTests/NumberTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ final class NumberTests: XCTestCase {
7272
"4"
7373
)
7474
XCTAssertEqual(
75-
Number(.infinity).toZinc(),
75+
Number.infinity.toZinc(),
7676
"INF"
7777
)
7878
XCTAssertEqual(
79-
Number((-1.0 * .infinity)).toZinc(),
79+
Number.negativeInfinity.toZinc(),
8080
"-INF"
8181
)
8282
XCTAssertEqual(
83-
Number(.nan).toZinc(),
83+
Number.nan.toZinc(),
8484
"NaN"
8585
)
8686
}

0 commit comments

Comments
 (0)