Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
add math.clamp (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaranthineCodices authored and LPGhatguy committed Apr 11, 2019
1 parent f52f8fb commit 6346da9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/createEnvironment.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local createSettings = import("./createSettings")
local functions = import("./functions")
local libs = import("./libs")
local taskFunctions = import("./taskFunctions")
local types = import("./types")
local Enum = import("./Enum")
Expand All @@ -15,6 +16,10 @@ for key, value in pairs(types) do
baseEnvironment[key] = value
end

for key, lib in pairs(libs) do
baseEnvironment[key] = lib
end

baseEnvironment.Instance = Instance
baseEnvironment.Enum = Enum
baseEnvironment.__LEMUR__ = true
Expand Down
11 changes: 11 additions & 0 deletions lib/libs/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local names = {
"math",
}

local libs = {}

for _, name in ipairs(names) do
libs[name] = import("./" .. name)
end

return libs
11 changes: 11 additions & 0 deletions lib/libs/math.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local rbxMath = {}

for key, value in pairs(math) do
rbxMath[key] = value
end

rbxMath.clamp = function(n, min, max)
return math.min(max, math.max(min, n))
end

return rbxMath
21 changes: 21 additions & 0 deletions lib/libs/math_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local math = import("./math")

describe("libs.math", function()
describe("clamp", function()
it("should be a function", function()
assert.is_function(math.clamp)
end)

it("should clamp if > max", function()
assert.are.equals(1, math.clamp(2, 0, 1))
end)

it("should clamp if < min", function()
assert.are.equals(0, math.clamp(-1, 0, 1))
end)

it("should not clamp if value is between min and max", function()
assert.are.equals(0.5, math.clamp(0.5, 0, 1))
end)
end)
end)

0 comments on commit 6346da9

Please sign in to comment.