units

package module
v0.0.0-...-6c28c39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2023 License: MIT Imports: 5 Imported by: 0

README

units

units offers shared code for handling standard units of measurement. It takes inspiration from Golang's time library and applies similar ideas to other units of measure. This is achieved through a common declaration called a Unit, which organizes measurements in increasing order, often derived from a base unit. A Unit also facilitates parsing and formatting of measurements to improve computer use and human readability (respectively). To get a better understanding of its functioning, you can explore the provided packages for more specific information.

import "go.pitz.tech/units"

Usage

Customize existing units

One nice thing about the way this library works is that it gives you the ability to define your own custom units used to format and parse text. Using the existing length package, we can reduce the number of units down to the measures we care about. Now, you can use this new unit to parse string text or format lengths as strings. For example, with ballistics we seldom care about millimeters and decameters and instead focus on kilometer (range to target), meter (size of target), and centimeter (drop of projectile). Similarly in the US, we think of this as yards (range to target), feet (size of target), and inches (drop of projectile).

package main

import "go.pitz.tech/units/length"
import "go.pitz.tech/units"

var (
	simplified = units.Unit[length.Length]{
		{length.Centimeter, []string{"cm"}},
		{length.Meter, []string{"m"}},
		{length.Kilometer, []string{"km"}},
	}
)

Declare your own

If customizing an existing unit isn't enough for you, then you can always look at declaring your own unit. For example, the other day I learned that in x-ray astronomy, flux density can be measured using crabs or millicrabs. To illustrate how we might define this using the units package, we can consider the following snippet.

package astronomy

import "go.pitz.tech/units"

type FluxDensity int64

const (
	MilliCrab FluxDensity = 1
	Crab FluxDensity = 1000 * MilliCrab
)

var (
	Standard = units.Unit[Flux]{
		{MilliCrab, []string{"??"}},
		{Crab, []string{"??"}},
	}
)

Now, I'm no astronomer so it's entirely possible that these representative values are completely incorrect. However, these values do illustrate how we can apply this library to other areas.

License

Copyright (c) 2023 Mya Pitzeruse

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package units offers shared code for handling standard units of measurement. It takes inspiration from Golang's time library and applies similar ideas to other units of measure. This is achieved through a common declaration called a Unit, which organizes measurements in increasing order, often derived from a base unit. A Unit also facilitates parsing and formatting of measurements to improve computer use and human readability (respectively). To get a better understanding of its functioning, you can explore the provided packages for more specific information.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrValueDoesNotMatchPattern notifies the caller that the provided string text did not match our expected format.
	ErrValueDoesNotMatchPattern = fmt.Errorf("value does not match pattern")
)

Functions

This section is empty.

Types

type Number

type Number interface {
	constraints.Signed
}

Number defines a constraint to ensure the values provided to units are integer based (i.e. we're working with whole numbers). This makes sure we're working with whole numbers and not handling fractions internally. This forces the programmer to handle all rounding and truncation.

type Symbol

type Symbol[T Number] struct {
	Size  T
	Label []string
}

Symbol defines how various sizes should be labeled. Some values may contain multiple labels, but the preferred label that will be used when printing should come first in the list.

type Unit

type Unit[T Number] []Symbol[T]

A Unit of measure is a standardized quantity used to quantify and express the magnitude or value of a physical quantity. It establishes a reference point or a standard against which measurements can be made and compared. Units of measure provide a consistent and universally understood way to communicate and exchange information about quantities in various domains, such as length, mass, time, temperature, volume, and many others.

func (Unit[T]) Format

func (u Unit[T]) Format(value T) (str string)

Format uses the underlying Unit to convert the provided value to a human-readable string. The benefit to this abstraction is that so long as a unit shares a common base unit, multiple formats can be used to represent the underlying value (for example, metric vs imperial).

func (Unit[T]) Parse

func (u Unit[T]) Parse(val string) (size T, err error)

Parse attempts to convert the provided string value to its equivalent numeric representation.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL