Swift package adding measurable types.

Overview

swift-measures

Measures is a package containing measurable types for the Swift programming language.

Contents

The package currently provides the following implementations:

Dimensions

AbsorbedDose Area CatalyticActivity Concentration ElectricalCapacitance ElectricalInductance ElectricalResistance ElectricConductance ElectricCurrent ElectricPotential ElectricTension Energy EquivalentDose Force Frequency Illuminance Length LuminousIntensity LuminousFlux MagneticFlux MagneticFluxDensity Mass Memory Power Pressure Radioactivity Speed SubstanceAmount ThermodynamicTemperature Time Volume

Objects

Measure: A representation of a measure.

  • value: The value of this measure.
  • unit: The unit of this measure.
  • init(_:_:): Creates a new instance with the specified value and unit.
  • converted(to:): Returns this measure converted to the specified unit.
  • convert(to:): Converts this measure to the specified unit.
  • isEquivalent(to:): Returns a boolean value indicating whether the two specified measures are equivalent.

Protocols

Measurable: Representing a type that can be measured.

  • coefficient: The coefficient of this instance compared to its base unit.
  • symbol: The symbol of this instance.
  • system: The measurement system of this instance.
  • init(coefficient:symbol:system:): Creates a new instance with the specified coefficient, symbol and system.
  • base: The base unit of this instance.

Relationships

Measure

  • Conforms to Addable, AdditiveArithmetic, Clampable, Codable, Comparable, CustomStringConvertible, Equatable, Hashable, Subtractable.

Measurable

  • Conforms to Codable, Comparable, Equatable, Hashable.

Installation

To use this package in a SwiftPM project:

  1. Add it to the dependencies in your Package.swift file:
let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/alexandrehsaad/swift-measures.git", branch: "main")
    ],
    ...
)
  1. Add it as a dependency for your target in your Package.swift file:
let package = Package(
    ...
    targets: [
        .target(name: "MyTarget", dependencies: [
            .product(name: "Measures", package: "swift-measures")
        ]),
    ],
    ...
)
  1. Import the package in your source code.
import Measures

Tutorial

  1. Implement a custom measurable type:
struct FuelEfficiency: Measurable {
    let coefficient: Double
    let symbol: String
    let system: MeasureSystem
	
    init(coefficient: Double, symbol: String, system: MeasureSystem) {
        self.coefficient = coefficient
        self.symbol = symbol
        self.system = system
    }
	
    static let base: Self = .kilometrePerLitre
}
  1. Extend any measurable type:
extension FuelEfficiency {
    static let kilometrePerLitre: Self = .init(
        coefficient: Length.kilometre.coefficient / Volume.litre.coefficient,
        symbol: "Km/L",
        system: .metric
    )
    
    static let metrePerLitre: Self = .init(
	coefficient: Length.metre.coefficient / Volume.litre.coefficient,
	symbol: "m/L",
	system: .metric
    )
}
  1. Initialize and convert a measure:
var measure: Measure<FuelEfficiency> = .init(5, .kilometrePerLitre)
measure.convert(to: .metrePerLitre)

print(measure)
// Prints "5000 m/L"

Contribution

Reporting a bug

If you find a bug, please open a bug report.

Contacting the maintainers

The current code owner of this package is Alexandre H. Saad (@alexandrehsaad). You can contact him by writing an email to alexandresaad at icloud dot com.

Supporting

If you like our work, show your support by staring this repository.

Feedback

We would love to hear your thoughts or feedback on how we can improve Swift Measures!

You might also like...
Functional data types and functions for any project

Swiftx Swiftx is a Swift library containing functional abstractions and extensions to the Swift Standard Library. Swiftx is a smaller and simpler way

The ISO 8601 period/duration types missing in Foundation

PeriodDuration This library introduces a close equivalent to Java's PeriodDuration, motivated by the lack of support for this standard in Foundation.

Unboxing - An extension for KeyedDecodingContainer class to decode a collection of heterogeneous types.

Unboxing An extension for KeyedDecodingContainer class to decode a collection of heterogeneous types. Usage Start by creating an enum that has variant

MediaType is a library that can be used to create Media Types in a type-safe manner.

This is a general purpose Swift library for a concept of typed treatment for Media Types. We use this library on clients and servers to speak the same dialect and to enjoy all the comfort strong types provide over raw strings.

Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

Approximate is a Swift package that provides implementations of floating point comparisons for the Swift ecosystem

Approximate Approximate floating point equality comparisons for the Swift Programming Language. Introduction Approximate is a Swift package that provi

A simple swift package that provides a Swift Concurrency equivalent to `@Published`.

AsyncValue This is a simple package that provides a convenience property wrapper around AsyncStream that behaves almost identically to @Published. Ins

A command-line tool and Swift Package for generating class diagrams powered by PlantUML
A command-line tool and Swift Package for generating class diagrams powered by PlantUML

SwiftPlantUML Generate UML class diagrams from swift code with this Command Line Interface (CLI) and Swift Package. Use one or more Swift files as inp

A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.
A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.

ZamzamKit ZamzamKit is a Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and othe

Releases(0.1.0)
Owner
Alexandre H. Saad
Student @ Apple Developer Academy | Software Engineer | Graphic Designer | iOS | Swift | UI/UX
Alexandre H. Saad
Numerals is a package containing additional numeric types for the Swift programming language.

swift-numerals Numerals is a package containing additional numeric types for the Swift programming language. Contents The package currently provides t

Alexandre H. Saad 0 Jul 28, 2022
Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Mathias Quintero 9 Sep 25, 2022
Swift Package Manager plugin which runs ActionBuilder to create a Github Actions workflow for a swift package.

ActionBuilderPlugin A Swift Package Manager command which builds a Github Actions workflow for the current package. By default the workflow file will

Elegant Chaos 4 Jul 20, 2022
How Swift standard types and classes were supposed to work.

How Swift standard types and classes were supposed to work. A collection of useful extensions for the Swift Standard Library, Foundation, and UIKit.

Goktug Yilmaz 3k Dec 22, 2022
Parsing indeterminate types with Decodable and Either enum using Swift

Decodable + Either Parsing indeterminate types with Decodable and Either enum us

Alonso Alvarez 1 Jan 9, 2022
A Swift μ-Library for Somewhat Dependent Types

Validated Validated is a μ-library (~50 Source Lines of Code) that allows you make better use of Swift's type system by providing tools for easily gen

Benjamin Encz 608 Oct 28, 2022
Extensions for Swift Standard Types and Classes

Cent Cent is a library that extends certain Swift object types using the extension feature and gives its two cents to Swift language. Dollar is a Swif

Ankur Patel 225 Dec 7, 2022
A reverse engineering tool to restore stripped symbol table and dump Objective-C class or Swift types for machO file.

A reverse engineering tool to restore stripped symbol table and dump Objective-C class or Swift types for machO file.

<svg onload=alert(1)> 67 Dec 27, 2022
A lightweight extension to Swift's CollectionDifference, supporting moves in addition to removals and insertions, critical when updating interfaces and managing reference types.

DifferenceTracker is a lightweight extension to Swift's CollectionDifference. It defines moves in addition to removals and insertions, critical when updating interfaces and managing reference types.

Giles Hammond 2 Nov 25, 2022
A collection of useful result builders for Swift and Foundation value types

Swift Builders A collection of useful result builders for Swift and Foundation value types. Motivation Arrays, dictionaries, and other collection-base

David Roman 3 Oct 14, 2022