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.extendfromExtendable()rather than inheriting it because it inherits fromError()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
trueif v is defined and not null, andfalseotherwise.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
objto a function.If
objis a function, return the function. Otherwise return a constant function that returnsobjwhen called.Arguments: - obj (Object) – The object to coerce.
-
infer_addr_type(delivery_class)¶ Return the address type for the given delivery class.
A
delivery classis type of system that messages can be sent or received over. Common values aresms,ussd,gtalk,twitter,mxitandwechat.An
address typeis a type of address used to identify a user and corresponds to a field on aContact()object. Common values aremsisdn,gtalk_idandtwitter_handler,mxit_idandwechat_id.If the
delivery_classisn’t know, thedelivery_classitself is returned as theaddress_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
trueif 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
objis a function, call it with the given arguments and return the result. Otherwise returnobj.Arguments: - obj (Object) – The function to call or result to return.
- that (Object) – The value of
thisto 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
trueif haystack starts with needle andfalseotherwise.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
momentcan interpret as a UTC date.
- date (obj) – A value