np

convert (lib)

See also: string.type

Description

The convert library consists of functions intended for the conversion and the formatting of data.

Library Functions

escapeHTML( raw ) → escapedString

Escapes a value such that HTML-relevant characters are transformed into their corresponding entities.

print( convert.escapeHTML('<script>alert("foo!");</script>') )
&lt;script&gt;alert(\&quot;foo!\&quot;);&lt;/script&gt;

escapeURI( raw ) → escapedString

Encodes a string so it becomes a validly escaped URL component.

print( '?b=' << convert.escapeURI('look at that!') )
?b=look%20at%20that%21

format( formatString   value1   ... ) → string

Returns a formatted string according to the rules defined in formatString.

print( convert.format('byte value to character: %c', 76) )
print( convert.format('exponential notation: %e', math.pi) )
print( convert.format('float: %f', math.pi) )
print( convert.format('float, expanded to 12 digits: %12f', math.pi) )
print( convert.format('float, with 12 decimal digits: %.12f', math.pi) )
print( convert.format('signed integer: %i', math.pi) )
print( convert.format('integer with leading zeros: %02i', math.pi) )
print( convert.format('octal: %o', 100) ) 
print( convert.format('quoted string: %q', 'world') )
print( convert.format('string: %s', 'hello') ) 
print( convert.format('string with padding: %10s', 'hello') ) 
print( convert.format('unsigned integer: %u', 100) )
print( convert.format('hex: %x %X', 100, 100) )
byte value to character: L
exponential notation: 3.141593e+00
float: 3.141593
float, expanded to 12 digits:     3.141593
float, with 12 decimal digits: 3.141592653590
signed integer: 3
integer with leading zeros: 03
octal: 144
quoted string: "world"
string: hello
string with padding:      hello
unsigned integer: 100
hex: 64 64

fromJSON( string ) → list

Parses a JSON-encoded string, builds an object from that and returns it.

print( convert.fromJSON('{ "foo":"bar" }') )
(: foo="bar" )

quote( value ) → string

Functionally identical to convert.format('%q', someString), the quote function quote-escapes a value.

print( convert.quote('"Look at me!') )
"\"Look at me!"

toJSON( value ) → string

Returns the JSON representation of an object.

print(convert.toJSON(: foo = 'bar' ))
{ "foo":"bar" }

tokenize( string ) → list

Tokenizes a string in an attempt to parse for a generic programming language. Token separators can be whitespace or any non-alphanumeric character.

print( convert.tokenize('(foo (bar baz))') )
(: "(" "foo" "(" "barbaz" ")" ")" )

toNumber( value ) → number

Returns the numerical value of an object. If defined, this function uses the toNumber behavior of the object.

-- converting any object to a number
new ns = '2001'
print( convert.toNumber(ns)+3 )
-- using the toNumber behavior
new l = create(: toNumber = { |=> 1234 })
print( convert.toNumber(l) )
2004
1234

toString( value ) → string

Returns the string representation of an object. If defined, this function uses the toString behavior of the object.

-- converting any object to a number
new ns = .101
print( convert.toString(ns) )
-- using the toString behavior
new l = create(: toString = { |=> 'Yo!' })
print( convert.toString(l) )
0.101
Yo!

unquote( string ) → string

Reverses the escaping performed by format.quote.

print( convert.unquote('"\"Look at me!"') )
"Look at me!