From d20a7321bad3725965bf8412b07c912f2a24522c Mon Sep 17 00:00:00 2001 From: osyrisrblx Date: Wed, 31 Jul 2019 11:11:30 -0700 Subject: [PATCH] add t.match --- README.md | 10 ++++++++-- lib/init.lua | 23 +++++++++++++++++++++++ lib/init.spec.lua | 6 ++++++ lib/t.d.ts | 2 ++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c0e1957..7a7d461 100644 --- a/README.md +++ b/README.md @@ -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.\ @@ -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. diff --git a/lib/init.lua b/lib/init.lua index 4861cd4..f770b8b 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -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 diff --git a/lib/init.spec.lua b/lib/init.spec.lua index 5ffe321..b61990c 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -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 \ No newline at end of file diff --git a/lib/t.d.ts b/lib/t.d.ts index f17943c..6e09e5b 100644 --- a/lib/t.d.ts +++ b/lib/t.d.ts @@ -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; /** checks to see if `value` is either nil or passes `check` */ optional: (check: (value: unknown) => value is T) => check; /** checks to see if `value` is a table and if its keys match against `check */