Strings¶
Useful functions to manipulate strings, based on similar implementations in other standard libraries.
Functions¶
camelCase¶
function dash.camelCase(str) --> string
str
to camel-case.
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.camelCase('Pepperoni Pizza') --> 'pepperoniPizza'
dash.camelCase('--pepperoni-pizza--') --> 'pepperoniPizza'
dash.camelCase('__PEPPERONI_PIZZA') --> 'pepperoniPizza'
capitalize¶
function dash.capitalize(str) --> string
str
.
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.capitalize("hello mould") --> "Hello mould"
charToHex¶
function dash.charToHex(char, format, useBytes) --> string
Type
char, string?, boolean? -> string
Parameters
char -
char
- a charformat -
string?
- a string (optional) - (optional) a string passed to dash.format which formats the hex value of each of the character's code points.useBytes -
boolean?
- a boolean (optional) - (default = false) whether to use the character's bytes, rather than UTF-8 code points.
Returns
string
- a string
Examples
dash.charToHex("<") --> "3C"
dash.charToHex("<", "&#{};") --> "C;"
dash.charToHex("π") --> "1F60F"
dash.charToHex("π", "0x{}") --> "0x1F60F"
dash.charToHex("π€·πΌββοΈ", "&#x{};") --> "🤷🏼‍♀️"
dash.charToHex("π€·πΌββοΈ", "%{}", true) --> "%F0%9F%A4%B7%F0%9F%8F%BC%E2%80%8D%E2%99%80%EF%B8%8F"
debug¶
function dash.debug(format, ...) --> string
Type
string, ... -> string
Parameters
format -
string
- a string - the format match string... -
...any
- any values
Returns
string
- a string
Examples
-- During development: dash.setDebug() -- At any point in the code: dash.debug("Hello {}", game.Players.LocalPlayer) -->> Hello builderman (for example)
Usage
- A common pattern would be to
dash.setDebug()
to alias toprint
during local development, and send debug messages to an HTTP server on a production build to allow remote debugging.
See
decodeHtml¶
function dash.decodeHtml(str) --> string
str
to their corresponding characters.
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.decodeHtml("<b>"Smashed"</b> 'Avocado' 😏") --> [[<b>"Smashed"</b> 'Avocado' π]]
decodeUrl¶
function dash.decodeUrl(str) --> string
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.decodeUrl("https://Egg+Fried%20Rice!?") --> "https://Egg+Fried Rice!?"
Usage
- This method is designed to act like
decodeURI
in JavaScript.
decodeUrlComponent¶
function dash.decodeUrlComponent(str) --> string
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Throws
- MalformedInput if str contains characters encoded incorrectly.
Examples
dash.decodeUrlComponent("https%3A%2F%2FEgg%2BFried%20Rice!%3F") --> "https://Egg+Fried Rice!?"
Usage
- This method is designed to act like
decodeURIComponent
in JavaScript.
encodeHtml¶
function dash.encodeHtml(str) --> string
&<>"'
in str
to their corresponding HTML entities.
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.encodeHtml([[Pease < Bacon > "Fish" & 'Chips']]) --> "Peas < Bacon > "Fish" & 'Chips'"
encodeQueryString¶
function dash.encodeQueryString(query) --> string
Type
<K,V>(Iterable<K,V> -> string)
Generics
K -
any
- the primary key type (extends any value)V -
any
- the primary value type (extends any value)
Parameters
query -
Iterable<K, V>
- an Iterable (of the primary key type and the primary value type)
Returns
string
- a string
Examples
dash.encodeQueryString({ time = 11, biscuits = "hob nobs", chocolatey = true })) --> "?biscuits=hob+nobs&time=11&chocolatey=true"
Usage
- A query string which contains duplicate keys with different values is technically valid, but this function doesn't provide a way to produce them.
encodeUrl¶
function dash.encodeUrl(str) --> string
Note that, unlike this function, HttpService.EncodeUrl
actually attempts to encode a string
for purposes as a URL component rather than an entire URL, and as such will not produce a valid
URL.
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.encodeUrl("https://example.com/Egg+Fried Rice!?π€·πΌββοΈ") --> "https://example.com/Egg+Fried%20Rice!?%F0%9F%A4%B7%F0%9F%8F%BC%E2%80%8D%E2%99%80%EF%B8%8F"
Usage
- This method is designed to act like
encodeURI
in JavaScript.
See
encodeUrlComponent¶
function dash.encodeUrlComponent(str) --> string
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.encodeUrlComponent("https://example.com/Egg+Fried Rice!?π€·πΌββοΈ") --> "https%3A%2F%2Fexample.com%2FEgg%2BFried%20Rice!%3F%F0%9F%A4%B7%F0%9F%8F%BC%E2%80%8D%E2%99%80%EF%B8%8F"
Usage
-
This method is designed to act like
encodeURIComponent
in JavaScript. -
This is very similar to
HttpService.EncodeUrl
, but is included for parity and conforms closer to the standard (e.g. EncodeUrl unnecessarily encodes!
).
endsWith¶
function dash.endsWith(str, suffix) --> bool
str
ends with the string suffix
.
Type
string, string -> bool
Parameters
str -
string
- a stringsuffix -
string
- a string
Returns
bool
- a boolean
Examples
dash.endsWith("Fun Roblox Games", "Games") --> true
dash.endsWith("Bad Roblox Memes", "Games") --> false
format¶
function dash.format(format, ...) --> string
{...}
substituted with readable representations
of the subsequent arguments.
This function is a simpler & more powerful version of dash.format, inspired by format!
in Rust.
{}
formats and prints the next argument using:format()
if available, or a suitable default representation depending on its type.{2}
formats and prints the 2nd argument.{#2}
prints the length of the 2nd argument.
Display parameters can be combined after a :
in the curly braces. Any format parameters used
in dash.format can be used here, along with these extras:
{:?}
formats any value using dash.serializeDeep.{:#?}
formats any value using dash.pretty.{:b}
formats a number in its binary representation.
Type
string, ... -> string
Parameters
format -
string
- a string... -
...any
- any values
Returns
string
- a string
Examples
local props = {"teeth", "claws", "whiskers", "tail"} dash.format("{:?} is in {:#?}", "whiskers", props) -> '"whiskers" is in {"teeth", "claws", "whiskers", "tail"}'
dash.format("{} in binary is {1:b}", 125) -> "125 in binary is 110100"
dash.format("The time is {:02}:{:02}", 2, 4) -> "The time is 02:04"
dash.format("The color blue is #{:06X}", 255) -> "The color blue is #0000FF"
Usage
-
Escape
{
with{{
and}
similarly with}}
. -
See https://developer.roblox.com/articles/Format-String for complete list of formating options and further use cases.
See
formatValue¶
function dash.formatValue(value, displayString) --> string
Type
any, DisplayString -> string
Parameters
value -
any
- any valuedisplayString -
DisplayString
- a DisplayString
Returns
string
- a string
Examples
dash.formatValue(255, ":06X") --> 0000FF
See
- dash.format - for a full description of valid display strings.
hexToChar¶
function dash.hexToChar(hex) --> char
Type
str -> char
Parameters
hex -
str
- a str
Returns
char
- a char
Throws
- MalformedInput if char is not a valid encoding.
Examples
dash.hexToChar("1F60F") --> "π"
dash.hexToChar("%1F60F") --> "π"
dash.hexToChar("#1F60F") --> "π"
dash.hexToChar("0x1F60F") --> "π"
kebabCase¶
function dash.kebabCase(str) --> string
str
to kebab-case, making all letters lowercase.
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.kebabCase('strongStilton') --> 'strong-stilton'
dash.kebabCase(' Strong Stilton ') --> 'strong-stilton'
dash.kebabCase('__STRONG_STILTON__') --> 'strong-stilton'
Usage
- Chain with
:upper()
if you need an upper kebab-case string.
leftPad¶
function dash.leftPad(str, length, prefix) --> string
length
from str
by repeating characters from prefix
at the start of the string.
Type
string, number, string -> string
Parameters
str -
string
- a stringlength -
number
- a numberprefix -
string
- a string - (default =" "
)
Returns
string
- a string
Examples
dash.leftPad("toast", 6) --> " toast"
dash.leftPad("2", 2, "0") --> "02"
dash.leftPad("toast", 10, ":)") --> ":):):toast"
pretty¶
function dash.pretty(value, serializeOptions) --> string
80
characters.
Optionally a table of SerializeOptions can be passed which will pass to the underlying dash.serialize function so you can customise what is displayed.
Type
<T>(T, SerializeOptions<T>? -> string)
Generics
T -
any
- the primary type (extends any value)
Parameters
value -
T
- the primary typeserializeOptions -
SerializeOptions<T>?
- a SerializeOptions (of the primary type) (optional)
Returns
string
- a string
Examples
local fox = { name = "Mr. Fox", color = "red" } print(dash.pretty(fox)) -->> {color = "red", name = "Mr. Fox"}
local fox = { name = "Mr. Fox", color = "red" } print(dash.pretty(fox, {omitKeys = {"name"}})) -->> {color = "red"}
See
- dash.serializeDeep for a compact alternative.
rightPad¶
function dash.rightPad(str, length, suffix) --> string
length
from str
by repeating characters from suffix
at the end of the string.
Type
string, number, string -> string
Parameters
str -
string
- a stringlength -
number
- a numbersuffix -
string
- a string - (default =" "
)
Returns
string
- a string
Examples
dash.rightPad("toast", 6) --> "toast "
dash.rightPad("2", 2, "!") --> "2!"
dash.rightPad("toast", 10, ":)") --> "toast:):):"
setDebug¶
function dash.setDebug(fn) --> void
Type
<A>(...A -> ())
Generics
A -
any
- the primary arguments (extends any value)
Parameters
fn -
...A
- the primary arguments - (default =
Returns
void
- nothing
Examples
local postMessage = dash.async(function(message) HttpService.PostAsync("https://example.com/log", message) end -- During production: dash.setDebug(postMessage) -- At any point in the code: dash.debug("Hello is printed") -- "Hello is printed" is posted to the server
Usage
- Calling
dash.setDebug()
will simply print all calls to dash.debug with formatted arguments.
See
snakeCase¶
function dash.snakeCase(str) --> string
str
to snake-case, making all letters uppercase.
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.snakeCase('sweetChickenCurry') --> 'SWEET_CHICKEN_CURRY'
dash.snakeCase(' Sweet Chicken Curry ') --> 'SWEET_CHICKEN__CURRY'
dash.snakeCase('--sweet-chicken--curry--') --> 'SWEET_CHICKEN__CURRY'
Usage
- Chain with
:lower()
if you need a lower snake-case string.
splitOn¶
function dash.splitOn(str, pattern) --> string[], string[]
str
into parts based on a pattern delimiter and returns a table of the parts, followed
by a table of the matched delimiters.
Type
string, pattern -> string[], string[]
Parameters
str -
string
- a stringpattern -
pattern
- a pattern
Returns
string[], string[]
- a tuple (an array (of strings) and an array (of strings))
Examples
dash.splitOn("rice") --> {"r", "i", "c", "e"}, {"", "", "", ""}
dash.splitOn("one.two::flour", "[.:]") --> {"one", "two", "", "flour"}, {".", ":", ":"}
Usage
-
This method is useful only when you need a pattern as a delimiter.
-
Use the Roblox native
string.split
if you are splitting on a simple string.
startsWith¶
function dash.startsWith(str, prefix) --> bool
str
starts with the string start
.
Type
string, string -> bool
Parameters
str -
string
- a stringprefix -
string
- a string
Returns
bool
- a boolean
Examples
dash.startsWith("Fun Roblox Games", "Fun") --> true
dash.startsWith("Chess", "Fun") --> false
titleCase¶
function dash.titleCase(str) --> string
str
to title-case, where the first letter of each word is capitalized.
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.titleCase("jello world") --> "Jello World"
dash.titleCase("yellow-jello with_sprinkles") --> "Yellow-jello With_sprinkles"
dash.titleCase("yellow jello's donβt mellow") --> "Yellow Jello's Dontβt Mellow"
Usage
- Dashes, underscores and apostraphes don't break words.
trim¶
function dash.trim(str) --> string
str
.
Type
string -> string
Parameters
str -
string
- a string
Returns
string
- a string
Examples
dash.trim(" roast veg ") --> "roast veg"