Skip to content

Commit 47071c7

Browse files
authored
Added wind speed support (#14)
* Added wind speed support * Update README.md
1 parent 35daf6a commit 47071c7

File tree

6 files changed

+135
-2
lines changed

6 files changed

+135
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ source 'https://github.com/cropio/cocoapods-specs.git'
1818
```
1919

2020
### Use
21-
For using this lib you need to get units table form cropio api
21+
For using this lib you need to get units table form **Cropwise Operations** api
2222

2323
```swift
2424
let table = [
@@ -33,6 +33,7 @@ let table = [
3333
"temperature": "fahrenheit",
3434
"precipitation_level": "in",
3535
"water_rate": "american_quart",
36+
"wind_speed": "m_per_sec",
3637
"fuel_consumption": "mile_per_uk_gallon",
3738
"short_length": "ft",
3839
"depth": "in",
@@ -80,6 +81,7 @@ let table = [
8081
"temperature": "fahrenheit",
8182
"precipitation_level": "in",
8283
"water_rate": "american_quart",
84+
"wind_speed": "m_per_sec",
8385
"fuel_consumption": "mile_per_uk_gallon",
8486
"short_length": "ft",
8587
"depth": "in",

Sources/units-swift/Units.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public struct Units {
2525
public let temperature: Temperature
2626
public let volume: Volume
2727
public let waterRate: WaterRate
28+
public let windSpeed: WindSpeed
2829
public let weight: Weight
2930
public let yieldWeight: YieldWeight
3031

@@ -71,6 +72,9 @@ public struct Units {
7172
self.volume = try .from(units: units, localizator: localizator)
7273
self.waterRate = try .from(units: units, localizator: localizator)
7374
self.weight = try .from(units: units, localizator: localizator)
75+
// try? Need for correct migration, some users may have old version of 'units' without 'windSpeed'
76+
self.windSpeed = (try? .from(units: units, localizator: localizator))
77+
?? .from(.mPerSec, localizator: localizator) // if not found set default
7478
self.yieldWeight = try .from(units: units, localizator: localizator)
7579
} else {
7680
self.area = .from(.ha, localizator: localizator)
@@ -88,6 +92,7 @@ public struct Units {
8892
self.temperature = .from(.celsius, localizator: localizator)
8993
self.volume = .from(.liter, localizator: localizator)
9094
self.waterRate = .from(.literPerHa, localizator: localizator)
95+
self.windSpeed = .from(.mPerSec, localizator: localizator)
9196
self.weight = .from(.centner, localizator: localizator)
9297
self.yieldWeight = .from(.centner, localizator: localizator)
9398
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// WindSpeed.swift
3+
// units-swift
4+
//
5+
// Created by Evegeny Kalashnikov on 02.10.2023.
6+
//
7+
8+
import Foundation
9+
10+
public struct WindSpeed: TransformableUnitType {
11+
12+
public enum Settings: UnitTypeSettings {
13+
case mPerSec
14+
case kmPerHour
15+
case milePerHour
16+
17+
public var factor: Double {
18+
switch self {
19+
case .mPerSec: return 1.0
20+
case .kmPerHour: return 3.6
21+
case .milePerHour: return 2.2369362920544
22+
}
23+
}
24+
25+
public static var mappings: [String: Settings] = [
26+
"m_per_sec": .mPerSec,
27+
"km_per_hour": .kmPerHour,
28+
"mile_per_hour": .milePerHour
29+
]
30+
}
31+
32+
public static var code: String {
33+
"wind_speed"
34+
}
35+
36+
public static func localization(settings: Settings, localizator: Localizator) -> Localization {
37+
switch settings {
38+
case .mPerSec: return localizator.mPerSec
39+
case .kmPerHour: return localizator.kmPerHour
40+
case .milePerHour: return localizator.milePerHour
41+
}
42+
}
43+
44+
public private(set) var settings: Settings
45+
public private(set) var localization: Localization
46+
47+
public init(settings: Settings, localization: Localization) {
48+
self.settings = settings
49+
self.localization = localization
50+
}
51+
}

Tests/units-swiftTests/UnitTypeTests.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class UnitTypeTests: XCTestCase {
4040
"temperature": "fahrenheit",
4141
"precipitation_level": "in",
4242
"water_rate": "american_quart",
43+
"wind_speed": "mile_per_hour",
4344
"fuel_consumption": "mile_per_uk_gallon",
4445
"short_length": "ft",
4546
"depth": "in",
@@ -110,6 +111,10 @@ class UnitTypeTests: XCTestCase {
110111
XCTAssertEqual(units.waterRate.to(369.89696315500004), 865)
111112
XCTAssertEqual(units.waterRate.localization.short, "Cuarto/acre")
112113

114+
XCTAssertEqual(units.windSpeed.from(10), 22.369362920544)
115+
XCTAssertEqual(units.windSpeed.to(5.6), 2.5034240000000025)
116+
XCTAssertEqual(units.windSpeed.localization.short, "mph")
117+
113118
XCTAssertEqual(units.weight.from(8712), 8712.0)
114119
XCTAssertEqual(units.weight.to(871.2), 871.2)
115120
XCTAssertEqual(units.weight.localization.short, "t")
@@ -187,6 +192,10 @@ class UnitTypeTests: XCTestCase {
187192
XCTAssertEqual(units.waterRate.to(369.89696315500004), 369.89696315500004)
188193
XCTAssertEqual(units.waterRate.localization.short, "л/га")
189194

195+
XCTAssertEqual(units.windSpeed.from(10), 10)
196+
XCTAssertEqual(units.windSpeed.to(5.6), 5.6)
197+
XCTAssertEqual(units.windSpeed.localization.short, "м/с")
198+
190199
XCTAssertEqual(units.weight.from(8712), 87120.0)
191200
XCTAssertEqual(units.weight.to(871.2), 87.12)
192201
XCTAssertEqual(units.weight.localization.short, "ц")
@@ -245,6 +254,7 @@ class UnitTypeTests: XCTestCase {
245254
"temperature": "fahrenheit",
246255
"precipitation_level": "in",
247256
"water_rate": "american_quart",
257+
"wind_speed": "mile_per_hour",
248258
"fuel_consumption": "mile_per_uk_gallon",
249259
"short_length": "ft",
250260
"depth": "in",
@@ -299,6 +309,7 @@ class UnitTypeTests: XCTestCase {
299309
"temperature": "fahrenheit",
300310
"precipitation_level": "in",
301311
"water_rate": "american_quart",
312+
"wind_speed": "mile_per_hour",
302313
"fuel_consumption": "mile_per_uk_gallon",
303314
"short_length": "ft",
304315
"depth": "in",
@@ -314,12 +325,43 @@ class UnitTypeTests: XCTestCase {
314325
}
315326
}
316327

328+
func testWindSpeedMigration() {
329+
let table = [ // table with old wind speed unit
330+
"length": "mile",
331+
"area": "decare",
332+
"weight": "tonn",
333+
"machinery_weight": "pound",
334+
"volume": "pint",
335+
"tank_volume": "american_gallon",
336+
"productivity": "kg_per_decare",
337+
"speed": "mile_per_hour",
338+
"temperature": "fahrenheit",
339+
"precipitation_level": "in",
340+
"water_rate": "american_quart",
341+
"fuel_consumption": "mile_per_uk_gallon",
342+
"short_length": "ft",
343+
"depth": "in",
344+
"row_spacing": "cm",
345+
"plant_spacing": "m"
346+
]
347+
348+
do {
349+
let units = try Units(units: table, language: "uk")
350+
XCTAssertEqual(units.windSpeed.from(1), 1)
351+
XCTAssertEqual(units.windSpeed.localization.short, "м/с")
352+
} catch {
353+
XCTFail("error - \(error)")
354+
}
355+
}
356+
317357
static var allTests = [
318358
("testFromError", testFromError),
319359
("testUnits", testUnits),
320360
("testDefaultUnits", testDefaultUnits),
321361
("testDefaultUnitsError", testDefaultUnitsError),
322362
("testUnitsError", testUnitsError),
323-
("testOptionalExtension", testOptionalExtension)
363+
("testOptionalExtension", testOptionalExtension),
364+
("testDekareUnits", testDekareUnits),
365+
("testWindSpeedMigration", testWindSpeedMigration)
324366
]
325367
}

Tests/units-swiftTests/UnitsTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,34 @@ final class UnitsTests: XCTestCase {
454454

455455
}
456456

457+
func testWindSpeed() {
458+
XCTAssertEqual(WindSpeed.code, "wind_speed")
459+
do {
460+
let mPerSec = try WindSpeed.from("m_per_sec", localizator: self.localizator)
461+
XCTAssertEqual(mPerSec.from(3392.106682491515), 3392.106682491515)
462+
XCTAssertEqual(mPerSec.to(3392.106682491515), 3392.106682491515)
463+
} catch let error {
464+
XCTFail("error - \(error)")
465+
}
466+
467+
do {
468+
let kmPerHour = try WindSpeed.from("km_per_hour", localizator: self.localizator)
469+
XCTAssertEqual(kmPerHour.from(4524.924480372933), 16289.72812934256)
470+
XCTAssertEqual(kmPerHour.to(16289.72812934256), 4524.924480372933)
471+
} catch let error {
472+
XCTFail("error - \(error)")
473+
}
474+
475+
do {
476+
let milePerHour = try WindSpeed.from("mile_per_hour", localizator: self.localizator)
477+
XCTAssertEqual(milePerHour.from(2975.349130874201), 6655.666452385016)
478+
XCTAssertEqual(milePerHour.to(6655.666452385016), 2975.349130874201)
479+
} catch let error {
480+
XCTFail("error - \(error)")
481+
}
482+
483+
}
484+
457485
func testShortLength() {
458486
XCTAssertEqual(ShortLength.code, "short_length")
459487
do {
@@ -610,6 +638,7 @@ final class UnitsTests: XCTestCase {
610638
("testLength", testLength),
611639
("testRowSpacing", testRowSpacing),
612640
("testWaterRate", testWaterRate),
641+
("testWindSpeed", testWindSpeed),
613642
("testShortLength", testShortLength),
614643
("testProductivity", testProductivity),
615644
("testDepth", testDepth)

units-swift.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/* End PBXAggregateTarget section */
2222

2323
/* Begin PBXBuildFile section */
24+
151860712ACAE5F700429C96 /* WindSpeed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 151860702ACAE5F700429C96 /* WindSpeed.swift */; };
2425
15C3727E22CFA6A300E6B841 /* Weight.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15C3726E22CFA6A200E6B841 /* Weight.swift */; };
2526
15C3727F22CFA6A300E6B841 /* Depth.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15C3726F22CFA6A200E6B841 /* Depth.swift */; };
2627
15C3728022CFA6A300E6B841 /* PlantSpacing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15C3727022CFA6A200E6B841 /* PlantSpacing.swift */; };
@@ -72,6 +73,7 @@
7273
/* End PBXContainerItemProxy section */
7374

7475
/* Begin PBXFileReference section */
76+
151860702ACAE5F700429C96 /* WindSpeed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindSpeed.swift; sourceTree = "<group>"; };
7577
15C3726E22CFA6A200E6B841 /* Weight.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Weight.swift; sourceTree = "<group>"; };
7678
15C3726F22CFA6A200E6B841 /* Depth.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Depth.swift; sourceTree = "<group>"; };
7779
15C3727022CFA6A200E6B841 /* PlantSpacing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlantSpacing.swift; sourceTree = "<group>"; };
@@ -144,6 +146,7 @@
144146
15C3727822CFA6A300E6B841 /* Temperature.swift */,
145147
15C3727922CFA6A300E6B841 /* Volume.swift */,
146148
15C3727222CFA6A300E6B841 /* WaterRate.swift */,
149+
151860702ACAE5F700429C96 /* WindSpeed.swift */,
147150
15C3726E22CFA6A200E6B841 /* Weight.swift */,
148151
15F8467A29967E2600961B13 /* YieldWeight.swift */,
149152
);
@@ -347,6 +350,7 @@
347350
15F8467B29967E2600961B13 /* YieldWeight.swift in Sources */,
348351
15C3728222CFA6A300E6B841 /* WaterRate.swift in Sources */,
349352
15FC3EDE22CF831900B0DB2B /* Units.swift in Sources */,
353+
151860712ACAE5F700429C96 /* WindSpeed.swift in Sources */,
350354
15FC3EDC22CF82E500B0DB2B /* UnitsType.swift in Sources */,
351355
15C3728822CFA6A300E6B841 /* Temperature.swift in Sources */,
352356
15C3728422CFA6A300E6B841 /* TankVolume.swift in Sources */,

0 commit comments

Comments
 (0)