Skip to content

Fluid Common API

Paul Manias edited this page May 3, 2024 · 1 revision

The Common API provides a number of utility functions to enhance existing interfaces such as table and string. It also adds the file interface.

The API can be loaded with the line:

require 'common'

String Enhancements

string.escXML()

str:escXML()

Escape str for an XML attribute value or content.

string.cap()

str:cap()

Capitalise a string.

string.trim()

str:trim()

Trims whitespace from the left and right sides of a string.

string.rtrim()

str:rtrim()

Trims whitespace from the right side of a string.

string.split()

str:split([Pattern])

Split a string using Pattern to identify each valid word. If Pattern is not specified, it defaults to %s.

string.startsWith()

str:startsWith(Cmp)

Returns true if the string starts with Cmp.

string.endsWith()

str:endsWith(Cmp)

Returns true if the string ends with Cmp.

Table Enhancements

table.sortByKeys()

table.sortByKeys(Table)

Sort all key values in Table for processing as paired k,v items. Intended for use in a for loop as follows:

for k,v in table.sortByKeys(the_table) do
   ...
end

table.toXML()

String = table.toXML(Table)

Parse the key-values of a table and produce an XML string.

Math Enhancements

math.round()

math.round(Num, [DecimalPlaces])

Round floating point Num to the nearest integer. If DecimalPlaces is specified then the function will round to the place indicated. For example math.round(8.2468,2) rounds to 8.25.

File Interface

file.readAll()

content = file.readAll(Path)

Reads all content from Path and returns it as a string. An exception will be thrown if failure occurs for any reason.

file.isFolder()

exists = file.isFolder(Path)

Returns true if the Path string explicitly references a folder. No test is performed to confirm whether or not the path exists.

file.splitPath()

path, filename = file.splitPath(Path)

Splits the folder and filename out from a Path string, returning them as distinct values. For instance parasol:a/b/file.txt results in parasol:a/b/ and file.txt.

file.sanitisePath()

path = file.sanitisePath(Path)

Removes repeated folder separators from a string. For instance volume:a/b///c\\f.txt becomes volume:a/b/c\f.txt.