RawDog

1.0.2

RawDog is a Swift library that extends RawRepresentable types with arithmetic operations
ChristianFox/RawDog

What's New

v1.0.2

2023-04-28T23:22:14Z
  • Updated Readme, no code changes

Latest Version Swift Platforms

Swift Package Manager Cocoapods Cathage Manually

CodeCoverage License Contribution First Timers Friendly

Size Files

RawDog

RawDog is a Swift library that extends RawRepresentable types with arithmetic operations. It defines two protocols and an enum to handle arithmetic for types with fallible initializers and infallible initializers.

Types

  • ArithmeticForFallibleType: This protocol is for types that have a fallible initializer. The arithmetic operations return optional values to account for the possibility of failure.
  • ArithmeticForInfallibleType: This protocol is for types that have an infallible initializer. The arithmetic operations always succeed and return non-optional values.
  • BinaryIntegerDivisionStrategy: This enum defines three strategies for handling division of binary integers: floor, round, and ceiling.

๐Ÿงช Tests

The library is fully tested with 100% code coverage

๐Ÿ’ป Installation

Swift Package Manager

To add RawDog to your project using Swift Package Manager, add the following dependency in your Package.swift file:

dependencies: [
    .package(url: "https://github.com/ChristianFox/RawDog.git", from: "1.0.0")
]

Don't forget to add RawDog to your target dependencies:

targets: [
    .target(
        name: "YourTarget",
        dependencies: ["RawDog"]),
]

๐Ÿ› ๏ธ Usage

To use RawDog conform your RawRepresentable type to either the ArithmeticForFallibleType or ArithmeticForInfallibleType protocol, depending on whether your type has a fallible or infallible initializer.

Example

Here's an example of a struct that represents SISeconds with raw values (Based on Dave DeLong's Time, the inspiration for the library), conforming to the ArithmeticForInfallibleType protocol:

import RawDog

struct SISeconds: RawRepresentable, ArithmeticForInfallibleType {

	let rawValue: Double
	init(rawValue: Double) {
		self.rawValue = rawValue
	}
}

Simply by conforming to the ArithmeticForInfallibleType protocol you can now perform any of the following operations:

var a = SISeconds(rawValue: 60)
let b = SISeconds(rawValue: 3600)

let c = a + b // 3660
let d = a - b // 60
let e = a * b // 216000
let f = a / b // 60
a += b // 3660
a -= b // 60
a *= b // 216000
a /= b // 60

let aDouble = 2.0
let g = a ~+ aDouble // 62
let h = a ~- aDouble // 60
let i = a ~* aDouble // 120
let j = a ~/ aDouble // 60
a ~+= aDouble // 62
a ~-= aDouble // 60
a ~*= aDouble // 120
a ~/= aDouble // 60

๐Ÿ•โ€๐Ÿฆบ Support

Please open an issue for support.

๐Ÿ‘ทโ€โ™‚๏ธ Contributing

Pull requests are welcome. I welcome developers of all skill levels to help improve the library, fix bugs, or add new features.

For major changes, please open an issue first to discuss what you would like to change.

Before submitting a pull request, please ensure that your code adheres to the existing code style and conventions, and that all tests pass. Additionally, if you're adding new functionality, please make sure to include unit tests to verify the behavior.

If you have any questions or need assistance, feel free to open an issue, and I'll do my best to help you out.

๐Ÿชช Licence

RawDog is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Jan 15 2024 08:20:44 GMT-1000 (Hawaii-Aleutian Standard Time)