The core library contains all the built-in methods and global functions.

Global Functions

:import(source) #

Import Axiom source code from another location. The code is executed in the global context. Subsequent calls with the same source do nothing.

Parameters

Notes

If source is a URL, the source code is downloaded and cached in a folder named ".axiom".

If source starts with '$', everything from the '$' to the first '/' is interpreted as an environment variable.

If source is a file path, it is automatically converted to an absolute path for de-duplication.

Example

import('https://aaronliu.cc/axiom/example.axm') #import URL import('$PWD/test.axm') #import file path with environment variable import('test.axm') #import file path :uuid() #

Generate a Universally Unique Identifier (UUID). This is just a random number that is so large that it is probabilistically guaranteed to be unique.

Return

Notes

Uses an algorithm to increase randomness at the cost of time.

:random() #

Generate a random number in the range [0,1).

Return

:randint(min,max) #

Generate a random number in the range [min,max].

Parameters

Return

:randstr(len) #

Generate a random string with length len. Characters are chosen from the set of 52 characters "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY346789", which is the set of alphanumeric characters with ambiguous characters removed (o/0/O, 1/I/l, 2/Z, 5/S).

Parameters

Return

:time(year,month,day,hour,minute,second) #

Generate a timestamp from the specified date.

A timestamp is the number of seconds since the January 1, 1970 UTC (the Unix epoch).

If no arguments are passed, returns a timestamp corresponding to the current time.

Parameters

Return

Shared Methods

:type() #

Get the type of the variable.

Return

Notes

The possible types are 'null', 'number', 'boolean', 'string', 'array', object'.

Example

print (null).type() #null print (true).type() #boolean print (2).type() #number print ''.type() #string print [].type() #array print {}.type() #object :boolean() #

Convert variable to boolean.

Return

:number() #

Convert variable to number.

Return

:string() #

Convert variable to string.

Return

Notes

For all variable types besides string, this is equivalent to :json(). For variables of type string, this does nothing (just returns the same string).

:json() #

Convert variable to a JSON string.

Return

Notes

This is like :string(), except for strings, the string is escaped and quoted in JSON format.

Example

print 'hello\nthere'.json() #"hello\nthere" :compare(other) #

Compare this variable to another variable.

Parameters

Return

Number Methods

:abs() #

Get the absolute value of a number.

Return

:acos() #

Get the arc cosine (inverse cosine) in radians.

Return

:asin() #

Get the arc sine (inverse sine) in radians.

Return

:atan() #

Get the arc tangent (inverse tangent) in radians.

Return

:cos() #

Get the cosine of a number in radians.

Return

:sin() #

Get the sine of a number in radians.

Return

:tan() #

Get the tangent of a number in radians.

Return

:exp() #

Get the natural exponent of a number.

Return

:ln() #

Get the natural logarithm of a number.

Return

:log() #

Get the base-10 logarithm of a number.

Return

:sqrt() #

Get the square root of a number.

Return

:pow(exponent) #

Raise this number to the exponent power.

Parameters

Return

:min(other) #

Get the minimum of this number and other

Parameters

Return

:max(other) #

Get the maximum of this number and other

Parameters

Return

:floor() #

Round number down to nearest integer.

Return

Notes

Negative numbers get rounded up in absolute value.

Example

print (1234.5).floor() #1234 print (-1234.5).floor() #-1235 :ceil() #

Round number up to nearest integer.

Return

Notes

Negative numbers get rounded down in absolute value.

Example

print (1234.5).ceil() #1235 print (-1234.5).ceil() #-1234 :int() #

Get integer part of a number.

Return

Notes

This is equivalent to rounding towards 0 (rather than towards negative/positive like :floor()/:ceil())

Example

print (1234.5).int() #1234 print (-1234.5).int() #-1234 :frac() #

Get fractional part of a number

Return

Example

print (1234.5).frac() #0.5 print (-1234.5).frac() #-0.5 :is_nan() #

Determine whether or not this number is invalid (not a number).

Return

Example

print (-1).sqrt().is_nan() #true :is_inf() #

Determine whether or not this number is infinite.

Return

Example

print (1/0).is_inf() #true :sign() #

Get the sign of a number.

Return

:comma() #

Insert commas into a number.

Return

Notes

Commas are inserted every 3 decimal places.

Example

print (1234).comma() #1,234 :bytes() #

Format this number as a size in bytes.

Return

Notes

Units go from B (bytes) to PB (petabytes).

Example

print (1234).bytes() #1.2 KB :date(format) #

Format this number (timestamp) as a calendar date.

Parameters

Return

Notes

The format string is a string with any combination of the following flags:

Flag Description Example ---- ----------- ------- %a - Abbreviated weekday name [Thu] %A - Full weekday name [Thursday] %b - Abbreviated month name [Aug] %B - Full month name [August] %d - Day of the month, zero-padded (01-31) [23] %H - Hour in 24h format (00-23) [14] %I - Hour in 12h format (01-12) [02] %m - Month as a decimal number (01-12) [08] %M - Minute (00-59) [55] %p - AM or PM designation [PM] %S - Second (00-61) [02] %w - Weekday number with Sunday as 0 (0-6) [4] %x - Date representation [2016/06/21] %X - Time representation [14:55:02] %y - Year, last two digits (00-99) [01] %Y - Year [2001]

Example

t = time(2000,1,2,3,45,56) print t.date() #2000/01/02 03:45:56 Sun print t.date('%B %d, %Y %I:%M %p') #January 02, 2000 03:45 AM

String Methods

:len() #

Get length of string.

Return

:find(str) #

Find index of str in this string.

Parameters

Return

:rfind(str) #

Find index of str in this string, searching from right to left.

Parameters

Return

:split(delims) #

Split string into an array of strings.

Parameters

Return

Notes

If delims is not specified, the behavior is to split the string along newlines. This will handle '\r\n' as a single character.

:slice(start,end) #

Slice this string into a smaller string.

Parameters

Return

Notes

If end is not specified, the string is sliced from start to the end of the string.

:trim() #

Remove leading and trailing whitespace from this string.

Return

Notes

Whitespace characters are space (' '), tab (' '), carriage return ('\r'), and newline ('\n').

:lower() #

Convert this string to lower case.

Return

Notes

Only English letters (A-Z) are supported.

:upper() #

Convert this string to upper case.

Return

Notes

Only English letters (a-z) are supported.

:match(pattern) #

Check if this string matches wildcard pattern.

Parameters

Return

Notes

A wildcard pattern is a string where '*' is used to match zero or more characters, and '?' is used to match any single character. Wildcard patterns are case-insensitive.

:extract(left,right) #

Extract the substring from this string that falls between two wildcard patterns.

Parameters

Return

Notes

A wildcard pattern is a string where '*' is used to match zero or more characters, and '?' is used to match any single character. Wildcard patterns are case-insensitive.

Example

print '<a href="http://example.com">test</a>'.extract('<a*href="','"') #http://example.com :skim(pattern) #

Perform mass extraction on string.

Parameters

Return

Notes

Multiple tokens (multiple '$' in pattern) are supported.

Example

print ` <ul> <li>a</li> <li>b</li> <li>c</li> </ul> `.skim('<li>$</li>') #output: #[ "a", "b", "c" ] :remove(c) #

Remove all instances of a single character from this string.

Parameters

Return

:replace(old,new) #

Replace all instances of string old with string new within this string.

Parameters

Return

:hex() #

Encode the bytes of this string as hexadecimal.

Return

Notes

The hexadecimal output is lower case.

:unhex() #

Decode this string as hexadecimal into bytes.

Return

Notes

The hexadecimals input can be either upper or lower case.

:base64() #

Encode the bytes of this string as base64.

Return

:unbase64() #

Decode this string as unbase64 into bytes.

Return

:escape() #

Encode the bytes of this string as an Axiom string literal.

Return

Notes

Axiom string literals are compatible with C-style string literals (C, C++, Python, Java, Javascript, etc.). See the Language Manual for details.

:unescape() #

Decode this string as an Axiom string literal into bytes.

Return

Notes

Axiom string literals are compatible with C-style string literals (C, C++, Python, Java, Javascript, etc.). See the Language Manual for details.

:url_encode(component) #

Encode the bytes of this string as a URL.

Parameters

Return

Notes

If component is false or not specified, this is equivalent to encodeURI() in Javascript. If component is true, this is equivalent to encodeURIComponent() in Javascript.

:url_decode() #

Decode this string as a URL into bytes.

Return

Notes

Equivalent to decodeURI() in Javascript.

:sha1() #

Get the SHA1 hash of this string.

Return

:md5() #

Get the MD5 hash of this string.

Return

:sha256() #

Get the SHA256 hash of this string.

Return

:hmac_sha1(key) #

Sign this string using the HMAC algorithm with hash function SHA1.

Parameters

Return

Notes

The key should be kept secret. To verify an incoming message, sign the message again and compare the signature with the incoming signature.

:hmac_md5(key) #

Sign this string using the HMAC algorithm with hash function MD5.

Parameters

Return

Notes

The key should be kept secret. To verify an incoming message, sign the message again and compare the signature with the incoming signature.

:hmac_sha256(key) #

Sign this string using the HMAC algorithm with hash function SHA256.

Parameters

Return

Notes

The key should be kept secret. To verify an incoming message, sign the message again and compare the signature with the incoming signature.

:cipher(key) #

Encrypt or decrypt a string using key. The algorithm is the same both ways.

Parameters

Return

Notes

For better security and validation, use :encrypt() and :decrypt()

:encrypt(key) #

Encrypt a string using key, with salting and validation.

Parameters

Return

:decrypt(key) #

Decrypt a string using key, with salting and validation.

Parameters

Return

:starts_with(str) #

Check if this string starts with str.

Parameters

Return

:ends_with(str) #

Check if this string ends with str.

Parameters

Return

:sort(asc) #

Sort the bytes of this string in specified order.

Parameters

Notes

The bytes are sorted in place (the string is modified).

:shuffle() #

Shuffle the bytes of this string randomly.

Notes

The bytes are shuffled in place (the string is modified).

:reverse() #

Reverse the bytes in this string.

Return

Notes

This reverses the bytes in a string, not the characters. To intepret the string as Unicode and reverse the characters, one should decode as Unicode first, like: s.decode().reverse().encode()

:decode() #

Decode this string as UTF-8 into array of code points.

Return

Notes

This is used to do character-level manipulation of strings, since strings are just bytes in Axiom. This is similar to converting a std::string to a std::wstring in C++.

This is the opposite of the array method :encode().

:parse_json() #

Parse this string as JSON.

Return

:parse_date() #

Parse this string as a date and return a timestamp.

Return

Notes

A variety of date formats are supported via fuzzy matching. Most common formats should work, including:

A variety of other formats should work as well, too many to enumerate.

Array Methods

:len() #

Get length of array.

Return

:top() #

Get the last element of array.

Return

:pop() #

Remove the last element of array.

Return

:push(item) #

Append item to array.

Parameters

:find(item) #

Find index of item in this array.

Parameters

Return

:rfind(item) #

Find index of item in this array, searching from right to left.

Parameters

Return

:sort(asc) #

Sort the elements of this array in specified order.

Parameters

Notes

The items are sorted in place (the array is modified).

:sort_by_key(key,asc) #

Sort the elements of this array by the specified key, assuming each element is an object.

Parameters

Notes

The items are sorted in place (the array is modified).

:sort_by_index(index,asc) #

Sort the elements of this array by the specified index, assuming each element is also an array.

Parameters

Notes

The items are sorted in place (the array is modified).

:sort_by(cmp) #

Sort the elements of this array using the specified comparator function.

Parameters

Notes

The items are sorted in place (the array is modified).

:join(delim) #

Combine the elements of the array into a single string.

Parameters

Return

:slice(start,end) #

Slice this array into a smaller array.

Parameters

Return

Notes

If end is not specified, the array is sliced from start to the end of the array.

:remove(index) #

Remove element at index.

Parameters

Notes

Elements after index are downshifted to fill hole.

:column(col) #

Get column of elements, assuming this is a two-dimensional array.

Parameters

Return

:transpose() #

Flip this array along the diagonal, assuming this is a two-dimensional array.

Return

:unique() #

Get the set of unique elements in this array.

Return

:shuffle() #

Shuffle the elements of this array randomly.

Notes

The elements are shuffled in place (the array is modified).

:reverse() #

Reverse the elements of this array.

Return

:encode() #

Encode this array as a UTF-8 byte string, assuming it is an array of numbers representing Unicode code points.

Return

Notes

This is the opposite of the string method :decode().

Object Methods

:len() #

Get number of pairs in object.

Return

:has(key) #

Check for key within object.

Parameters

Return

:key(n) #

Get the nth key within object.

Parameters

Return

Notes

Key order is preserved in Axiom (unlike most other programming languages). The key order is the order in which the keys were added.

:keys() #

Get all keys within object.

Return

Notes

Key order is preserved in Axiom (unlike most other programming languages). The key order is the order in which the keys were added.

:remove(key) #

Remove pair with key key from object.

Parameters