Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Regex types #250

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 57 additions & 42 deletions types/regex.luau
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,82 @@ local RegexMatch = {

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("(?<day>[0-9]{2})-(?<month>[0-9]{2})-(?<year>[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("(?<day>[0-9]{2})-(?<month>[0-9]{2})-(?<year>[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 = {}

Expand Down