Module:UtilsString/Documentation: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
m (MannedTooth moved page Module:UtilsString/doc to Module:UtilsString/Documentation without leaving a redirect: Text replacement - "/doc" to "/Documentation")
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
This module provides utility functions for manipulating Lua strings in general. For string manipulation and formatting that is specific to wikitext, see [[Module:UtilsMarkup]].
This module provides utility functions for manipulating Lua strings in general. For string manipulation and formatting that is specific to wikitext, see [[Module:UtilsMarkup]].
This module re-implements some of the functions in the {{Scribunto Manual|lib=Text library|<code>mw.text</code>}} library. Use these functions whenever possible — <code>mw.text</code> is an order of magnitude slower because it uses {{Scribunto Manual|lib=Using library|<code>mw.ustring</code>}}.
Like [[Module:UtilsTable]], some functions have both procedural and functional variants.
{{#invoke:Documentation|Module}}

Latest revision as of 15:37, 27 April 2020

This module provides utility functions for manipulating Lua strings in general. For string manipulation and formatting that is specific to wikitext, see Module:UtilsMarkup.

This module re-implements some of the functions in the mw.text library. Use these functions whenever possible — mw.text is an order of magnitude slower because it uses mw.ustring.

Like Module:UtilsTable, some functions have both procedural and functional variants.

This module exports the following functions.

endsWith

endsWith_endsWith

endsWith(str, pattern)

Returns

  • true if str ends with pattern, else false.

Examples

#InputOutputResult
1
endsWith("Fooloo Limpah", "Limpah")
true
2
endsWith("Fooloo Limpah", "limpah")
false
3
endsWith("Fooloo Limpah", "")
true
4
endsWith("Wood (Character)", ")", true)
true

_endsWith(str)

Returns

  • true if str ends with pattern, else false.

Examples

#InputOutputResult
5
_endsWith("Limpah")("Fooloo Limpah")
true
6
_endsWith("limpah")("Fooloo Limpah")
false
7
_endsWith("")("Fooloo Limpah")
true
8
_endsWith(")")("Wood (Character)")
true

endsWithRegex

endsWithRegex_endsWithRegex

endsWithRegex(str, pattern)

Returns

  • true if str ends with regular expression pattern, else false.

Examples

#InputOutputResult
9
endsWithRegex("Wood (Character)", "%([^)]*%)")
true
10
endsWithRegex("Wood", "%([^)]*%)")
false

_endsWithRegex(str)

Returns

  • true if str ends with regular expression pattern, else false.

Examples

#InputOutputResult
11
_endsWithRegex("%([^)]*%)")("Wood (Character)")
true
12
_endsWithRegex("%([^)]*%)")("Wood")
false

interpolate

interpolate(formatStr, args)

Approximation of string interpolation

Parameters

Returns

  • The formatted string.

Examples

#InputOutputStatus
13
interpolate(
  "${wiki} is a ${franchise} encyclopedia that anyone can edit.",
  {
    wiki = "Zelda Wiki",
    franchise = "''Zelda''",
  }
)
"Zelda Wiki is a ''Zelda'' encyclopedia that anyone can edit."

isEmpty

isEmpty(str)

Returns

  • true if and only if the value is nil or ""

Examples

#InputOutputResult
14
isEmpty(nil)
true
15
isEmpty("")
true
16
isEmpty(" ")
false

isBlank

isBlank(str)

Returns

  • true if and only if str is nil, blank, or whitespace.

Examples

#InputOutputResult
17
isBlank("  ")
true
18
isBlank("\n\n\n")
true
19
isBlank(nil)
true
20
isBlank("foo")
false

kebabCase

kebabCase(str)

Returns

  • The string converted to kebab-case

Examples

#InputOutputStatus
21
kebabCase("This is a string")
"this-is-a-string"

nilIfEmpty

nilIfEmpty(str)

Returns

  • nil if value is nil or empty string, otherwise returns the given value.

Examples

#InputOutputStatus
22
nilIfEmpty("")
nil
23
nilIfEmpty(nil)
nil
24
nilIfEmpty(" ")
" "

notBlank

notBlank(str)

Returns

  • true if and only if str does not contain only whitespace.

Examples

#InputOutputResult
25
notBlank("  ")
false
26
notBlank("\n\n\n")
false
27
notBlank(nil)
false
28
notBlank("foo")
true

notEmpty

notEmpty(str)

Returns

  • true if and only if str is neither nil nor an empty string.

Examples

#InputOutputResult
29
notEmpty(" ")
true
30
notEmpty("")
false
31
notEmpty(nil)
false

split

split_split

split(str, [pattern], [plain])

A performant alternative to mw.text.split.

Parameters

Returns

  • A table of the split strings.

Examples

#InputOutputResult
32
split(" foo,    bar,baz ")
{" foo", "bar", "baz "}
33
split("foo bar baz", " ")
{"foo", "bar", "baz"}
Support for Unicode strings
34
split("アイウエオ", "")
{"ア", "イ", "ウ", "エ", "オ"}
35
split("グタンバチの祠, インイサの祠")
{"グタンバチの祠", "インイサの祠"}

_split(str)

A performant alternative to mw.text.split.

Parameters

Returns

  • A table of the split strings.

Examples

#InputOutputResult
36
_split()(" foo,    bar,baz ")
{" foo", "bar", "baz "}
37
_split(" ")("foo bar baz")
{"foo", "bar", "baz"}
Support for Unicode strings
38
_split("")("アイウエオ")
{"ア", "イ", "ウ", "エ", "オ"}
39
_split()("グタンバチの祠, インイサの祠")
{"グタンバチの祠", "インイサの祠"}

startsWith

startsWith_startsWith

startsWith(str, pattern)

Returns

  • true if str starts with pattern, else false.

Examples

#InputOutputResult
40
startsWith("Fooloo Limpah", "Foo")
true
41
startsWith("Fooloo Limpah", "foo")
false
42
startsWith("Fooloo Limpah", "")
true
43
startsWith("[[foo]]", "[[")
true

_startsWith(str)

Returns

  • true if str starts with pattern, else false.

Examples

#InputOutputResult
44
_startsWith("Foo")("Fooloo Limpah")
true
45
_startsWith("foo")("Fooloo Limpah")
false
46
_startsWith("")("Fooloo Limpah")
true
47
_startsWith("[[")("[[foo]]")
true

startsWithRegex

startsWithRegex_startsWithRegex

startsWithRegex(str, pattern)

Returns

  • true if str starts with regular expression pattern, else false.

Examples

#InputOutputResult
48
startsWithRegex("foo", "[af]")
true
49
startsWithRegex("aoo", "[af]")
true
50
startsWithRegex("boo", "[af]")
false

_startsWithRegex(str)

Returns

  • true if str starts with regular expression pattern, else false.

Examples

#InputOutputResult
51
_startsWithRegex("[af]")("foo")
true
52
_startsWithRegex("[af]")("aoo")
true
53
_startsWithRegex("[af]")("boo")
false

stripTrailingParentheses

stripTrailingParentheses(str)

Returns

  • The string minus any text in trailing parentheses.

Examples

#InputOutputStatus
54
stripTrailingParentheses("Link's Awakening (Nintendo Switch)")
"Link's Awakening"
55
stripTrailingParentheses("foo (bar) baz")
"foo (bar) baz"

sub

sub_sub

sub(str, startIndex, [endIndex])

Equivalent to string.sub.

Parameters

Returns

  • Function returning a substring of str from startIndex to endIndex (inclusive).

Examples

#InputOutputStatus
56
sub("Fooloo Limpah", 8)
"Limpah"
57
sub("Fooloo Limpah", 1, 6)
"Fooloo"
58
sub("Fooloo Limpah", 20)
""
59
sub("Fooloo Limpah", -20)
"Fooloo Limpah"
60
sub("Fooloo Limpah", 8, 20)
"Limpah"

_sub(str)

Equivalent to string.sub.

Parameters

Returns

  • Function returning a substring of str from startIndex to endIndex (inclusive).

Examples

#InputOutputStatus
61
_sub(8)("Fooloo Limpah")
"Limpah"
62
_sub(1, 6)("Fooloo Limpah")
"Fooloo"
63
_sub(20)("Fooloo Limpah")
""
64
_sub(-20)("Fooloo Limpah")
"Fooloo Limpah"
65
_sub(8, 20)("Fooloo Limpah")
"Limpah"

trim

trim_trim

trim(str, [pattern])

A performant alternative to mw.text.trim.

Parameters

Returns

  • The trimmed string.

Examples

#InputOutputStatus
66
trim("  foo")
"foo"
67
trim(":Category:Link", ":")
"Category:Link"
Unicode support
68
trim(" グタンバチの祠 ")
"グタンバチの祠"

_trim(str)

A performant alternative to mw.text.trim.

Parameters

Returns

  • The trimmed string.

Examples

#InputOutputStatus
69
_trim()("  foo")
"foo"
70
_trim(":")(":Category:Link")
"Category:Link"
Unicode support
71
_trim()(" グタンバチの祠 ")
"グタンバチの祠"