Skip to content

Commit

Permalink
Merge pull request #2 from 8rightside/feature
Browse files Browse the repository at this point in the history
nextUp and nextDown convenience functions added
  • Loading branch information
8rightside authored Nov 12, 2022
2 parents 660083f + bd61b9c commit 3a0219d
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ let nextUp2 = e12.nextValueUp(from: nonPreferredValue)
*/
let nextDown1 = e12.nextValueDown(from: preferredValue)
let nextDown2 = e12.nextValueDown(from: nonPreferredValue)
/*:
### nextValue Convenience Functions
In addition all of the `Resistor` types contain convenience functions for calculating the next
value up or down for a given `ESeries`.
*/
let inSeries = FourBandResistor(value: 4700)
let nextUpInSeries = inSeries.nextValueUp(inSeries: E24Series())

let notInSeries = FiveBandResistor(value: 570)
let nextDownInSeries = notInSeries.nextValueDown(inSeries: E24Series())
//: [Example Use Case >](@next)
Binary file modified Documentation.playground/Resources/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions Sources/Resistance/E Series/ESeriesProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* MIT license, see LICENSE file for details
*/

// MARK:- API
// MARK: - API
import Foundation

public protocol ESeriesProtocol {
Expand Down Expand Up @@ -66,7 +66,7 @@ extension ESeriesProtocol {
}
}

// MARK:- Internal
// MARK: - Internal
extension ESeriesProtocol {
private func calculateNextValue(from value: Double, reverseSorted: Bool = false) -> Double {
let sigFigs = value.hundredsDecade
Expand Down
10 changes: 5 additions & 5 deletions Sources/Resistance/Internal/BandsCalculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* MIT license, see LICENSE file for details
*/

// MARK:- Four Band
// MARK: - Four Band
extension BandsCalculator {
static func fourBandColors(value: Double) -> (digit1: Digit, digit2: Digit, multiplier: Multiplier) {
guard value >= fourBandMin else { return calculateFourBandColors(value: fourBandMin) }
Expand All @@ -23,7 +23,7 @@ extension BandsCalculator {
}
}

// MARK:- Five Band
// MARK: - Five Band
extension BandsCalculator {
static func fiveBandColors(value: Double) -> (digit1: Digit, digit2: Digit, digit3: Digit, multiplier: Multiplier) {
guard value >= fiveBandMin else { return calculateFiveBandColors(value: fiveBandMin) }
Expand All @@ -42,18 +42,18 @@ extension BandsCalculator {
}
}

// MARK:- Internal
// MARK: - Internal
import Foundation

enum BandsCalculator {
// MARK:- Constants
// MARK: - Constants
static let fourBandMin: Double = 0.1
static let fourBandMax: Double = 99_000_000_000
static let fiveBandMin: Double = 1
static let fiveBandMax: Double = 999_000_000_000
}

// MARK:- Functions
// MARK: - Functions
extension BandsCalculator {
private static func calculateFourBandColors(value: Double) -> (digit1: Digit, digit2: Digit, multiplier: Multiplier) {
var sigfigs = value.hundredsDecade
Expand Down
14 changes: 14 additions & 0 deletions Sources/Resistance/Protocols/BandedResistor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ extension BandedResistor {
}
return SixBandResistor(resistor: self, tolerance: self.tolerance, coefficient: coefficient)
}

/// Returns the value of the next resistor up from the passed E Series
/// - Parameter eSeries: E Series from which to get next value
/// - Returns: `Double` representing next value up
public func nextValueUp(inSeries eSeries: ESeriesProtocol) -> Double {
return eSeries.nextValueUp(from: self)
}

/// Returns the value of the next resistor down from the pass E Series
/// - Parameter eSeries: E Series from which to get next value
/// - Returns: `Double` representing next value down
public func nextValueDown(inSeries eSeries: ESeriesProtocol) -> Double {
return eSeries.nextValueDown(from: self)
}
}

// MARK: Custom String Convertible
Expand Down
6 changes: 3 additions & 3 deletions Sources/Resistance/Resistor/FiveBandResistor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public struct FiveBandResistor: BandedResistor {
}
}

// MARK:- Convenience Inits
// MARK: - Convenience Inits
extension FiveBandResistor {
public init(value: Double, tolerance: Tolerance = .gold) {
let colors = BandsCalculator.fiveBandColors(value: value)
Expand All @@ -38,15 +38,15 @@ extension FiveBandResistor {
}
}

// MARK:- Failable Inits
// MARK: - Failable Inits
extension FiveBandResistor {
public init(exactValue: Double, tolerance: Tolerance = .gold) throws {
let colors = try BandsCalculator.fiveBandColorsOrFail(value: exactValue)
self.init(digit1: colors.digit1, digit2: colors.digit2, digit3: colors.digit3, multiplier: colors.multiplier, tolerance: tolerance)
}
}

// MARK:- Multiplier Functions
// MARK: - Multiplier Functions
extension FiveBandResistor {
public func decadeUp() -> FiveBandResistor {
guard multiplier != .white else { return self }
Expand Down
8 changes: 4 additions & 4 deletions Sources/Resistance/Resistor/FourBandResistor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Foundation

// MARK:- Properties
// MARK: - Properties
public struct FourBandResistor: BandedResistor {
let digit1: Digit
let digit2: Digit
Expand All @@ -25,7 +25,7 @@ public struct FourBandResistor: BandedResistor {
}
}

// MARK:- Convenience Inits
// MARK: - Convenience Inits
extension FourBandResistor {
public init(value: Double, tolerance: Tolerance = .gold) {
let colors = BandsCalculator.fourBandColors(value: value)
Expand All @@ -37,7 +37,7 @@ extension FourBandResistor {
}
}

// MARK:- Failable Inits
// MARK: - Failable Inits
extension FourBandResistor {
public init(exactValue: Double, tolerance: Tolerance = .gold) throws {
let colors = try BandsCalculator.fourBandColorsOrFail(value: exactValue)
Expand All @@ -49,7 +49,7 @@ extension FourBandResistor {
}
}

// MARK:- Multiplier Functions
// MARK: - Multiplier Functions
extension FourBandResistor {
public func decadeUp() -> FourBandResistor {
guard multiplier != .white else { return self }
Expand Down
6 changes: 3 additions & 3 deletions Sources/Resistance/Resistor/SixBandResistor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public struct SixBandResistor: BandedResistor {
}
}

// MARK:- Convenience Inits
// MARK: - Convenience Inits
extension SixBandResistor {
public init(value: Double, tolerance: Tolerance = .gold, coefficient: Coefficient = .brown) {
let colors = BandsCalculator.fiveBandColors(value: value)
Expand All @@ -53,15 +53,15 @@ extension SixBandResistor {
}
}

// MARK:- Failable Inits
// MARK: - Failable Inits
extension SixBandResistor {
public init(exactValue: Double, tolerance: Tolerance = .gold, coefficient: Coefficient = .brown) throws {
let colors = try BandsCalculator.fiveBandColorsOrFail(value: exactValue)
self.init(digit1: colors.digit1, digit2: colors.digit2, digit3: colors.digit3, multiplier: colors.multiplier, tolerance: tolerance, coefficient: coefficient)
}
}

// MARK:- Multiplier Functions
// MARK: - Multiplier Functions
extension SixBandResistor {
public func decadeUp() -> SixBandResistor {
guard multiplier != .white else { return self }
Expand Down
6 changes: 3 additions & 3 deletions Tests/ResistanceTests/E Series/ESeriesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension ESeriesTests {
}
}

// MARK:- Next Value Up
// MARK: - Next Value Up
extension ESeriesTests {
func test_nextValueUp_with2Digits_notInSeries() {
let sut = E24Series()
Expand Down Expand Up @@ -100,7 +100,7 @@ extension ESeriesTests {
}
}

// MARK:- Next Value Down
// MARK: - Next Value Down
extension ESeriesTests {
func test_nextValueDown_with2Digits_notInSeries() {
let sut = E24Series()
Expand Down Expand Up @@ -139,7 +139,7 @@ extension ESeriesTests {
}
}

// MARK:- Internal
// MARK: - Internal
import XCTest
@testable import Resistance

Expand Down
10 changes: 5 additions & 5 deletions Tests/ResistanceTests/Resistor/DoubleExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension DoubleExtensionTests {
}
}

// MARK:- Sig Figs
// MARK: - Sig Figs
extension DoubleExtensionTests {
func test_sigFigs_1Digit() {
let value: Double = 4
Expand Down Expand Up @@ -77,7 +77,7 @@ extension DoubleExtensionTests {
}
}

// MARK:- Power Of Ten
// MARK: - Power Of Ten
extension DoubleExtensionTests {
func test_powerOfTen_1Digit() {
let value: Double = 4
Expand All @@ -100,7 +100,7 @@ extension DoubleExtensionTests {
}
}

// MARK:- Hundreds Decade
// MARK: - Hundreds Decade
extension DoubleExtensionTests {
func test_hundredsDecade_1Digit() {
let value: Double = 4
Expand Down Expand Up @@ -128,7 +128,7 @@ extension DoubleExtensionTests {
}
}

// MARK:- Sig Figs Rounded
// MARK: - Sig Figs Rounded
extension DoubleExtensionTests {
func test_sigFigsRounded_by1_5Digits() {
let value1: Double = 12457
Expand Down Expand Up @@ -166,7 +166,7 @@ extension DoubleExtensionTests {
}
}

// MARK:- Internal
// MARK: - Internal
import XCTest
@testable import Resistance

Expand Down
33 changes: 24 additions & 9 deletions Tests/ResistanceTests/Resistor/FiveBandResistorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* MIT license, see LICENSE file for details
*/

// MARK:- Resistance Value
// MARK: - Resistance Value
extension FiveBandResistorTests {
func test_value_min() {
let resistor = FiveBandResistor(digit1: .brown, digit2: .black, digit3: .black, multiplier: .silver, tolerance: .gold)
Expand Down Expand Up @@ -32,7 +32,7 @@ extension FiveBandResistorTests {
}
}

// MARK:- Description
// MARK: - Description
extension FiveBandResistorTests {
func test_description_1digit() {
let resistor = FiveBandResistor(digit1: .grey, digit2: .black, digit3: .black, multiplier: .silver, tolerance: .gold)
Expand Down Expand Up @@ -60,7 +60,7 @@ extension FiveBandResistorTests {
}
}

// MARK:- Tolerance Value Range
// MARK: - Tolerance Value Range
extension FiveBandResistorTests {
func test_toleranceValueRange_gold() {
let resistor = FiveBandResistor(digit1: .brown, digit2: .black, digit3: .black, multiplier: .brown, tolerance: .gold)
Expand All @@ -84,7 +84,7 @@ extension FiveBandResistorTests {
}
}

// MARK:- Init Value Tolerance
// MARK: - Init Value Tolerance
extension FiveBandResistorTests {
func test_init_value_belowMin() {
let resistor = FiveBandResistor(value: 0.5)
Expand Down Expand Up @@ -122,7 +122,7 @@ extension FiveBandResistorTests {
}
}

// MARK:- Init Resistor Tolerance
// MARK: - Init Resistor Tolerance
extension FiveBandResistorTests {
func test_init_resistor_4Band() {
let resistor = FourBandResistor(value: 3300)
Expand Down Expand Up @@ -165,7 +165,7 @@ extension FiveBandResistorTests {
}
}

// MARK:- Init Exact Value Tolerance
// MARK: - Init Exact Value Tolerance
extension FiveBandResistorTests {
func test_init_exactValue_belowMin() throws {
XCTAssertThrowsError(try FiveBandResistor(exactValue: 0.05))
Expand Down Expand Up @@ -194,7 +194,7 @@ extension FiveBandResistorTests {
}
}

// MARK:- Decade Functions
// MARK: - Decade Functions
extension FiveBandResistorTests {
func test_decadeUp() {
let sut = FiveBandResistor(digit1: .brown, digit2: .red, digit3: .orange, multiplier: .orange, tolerance: .gold)
Expand All @@ -221,7 +221,7 @@ extension FiveBandResistorTests {
}
}

// MARK:- Conversion Functions
// MARK: - Conversion Functions
extension FiveBandResistorTests {
func test_convertToFourBand() {
let sut = FiveBandResistor(value: 2200, tolerance: .orange)
Expand All @@ -246,7 +246,22 @@ extension FiveBandResistorTests {
}
}

// MARK:- Internal
// MARK: - Next Value Functions
extension FiveBandResistorTests {
func test_nextValueUp() {
let sut = FiveBandResistor(value: 5500)
let result = sut.nextValueUp(inSeries: E24Series())
XCTAssertEqual(result, 5600)
}

func test_nextValueDown() {
let sut = FiveBandResistor(value: 5500)
let result = sut.nextValueDown(inSeries: E24Series())
XCTAssertEqual(result, 5100)
}
}

// MARK: - Internal
import XCTest
import Resistance

Expand Down
Loading

0 comments on commit 3a0219d

Please sign in to comment.