Skip to content

Commit

Permalink
add conversions and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
co1emi11er2 committed Sep 9, 2024
1 parent 403e44c commit f407b33
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 9 deletions.
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StructuralUnits"
uuid = "ec81c399-378c-4a82-baa1-80fb2fc85b6c"
authors = ["Cole Miller"]
version = "0.1.0"
version = "0.2.0"

[deps]
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Expand All @@ -14,8 +14,8 @@ UnitfulLatexify = "^1"
Reexport = "^1"
julia = "1.10"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
# [extras]
# Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
# [targets]
# test = ["Test"]
158 changes: 158 additions & 0 deletions src/Conversions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# ----------------
# ft
# ----------------
"""
to_ft(x)
Converts x to a Unitful quantity with unit `ft`.
"""
to_ft(x::Real) = x * ft
to_ft(x::String) = parse(Float64, x) * ft
to_ft(x::Quantity) = x
function to_ft(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end


# ----------------
# inch
# ----------------
"""
to_inch(x)
Converts x to a Unitful quantity with unit `inch`.
"""
to_inch(x::Real) = x * inch
to_inch(x::String) = parse(Float64, x) * inch
to_inch(x::Quantity) = x
function to_inch(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end


# ----------------
# inch2
# ----------------
"""
to_inch2(x)
Converts x to a Unitful quantity with unit `inch^2`.
"""
to_inch2(x::Real) = x * inch^2
to_inch2(x::String) = parse(Float64, x) * inch^2
to_inch2(x::Quantity) = x
function to_inch2(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end


# ----------------
# pcf
# ----------------
"""
to_pcf(x)
Converts x to a Unitful quantity with unit `pcf`.
"""
to_pcf(x::Real) = x * pcf
to_pcf(x::String) = parse(Float64, x) * pcf
to_pcf(x::Quantity) = x
function to_pcf(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end


# ----------------
# kcf
# ----------------
"""
to_kcf(x)
Converts x to a Unitful quantity with unit `kcf`.
"""
to_kcf(x::Real) = x * kcf
to_kcf(x::String) = parse(Float64, x) * kcf
to_kcf(x::Quantity) = x
function to_kcf(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end


# ----------------
# plf
# ----------------
"""
to_plf(x)
Converts x to a Unitful quantity with unit `plf`.
"""
to_plf(x::Real) = x * plf
to_plf(x::String) = parse(Float64, x) * plf
to_plf(x::Quantity) = x
function to_plf(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end


# ----------------
# klf
# ----------------
"""
to_klf(x)
Converts x to a Unitful quantity with unit `plf`.
"""
to_klf(x::Real) = x * klf
to_klf(x::String) = parse(Float64, x) * klf
to_klf(x::Quantity) = x
function to_klf(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end


# ----------------
# ksi
# ----------------
"""
to_ksi(x)
Converts x to a Unitful quantity with unit `ksi`.
"""
to_ksi(x::Real) = x * ksi
to_ksi(x::String) = parse(Float64, x) * ksi
to_ksi(x::Quantity) = x
function to_ksi(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end


# ----------------
# mph
# ----------------
"""
to_mph(x)
Converts x to a Unitful quantity with unit `mph`.
"""
to_mph(x::Real) = x * mph
to_mph(x::String) = parse(Float64, x) * mph
to_mph(x::Quantity) = x
function to_mph(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end


# ----------------
# deg
# ----------------
"""
to_deg(x)
Converts x to a Unitful quantity with unit `deg`.
"""
to_deg(x::Real) = x * °
to_deg(x::String) = parse(Float64, x) * °
to_deg(x::Quantity) = x
function to_deg(x)
ErrorException("Incorrect Type. Expected float, int, or string, got $(typeof(x))")
end
3 changes: 3 additions & 0 deletions src/StructuralUnits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Reexport
@reexport using UnitfulLatexify

export ft, inch, kip, ksi, ksf, klf, plf, mph, kcf, pcf, °
export to_ft, to_inch, to_inch2, to_pcf, to_kcf, to_plf, to_klf, to_ksi, to_mph, to_deg

Unitful.register(StructuralUnits)
@unit kip "kip" Kip 1000*u"lbf" false
Expand Down Expand Up @@ -46,6 +47,8 @@ const kcf = u"kcf"
const pcf = u"pcf"
const ° = u"°"

include("Conversions.jl")

const localpromotion = copy(Unitful.promotion)
function __init__()
Unitful.register(StructuralUnits)
Expand Down
71 changes: 71 additions & 0 deletions test/Conversions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using StructuralUnits

# ft conversion
let
@test 5.0ft == to_ft(5)
@test 5.0ft == to_ft("5")
@test 1.0ft == to_ft(12inch)
end

# inch conversion
let
@test 5.0inch == to_inch(5)
@test 5.0inch == to_inch("5")
@test 12.0inch == to_inch(1ft)
end

# inch^2 conversion
let
@test 5.0inch^2 == to_inch2(5)
@test 5.0inch^2 == to_inch2("5")
@test 144.0inch^2 == to_inch2(1ft^2)
end

# pcf conversion
let
@test 5.0pcf == to_pcf(5)
@test 5.0pcf == to_pcf("5")
@test 1000.0pcf == to_pcf(1kcf)
end

# kcf conversion
let
@test 5.0kcf == to_kcf(5)
@test 5.0kcf == to_kcf("5")
@test 1.0kcf == to_kcf(1000pcf)
end

# plf conversion
let
@test 5.0plf == to_plf(5)
@test 5.0plf == to_plf("5")
@test 1000.0plf == to_plf(1klf)
end

# plf conversion
let
@test 5.0klf == to_klf(5)
@test 5.0klf == to_klf("5")
@test 1.0klf == to_klf(1000plf)
end

# ksi conversion
let
@test 5.0ksi == to_ksi(5)
@test 5.0ksi == to_ksi("5")
@test 1.0ksi == to_ksi(1ksi)
end

# mph conversion
let
@test 5.0mph == to_mph(5)
@test 5.0mph == to_mph("5")
@test 1.0mph == to_mph(1mph)
end

# mph conversion
let
@test 5.0° == to_deg(5)
@test 5.0° == to_deg("5")
@test 1.0° == to_deg(1°)
end
Loading

2 comments on commit f407b33

@co1emi11er2
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/120175

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" f407b337ffb778c692139b321923a848eecf6775
git push origin v0.2.0

Please sign in to comment.