Skip to content

Commit

Permalink
Merge d20a732 into b216be6
Browse files Browse the repository at this point in the history
  • Loading branch information
osyrisrblx committed Jul 31, 2019
2 parents b216be6 + d20a732 commit 15f0e8b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ There's also type checks for arrays and interfaces but we'll cover those in thei

t includes a few special functions for checking numbers, these can be useful to ensure the given value is within a certain range.

**General:**

**General:**\
**`t.nan`**\
determines if value is `NaN`\
All of the following checks will not pass for `NaN` values.\
Expand Down Expand Up @@ -170,6 +169,13 @@ checks `t.number` and determines if value < max
**`t.numberConstrainedExclusive(min, max)`**\
checks `t.number` and determines if min < value < max

## Special String Functions

t includes a few special functions for checking strings

**`t.match(pattern)`**\
checks `t.string` and determines if value matches the pattern via `string.match(value, pattern)`

## Arrays
In Lua, arrays are a special type of table where all the keys are sequential integers.\
t has special functions for checking against arrays.
Expand Down
23 changes: 23 additions & 0 deletions lib/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,29 @@ function t.numberConstrainedExclusive(min, max)
end
end

--[[**
ensures value matches string pattern
@param string pattern to check against
@returns A function that will return true iff the condition is passed
**--]]
function t.match(pattern)
assert(t.string(pattern))
return function(value)
local stringSuccess, stringErrMsg = t.string(value)
if not stringSuccess then
return false, stringErrMsg
end

if string.match(value, pattern) == nil then
return false, string.format("\"%s\" failed to match pattern \"%s\"", value, pattern)
end

return true
end
end

--[[**
ensures value is either nil or passes check
Expand Down
6 changes: 6 additions & 0 deletions lib/init.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,10 @@ return function()
assert(not (myInterface({ buttonInFrame = frame })))
end
end)

it("should support t.match", function()
local check = t.match("%d+")
assert(check("123"))
assert(not (check("abc")))
end)
end
2 changes: 2 additions & 0 deletions lib/t.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ interface t {
numberConstrained: (min: number, max: number) => (value: unknown) => value is number;
/** checks to see if `value` is a number and `min < value < max` */
numberConstrainedExclusive: (min: number, max: number) => (value: unknown) => value is number;
/** checks `t.string` and determines if value matches the pattern via `string.match(value, pattern)` */
match: (pattern: string) => check<string>;
/** checks to see if `value` is either nil or passes `check` */
optional: <T>(check: (value: unknown) => value is T) => check<T | undefined>;
/** checks to see if `value` is a table and if its keys match against `check */
Expand Down

0 comments on commit 15f0e8b

Please sign in to comment.