String Recipes
Source: string.spq (includes integer.spq)
sk_slice
Returns a slice of the string passed in, even if indexes are out of range.
Type: function
| Argument | Description |
|---|---|
s | The string to slice. |
start | Starting index, zero-based, inclusive. |
end | Ending index, exclusive. |
sk_slice('howdy', 0, 3)
-- => 'how'
Implementation:
fn sk_slice(s, start, end): (
s[sk_clamp(start, -len(s), len(s)):sk_clamp(end, -len(s), len(s))]
)
sk_capitalize
Upper the first character of the string, lower the remaining string.
Type: function
| Argument | Description |
|---|---|
s | The string to capitalize. |
sk_capitalize('hoWDy')
-- => 'Howdy'
Implementation:
fn sk_capitalize(s): (
f"{upper(sk_slice(s, 0, 1))}{lower(sk_slice(s,1,len(s)))}"
)
sk_titleize
Splits string by space and capitalizes each word.
Type: function
| Argument | Description |
|---|---|
s | The string to titleize |
sk_titleize('once uPON A TIME')
-- => 'Once Upon A Time'
Implementation:
fn sk_titleize(s): (
[unnest split(s, " ") | values sk_capitalize(this)] | join(this, " ")
)
sk_pad_left
Inserts pad_char to the left of the string until it reaches target_length.
Type: function
| Argument | Description |
|---|---|
s | The string to pad |
pad_char | The character to pad with |
target_length | The target length of the string |
values sk_pad_left('abc', ' ', 5)
-- => ' abc'
Implementation:
fn sk_pad_left(s, pad_char, target_length): (
len(s) < target_length ? sk_pad_left(f'{pad_char}{s}', pad_char, target_length) : s
)
sk_pad_right
Inserts pad_char to the right of the string until it reaches target_length.
Type: function
| Argument | Description |
|---|---|
s | The string to pad |
pad_char | The character to pad with |
target_length | The target length of the string |
values sk_pad_right('abc', ' ', 5)
-- => 'abc '
Implementation:
fn sk_pad_right(s, pad_char, target_length): (
len(s) < target_length ? sk_pad_right(f'{s}{pad_char}', pad_char, target_length) : s
)
sk_left
Returns the first n characters of a string.
Type: function
| Argument | Description |
|---|---|
s | The string. |
n | Number of characters from the left. |
sk_left('hello', 3)
-- => 'hel'
Implementation:
fn sk_left(s, n): (sk_slice(s, 0, sk_clamp(n, 0, len(s))))
sk_right
Returns the last n characters of a string.
Type: function
| Argument | Description |
|---|---|
s | The string. |
n | Number of characters from the right. |
sk_right('hello', 3)
-- => 'llo'
Implementation:
fn sk_right(s, n): (sk_slice(s, len(s) - sk_clamp(n, 0, len(s)), len(s)))
sk_mid
Returns n characters from a string starting at a given position.
Type: function
| Argument | Description |
|---|---|
s | The string. |
start | Starting index, zero-based. |
n | Number of characters to return. |
sk_mid('hello world', 6, 5)
-- => 'world'
Implementation:
fn sk_mid(s, start, n): (sk_slice(s, sk_clamp(start, 0, len(s)), sk_clamp(start, 0, len(s)) + sk_clamp(n, 0, len(s))))
sk_urldecode
URL decoder for SuperDB. Splits on %, decodes each hex-encoded segment, and joins back together.
Type: operator
| Argument | Description |
|---|---|
url | The URL-encoded string to decode |
super -I string.spq -s -c 'values sk_urldecode("%2Ftavern%20test")' -
Implementation:
op sk_decode_seg s: (
len(s) == 0
? s
: (is_error(hex(s[0:2]))
? s
: f'{hex(s[0:2])::string}{s[2:]}')
)
op sk_urldecode url: (
split(url, "%%")
| unnest this
| sk_decode_seg this
| collect(this)
| join(this, "")
)