Wi Standard Method Library Reference - cyxigo/wi GitHub Wiki
Introduction
This page mirrors Standard Library Reference, but for Standard Method Library.
| Type | Methods |
|---|---|
string |
bytes, sub, upper, lower, trim, has, indexof, startswith, endswith, isdigit, isalpha, isalnum, compare, replace, split, reverse, repeat, each, select, where, find |
array |
copy, clear, capacity, count, reverse, reversed, add, has, indexof, remove, removeat, insertat, pop, concat, slice, join, each, select, where, sort, find |
map |
copy, clear, capacity, count, keys, values, has, getordefault, remove, each, select, where, find |
[!NOTE] Modifying an array or a map from inside a callback (
each,select,where,sort,find) is a runtime error.
[!NOTE]
has,indexof, andremovecompare items with==, so arrays, maps, and objects are matched by reference (usestd::equalsfor structural comparison).
string
Methods
bytes
Get the byte representation of a string
Parameters:
string str: String
Returns: array of byte values (0-255)
sub
Get a substring
Parameters:
string str: Stringreal start: Start index, inclusivereal end: End index, exclusive
Returns: string substring from start to end
upper
Convert a string to uppercase
Parameters:
string str: String
Returns: string uppercased str
Note: is not UTF-8 aware (will only work on ASCII, everything else – no op)
lower
Convert a string to lowercase
Parameters:
string str: String
Returns: string lowercased str
Note: is not UTF-8 aware (will only work on ASCII, everything else – no op)
trim
Remove leading and trailing whitespace from a string
Parameters:
string str: String
Returns: string str with leading and trailing whitespace removed
has
Check if a string contains a substring
Parameters:
string str: Stringstring target: Substring to search for
Returns: bool true if str contains target, false otherwise
indexof
Get the index of the first occurrence of a substring in a string
Parameters:
string str: Stringstring target: Substring to search for
Returns: real index of the substring, or -1 if not found
startswith
Check if a string starts with a prefix
Parameters:
string str: Stringstring prefix: Prefix to check for
Returns: bool true if str starts with prefix, false otherwise
endswith
Check if a string ends with a suffix
Parameters:
string str: Stringstring suffix: Suffix to check for
Returns: bool true if str ends with suffix, false otherwise
isdigit
Check if every character in a string is a digit (0-9).
Returns false for an empty string
Parameters:
string str: String
Returns: bool true if every character in str is a digit, false otherwise
Note: is not UTF-8 aware
isalpha
Check if every character in a string is an alphabetic character (a-z, A-Z).
Returns false for an empty string
Parameters:
string str: String
Returns: bool true if every character in str is an alphabetic character, false otherwise
Note: is not UTF-8 aware
isalnum
Check if every character in a string is alphanumeric (a digit or an alphabetic character).
Returns false for an empty string
Parameters:
string str: String
Returns: bool true if every character in str is alphanumeric, false otherwise
Note: is not UTF-8 aware
compare
Check if a string is lexicographically less than another
Parameters:
string a: String Astring b: String B
Returns: bool true if a sorts before b, false otherwise
replace
Replace all occurrences of a substring with another string
Parameters:
string str: Stringstring old: Substring to replacestring new: Replacement string
Returns: string new string with every occurrence of old replaced by new
split
Split a string by a separator
Parameters:
string str: Stringstring sep: Separator
Returns: array of string parts
reverse
Reverse a string
Parameters:
string str: String
Returns: string reversed str
repeat
Repeat a string a number of times
Parameters:
string str: Stringreal times: Number of times to repeat
Returns: string new repeated string
each
Call a function for each character in a string
Parameters:
string str: Stringfunction fn: Function called with(char)for each character
Returns: string the same string
select
Create a new string by transforming each character with a function
Parameters:
string str: Stringfunction fn: Function called with(char)for each character, its return value is concatenated to the result string, return value must be a string
Returns: string new string of transformed characters
where
Create a new string containing only the characters for which a function returns a truthy value
Parameters:
string str: Stringfunction fn: Function called with(char)for each character
Returns: string new string for which fn returned truthy
find
Get the first character for which a function returns a truthy value
Parameters:
string str: Stringfunction fn: Function called with(char)for each character
Returns: string the first matching character, or null if there is none
array
Methods
copy
Create a shallow copy of an array
Parameters:
array arr: Array
Returns: array shallow copy of arr
clear
Remove all the items from an array
Parameters:
array arr: Array
Returns: array the same array
capacity
Get the current item capacity of an array
Parameters:
array arr: Array
Returns: real current item capacity of arr
count
Get the number of the items in an array
Parameters:
array arr: Array
Returns: real number of items in arr
reverse
Reverse an array
Parameters:
array arr: Array
Returns: array the same array, reversed
reversed
Get a new array with the items in reverse order
Parameters:
array arr: Array
Returns: array new reversed array
add
Add a value to the end of an array
Parameters:
array arr: Arrayany value: Value to add
Returns: array the same array
has
Check if an array contains a value
Parameters:
array arr: Arrayany value: Value to search for
Returns: bool true if arr contains value, false otherwise
indexof
Get the index of the first occurrence of a value in an array
Parameters:
array arr: Arrayany value: Value to search for
Returns: real index of the value, or -1 if not found
remove
Remove the first occurrence of a value from an array
Parameters:
array arr: Arrayany value: Value to remove
Returns: bool true if a value was found and removed, false otherwise
removeat
Remove the item at the given index
Parameters:
array arr: Arrayreal index: Index to remove
Returns: any the removed value
insertat
Insert a value into an array at the given index
Parameters:
array arr: Arrayreal index: Index to insert at (can equal the array's current length, to insert at the end)any value: Value to insert
Returns: array the same array
pop
Remove the last item of an array
Parameters:
array arr: Array
Returns: any the removed value
concat
Concatenate multiple arrays into a new array
Parameters:
array ...arrays: Arrays to concatenate
Returns: array new concatenated array
slice
Get a slice of an array
Parameters:
array arr: Arrayreal start: Start index, inclusivereal end: End index, exclusive
Returns: array new array containing the items from start to end
join
Join array items (converted to strings implicitly) into a single string
Parameters:
array arr: Arraystring sep: Separator
Returns: string joined string
each
Call a function for each item in an array
Parameters:
array arr: Arrayfunction fn: Function called with(item)for each item
Returns: array the same array
select
Create a new array by transforming each item with a function
Parameters:
array arr: Arrayfunction fn: Function called with(item)for each item, its return value is added to the result array
Returns: array new array of transformed items
where
Create a new array containing only the items for which a function returns a truthy value
Parameters:
array arr: Arrayfunction fn: Function called with(item)for each item
Returns: array new array of items for which fn returned truthy
sort
Sort an array in place using a comparator function (elements that compare equal may end up in any order)
Parameters:
array arr: Arrayfunction fn: Function called with(a, b)for each comparison, should return truthy ifashould be ordered beforeb
Returns: array the same array, sorted
find
Get the first item for which a function returns a truthy value
Parameters:
array arr: Arrayfunction fn: Function called with(item)for each item
Returns: any the first matching item, or null if there is none
map
Methods
copy
Create a shallow copy of a map
Parameters:
map m: Map
Returns: map shallow copy of m
clear
Remove all the entries from a map
Parameters:
map m: Map
Returns: map the same map
capacity
Get the current entry capacity of a map
Parameters:
map m: Map
Returns: real current entry capacity of m
count
Get the number of live entries in a map
Parameters:
map m: Map
Returns: real number of live entries in m
keys
Get all the keys in a map
Parameters:
map m: Map
Returns: array of keys
values
Get all the values in a map
Parameters:
map m: Map
Returns: array of values
has
Check if a map has a key
Parameters:
map m: Mapany key: Key to check for
Returns: bool true if m has key, false otherwise
getordefault
Get a value from a map, or a default value if the key is not present
Parameters:
map m: Mapany key: Key to look upany default: Value to return if the key is not present
Returns: any value for key, or default if the key is not present
remove
Remove a key from a map
Parameters:
map m: Mapany key: Key to remove
Returns: bool true if key existed and was removed, false otherwise
each
Call a function for each entry in a map
Parameters:
map m: Mapfunction fn: Function called with(key, value)for each entry
Returns: map the same map
select
Create a new map by transforming each key and value with functions
Parameters:
map m: Mapfunction key_fn: Function called with(key)for each entry, its return value becomes the new keyfunction value_fn: Function called with(value)for each entry, its return value becomes the new value
Returns: map new map of transformed entries
where
Create a new map containing only the entries for which a function returns a truthy value
Parameters:
map m: Mapfunction fn: Function called with(key, value)for each entry
Returns: map new map of entries for which fn returned truthy
find
Get the key of the first entry for which a function returns a truthy value
Parameters:
map m: Mapfunction fn: Function called with(key, value)for each entry
Returns: any the key of the first matching entry, or null if there is none