From d6e172bbb868555d4e4c7e1b42554a82a21da639 Mon Sep 17 00:00:00 2001 From: HowManySmall Date: Sat, 31 Aug 2024 18:34:29 -0600 Subject: [PATCH 1/2] fixes regex types --- types/regex.luau | 109 +++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 47 deletions(-) diff --git a/types/regex.luau b/types/regex.luau index 59756f37..46b9fb99 100644 --- a/types/regex.luau +++ b/types/regex.luau @@ -11,75 +11,90 @@ - `len` -- The length of the text that was matched. ]=] local RegexMatch = { - start = 0, - finish = 0, - text = "", - len = 0, + start = 0; + finish = 0; + text = ""; + len = 0; } type RegexMatch = typeof(RegexMatch) ---[=[ - @class RegexCaptures - - Captures from a regular expression. -]=] local RegexCaptures = {} ---[=[ - @within RegexCaptures - @tag Method - - Returns the match at the given index, if one exists. - - @param index -- The index of the match to get - @return RegexMatch -- The match, if one exists -]=] function RegexCaptures.get(self: RegexCaptures, index: number): RegexMatch? return nil :: any end ---[=[ - @within RegexCaptures - @tag Method - - Returns the match for the given named match group, if one exists. - - @param group -- The name of the group to get - @return RegexMatch -- The match, if one exists -]=] function RegexCaptures.group(self: RegexCaptures, group: string): RegexMatch? return nil :: any end +function RegexCaptures.format(self: RegexCaptures, format: string): string + return nil :: any +end + --[=[ - @within RegexCaptures - @tag Method + @class RegexCaptures - Formats the captures using the given format string. + Captures from a regular expression. +]=] +export type RegexCaptures = typeof(setmetatable( + {} :: { + --[=[ + @within RegexCaptures + @tag Method + @method get - ### Example usage + Returns the match at the given index, if one exists. - ```lua - local regex = require("@lune/regex") + @param index -- The index of the match to get + @return RegexMatch -- The match, if one exists + ]=] - local re = regex.new("(?[0-9]{2})-(?[0-9]{2})-(?[0-9]{4})") + get: (self: RegexCaptures, index: number) -> RegexMatch?, - local caps = re:captures("On 14-03-2010, I became a Tenneessee lamb."); - assert(caps ~= nil, "Example pattern should match example text") + --[=[ + @within RegexCaptures + @tag Method + @method group - local formatted = caps:format("year=$year, month=$month, day=$day") - print(formatted) -- "year=2010, month=03, day=14" - ``` + Returns the match for the given named match group, if one exists. - @param format -- The format string to use - @return string -- The formatted string -]=] -function RegexCaptures.format(self: RegexCaptures, format: string): string - return nil :: any -end + @param group -- The name of the group to get + @return RegexMatch -- The match, if one exists + ]=] + group: (self: RegexCaptures, group: string) -> RegexMatch?, + + --[=[ + @within RegexCaptures + @tag Method + @method format + + Formats the captures using the given format string. + + ### Example usage + + ```lua + local regex = require("@lune/regex") + + local re = regex.new("(?[0-9]{2})-(?[0-9]{2})-(?[0-9]{4})") + + local caps = re:captures("On 14-03-2010, I became a Tenneessee lamb."); + assert(caps ~= nil, "Example pattern should match example text") + + local formatted = caps:format("year=$year, month=$month, day=$day") + print(formatted) -- "year=2010, month=03, day=14" + ``` -export type RegexCaptures = typeof(RegexCaptures) + @param format -- The format string to use + @return string -- The formatted string + ]=] + format: (self: RegexCaptures, format: string) -> string, + }, + {} :: { + __len: (self: RegexCaptures) -> number, + } +)) local Regex = {} @@ -138,7 +153,7 @@ end @param text -- The text to split @return { string } -- The split text ]=] -function Regex.split(self: Regex, text: string): { string } +function Regex.split(self: Regex, text: string): {string} return nil :: any end From 7936cf929906c2975fde426d36c885423a5f7e1f Mon Sep 17 00:00:00 2001 From: HowManySmall Date: Mon, 2 Sep 2024 12:57:50 -0600 Subject: [PATCH 2/2] fix formatting --- types/regex.luau | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/types/regex.luau b/types/regex.luau index 46b9fb99..068b8dda 100644 --- a/types/regex.luau +++ b/types/regex.luau @@ -11,10 +11,10 @@ - `len` -- The length of the text that was matched. ]=] local RegexMatch = { - start = 0; - finish = 0; - text = ""; - len = 0; + start = 0, + finish = 0, + text = "", + len = 0, } type RegexMatch = typeof(RegexMatch) @@ -153,7 +153,7 @@ end @param text -- The text to split @return { string } -- The split text ]=] -function Regex.split(self: Regex, text: string): {string} +function Regex.split(self: Regex, text: string): { string } return nil :: any end