Namespace $sf.lib.lang

$sf.lib.lang Defines helper functions / objects for JavaScript common needs, such as type conversion and object serialization

Method Summary

Method Detail

  • <static> {Boolean} $sf.lib.lang.callable(A)
    Checks that a function reference can be called safely. Sometimes function references are part of objects that may have been garbage collected (such as a function reference from another window or dom element). This method checks the reference by making sure it has a constructor and toString properties. Note that this doesn't mean that the function itself when called (or its subsquent call stack), can't throw an error. . . simply that you are able to call it. . .
    Parameters:
    {Function} A
    reference to a JavaScript function
    Returns:
    {Boolean} true if function can be called safely, otherwise false.
  • <static> {Boolean} $sf.lib.lang.cbool(val)
    Forces type conversion of any JavaScript variable to a boolean. "Falsy" values such as "", 0, null, and undefined all return false String values of "0", "false", "no", "undefined", "null" also return false
    Parameters:
    {*} val
    Any JavaScript reference / value
    Returns:
    {Boolean} The normalized boolean value
  • <static> {Number|NaN|*} $sf.lib.lang.cnum(val, defVal, minVal, maxVal)
    Forces type convertion of any JavaScript variable to a number. Values / objects that cannot be converted, will be returned as NaN, unless a default value is specified, in which case the default value is used.
    Parameters:
    {*} val
    Any JavaScript reference / value
    {*} defVal Optional
    use this value if original value cannot be converted to a number, or if value is less than min value, or if value is less than max value.
    {Number} minVal Optional
    specifies the lowest numerical value, if original value is less than this value, the defVal will be returned.
    {Number} maxVal Optional
    specifies the greatest numerical value, if original value is greater than this value, the defVal will be returned.
    Returns:
    {Number|NaN|*} the converted value, otherwise NaN or default value
  • <static> {String} $sf.lib.lang.cstr(str)
    Forces type conversion of any JavaScript variable to a string value. Note that "falsy" values or values that cannot be converted will be returned as an empty string ("").
    Parameters:
    {*} str
    Any object that needs to be converted to a string value.
    Returns:
    {String} The normalized string value.
  • <static> {Object} $sf.lib.lang.def(str_ns, aug, root, no_ovr)
    Define a JavaScript Namespace within a given context
    Parameters:
    {String} str_ns
    The name of the namespace in dot notation as a string (e.g. "Foo.bar")
    {Object} aug Optional
    defines the object at the end of the namespace. If namespace is already specified, and this object is provided, the namespace will be augmented with properties from this object. If nothing is passed in, defaults to using an empty object.
    {Object} root Optional
    the root object from which the namespace is defined. If not passed in defaults to the global/window object
    {Boolean} no_ovr Optional
    if true, properties already defined on root with the same name will be ignored
    Returns:
    {Object} The object at the end of the namespace
  • <static> {String} $sf.lib.lang.guid(prefix)
    Generate a unique id string
    Parameters:
    {String} prefix Optional
    a substring to use a prefix
    Returns:
    {String} unique id string
  • <static> {String} $sf.lib.lang.jssafe_html(str)
    Given a string of HTML escape quote marks and seperate script tags so that browsers don't get tripped up during processing.
    Parameters:
    {String} str
    A string of HTML markup to be processed
    Returns:
    {String}
  • <static> {Object} $sf.lib.lang.mix(r, s, owned, skipFuncs, no_ovr)
    Mixed the properties of one object into another object. Note that this function is recursive
    Parameters:
    {Object} r
    The object that will receive properties
    {Object} s
    The object that will deliever properties
    {Boolean} owned Optional
    Whether or not to skip over properties that are part of the object prototype
    {Boolean} skipFuncs Optional
    Whether or not to skip over function references
    {Boolean} no_ovr Optional
    Whether or not to overwrite properties that may have already been filled out
    Returns:
    {Object} The receiver object passed in with potentially new properties added
  • <static> $sf.lib.lang.noop()
    A function reference that does nothing.
    Returns:
    undefined
  • <static> {*} $sf.lib.lang.ns(str_ns, root)
    Checks for the existence of a JavaScript namespace as opposed to def, which will automatically define the namespace with a given context.
    Parameters:
    {String} str_ns
    A string with . or [] notation of a JavaScript namesace (e.g. "foo.bar.show", or "foo['bar']['show']").
    {Object} root Optional
    the root object to check within. .defaults to global / window
    Returns:
    {*} The endpoint reference of the namespace or false if not found
  • <static> {Number} $sf.lib.lang.rand()
    Return a random integer anywhere from 0 to 99
    Returns:
    {Number} random number
  • <static> {Number} $sf.lib.lang.time()
    Return the current time in milliseconds, from the epoch
    Returns:
    {Number} current time
  • <static> {String} $sf.lib.lang.trim(str)
    Trim the begining and ending whitespace from a string. Note that this function will convert an argument to a string first for type safety purposes. If string cannot be converted, and empty string is returned
    Parameters:
    str
    Returns:
    {String} trimmed string