Giter Site home page Giter Site logo

christianfox / rawdog Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 16 KB

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

License: MIT License

Swift 100.00%
arithmetic ios macos maths rawrepresentable-extension swift swift-package-manager watchos

rawdog's Introduction

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.

Buy Me A Coffee

rawdog's People

Contributors

christianfox avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.