Utils

class BaseError()

An extendable error class that inherits from Error().

Example usage:

var MyError = BaseError.extend(function(self, message) {
    self.name = "MyError";
    self.message = message;
});

BaseError() copies .extend from Extendable() rather than inheriting it because it inherits from Error() already.

class DeprecationError()

Thrown when deprecated functionality is used.

class Extendable()

A base class for extendable classes.

class Extendable.extend(Child)

Create a sub-class.

Arguments:
  • Child (Class) – The constructor for the child class.

Example usage:

var MyClass = Extendable.extend(function(self, name) {
    self.my_name = name;
});

var OtherClass = MyClass.extend(function(self, other) {
    MyClass.call("custom_name");
    self.other_var = other;
});
basic_auth(username, password)

Return an HTTP Basic authentication header value for the given username and password.

Arguments:
  • username (string) – The username to authenticate as.
  • password (string) – The password to authenticate with.
exists(v)

Return true if v is defined and not null, and false otherwise.

Arguments:
  • v (Object) – The value to check.
format_addr(addr, type)

Format an address as a standardized string.

This function delegates to the formatter format_addr[type] or returns the address unchanged if there is no custom formatter.

Arguments:
  • addr (string) – The address to format.
  • type (string) – The address type for the address.
format_addr.gtalk_id(addr)

Canonicalize a Gtalk address by stripping the device-specifier, if any.

Arguments:
  • addr (string) – The Gtalk address to format.
format_addr.msisdn(addr)

Canonicalize an MSISDN by adding a + prefix if one is not present.

Arguments:
  • addr (string) – The MSISDN to format.
functor(obj)

Coerce obj to a function.

If obj is a function, return the function. Otherwise return a constant function that returns obj when called.

Arguments:
  • obj (Object) – The object to coerce.
infer_addr_type(delivery_class)

Return the address type for the given delivery class.

A delivery class is type of system that messages can be sent or received over. Common values are sms, ussd, gtalk, twitter, mxit and wechat.

An address type is a type of address used to identify a user and corresponds to a field on a Contact() object. Common values are msisdn, gtalk_id and twitter_handler, mxit_id and wechat_id.

If the delivery_class isn’t know, the delivery_class itself is returned as the address_type.

Arguments:
  • delivery_class (string) – The delivery class to look up.
The mapping of delivery classes to address types is a low-level
implementation detail that is subject to change. Use higher-level
alternatives where possible.
inherit(Parent, Child)

Inherit the parent’s prototype and mark the child as extending the parent.

Arguments:
  • Parent (Class) – The parent class to inherit and extend from.
  • Child (Class) – The child class that inherits and extends.
is_integer(v)

Return true if v is of type number and has no fractional part.

Arguments:
  • v (Object) – The value to check.
maybe_call(obj, that, args)

Coerce a function to its result.

If obj is a function, call it with the given arguments and return the result. Otherwise return obj.

Arguments:
  • obj (Object) – The function to call or result to return.
  • that (Object) – The value of this to bind to the function.
  • args (Array) – Arguments to call the function with.
normalize_msisdn(number, country_code)

Normalizes an MSISDN number.

This function will normalize an MSISDN number by removing any invalid characters and adding the country code. It will return null if the given number cannot be normalized.

This function is based on the MSISDN normalize function found within the vumi utils.

Arguments:
  • number (string) – The number to normalize.
  • country_code (string) – (optional) The country code for the number.
starts_with(haystack, needle)

Return true if haystack starts with needle and false otherwise.

Arguments:
  • haystack (string) – The string to search within.
  • needle (string) – The string to look for.

If either parameter is false-like, it is treated as the empty string.

uuid()

Return a UUID (version 4).

vumi_utc(date)

Format a date in Vumi’s date format.

Arguments:
  • date (obj) – A value moment can interpret as a UTC date.