Library: utilities
Overview
A collection of miscellaneous utilities.
source: utilities.js
Constructors
Constructor for an object that holds a set of strings, with basic
set operations. This is better for keeping track of a set than
using an associative arrays, because it avoids key collisions with
javascript built-ins like 'toString', 'class', etc.
parameters
| string | initialString1 | |
| string | initialString2 | |
| string | etc | ... |
methods
- contains(x)
- add(x)
- remove(x)
- forEach(f)
The error thrown by wget and wpost if there's a problem retrieveing the requested URL.
parameters
| string | message | A status message describing why the action failed. |
| HttpResponse | info | Additional info, including headers and received data, if any. |
Description of an object containing the properties of the HTTP response returned by
wget and wpostproperties
- number status
- string statusInfo
- string data
- string contentType
- object headers
Functions
Returns a string version of the MD5 signature of x.
parameters
| String | x | a string |
returns
| String | the md5 hash of x |
examples
print(md5("appjet")); // prints "9b458805f67473b49761c13e48c5de35"
Iterator convenience for JavaScript Objects.
Note that if func returns false, the iteration will be immediately terminated.
(Returning undefined, or not specifying a return type, does not terminate the iteration).
parameters
| object | obj | The object over which to iterate. |
| function | func | The function to run on each [key,value] pair. |
examples
var pastels = {
red: "#fcc",
green: "#cfc",
blue: "#ccf"
};
eachProperty(pastels, function(key, value) {
print(DIV({style: 'background: '+value+';'}, key));
});
Douglas Crockford's "object" function for prototypal inheritance, taken from
http://javascript.crockford.com/prototypal.html
parameters
| object | parent | The parent object. |
returns
| object | A new object whose prototype is parent. |
Creates an array of the properties of
obj,
not including built-in or inherited properties. If no
argument is given, applies to the global object.parameters
| object | obj |
examples
// Prints "abc"
keys({a: 1, b: 2, c: 3}).forEach(function(k) {
print(k);
}
// Prints all the functions and object members of the global "appjet" object,
// one per line.
print(keys(appjet).join('\n'));
Comparator that returns -1, +1, or 0 depending on whether ab, or
neither, respectively.
parameters
| object | a | |
| object | b |
returns
| number | -1, 0, or +1 |
Removes leading and trailing whitespace from a string.
parameters
| string | str |
returns
| string | The trimmed string. |
Returns whether this set contains the given string.
parameters
| string | x |
returns
| boolean | Whether x exits in this set. |
Removes the given string from the set if it is contained in it.
parameters
| string | x | The string to remove from the set. |
Iterators over the strings in the set, calling the provided
function on each element.
parameters
| function | f | The function to call on each string in the set. |
Fetches the text of a URL and returns it as a string.
parameters
| string | url | The name of the url to retreive. If the transport is not specified, HTTP is assumed. |
| object | params? | Optional parameters to include with the GET, as a dictionary of {name: value} entries. |
| object | options? |
Optional object with three optional
properties:
|
returns
| string | The full text of the url's content. |
| HttpResponse |
The "complete" response, returned if the "complete" parameter is true.
|
examples
g = wget("google.com");
page.setMode("plain");
print(raw(g));
Simple way to POST data to a URL and get back the response. Values of params will
automatically be escaped.
parameters
| string | url | The url to POST to. |
| object | params? | Optional parameters to include with the POST, as a dictionary of {name: value} entries. |
| object | options? |
Optional object with three optional
properties:
|
returns
| string | The full text returned from the server POSTed to. |
| HttpResponse |
The complete response, returned if the "complete" parameter is true.
|
examples
result = wpost("example.com", {id: 25, value: "here is the post value"});
Simple way to send an email to a single recipient. Emails will have a
"from" address of
noreply@{appjet.appName}.{appjet.mainDomain}.
Sending is limited to 100 emails per developer account per day. However,
emails sent to the address on file for the app's owner are not counted
toward this limit.parameters
| string | toAddress | The one email address to send a message to. |
| string | subject | The message subject. |
| string | body | The message body. |
| object | headers? | Optional headers to include in the message, as a dictionary of {name: value} entries. |
examples
result = sendEmail("noone@example.com", "Test Subject",
"Greetings!", {"Reply-To": "sender@example.com"});
Objects
number
HttpResponse.status
The status code of the server's response, or an internal status code if connecting to the server failed.
string
HttpResponse.statusInfo
An explanation of the status code if it is an internal code (that is, less than 0)
object
HttpResponse.headers
An object containing property names and values corresponding to the headers returned by the server.