Module

base/util

Utilities module.

Members

# static constant isArray

Check to see if the input parameter is actually an Array.

Checks if the object is an array.

Methods

# static clone(obj, deepopt) → {Object}

Clones an object.

Parameters:
Name Type Attributes Description
obj Object | Array

Object to check

deep Boolean <optional>

Deep copy

a copy of the object

Object

# static deepSearch(obj, key) → {*}

Provide "deep search" into an object to developer. It is best illustrated with example below, which is based on the question https://stackoverflow.com/questions/30091572/search-deep-nested-json

Parameters:
Name Type Description
obj Object

Object to be queried.

key String

The search term.

  • Guaranteed to return undefined if search item cannot be found. Searching for an empty string will also return undefined. Otherwise, the value held will be returned.
*
Example
var dummy = {
  "menuInputRequestId": 1,
  "catalog":[
    {
      "uid": 1,
      "name": "Pizza",
      "desc": "Italian cuisine",
      "products": [
        {
          "uid": 3,
          "name": "Devilled chicken",
          "desc": "chicken pizza",
          "prices":[
            {
              "uid": 7,
              "name": "regular",
              "price": "$10"
            },
            {
              "uid": 8,
              "name": "large",
              "price": "$12"
            }
          ]
        }
      ]
    },
    ///
 }

 // Return dummy.menuInputRequestId, which is 1.
 var test1 = deepSearch(dummy, "menuInputRequestId")

 // Return dummy.catalog, which will be an object.
 var test2 = deepSearch(dummy, "catalog")

 // Return dummy.catalog[0].products[0].prices[0].name, which is "regular".
 var test3 = deepSearch(dummy, "catalog.0.products.0.prices.0.name")

 // Return undefined, because of mis-spelling of the property "products" as "product".
 var test4 = deepSearch(dummy, "catalog.0.product")

 // Return undefined, because "catalog.0.products.1" is not available.
 var test5 = deepSearch(dummy, "catalog.0.products.1.name")

 // Return undefined, because "catalog.0.products.owner" is not available.
 var test6 = deepSearch(dummy, "catalog.0.products.owner.name")

# static empty(str) → {Boolean}

Determine if a string is empty or only containing whitespaces.

Parameters:
Name Type Description
str String

String to be checked.

True if it is, false otherwise.

Boolean
Example
empty(" "); // true

# static extend(dest, src, deepopt) → {Object}

Extends a dest with properties from src. Possible deep copy/extending by setting deep = true.

Parameters:
Name Type Attributes Description
dest Object

Destination object to be extended

src Object

Source object with properties to extend with

deep Boolean <optional>

Deep copy

extended object

Object

# static isBoolean(obj) → {Boolean}

Check to see if the input parameter is actually a Boolean object.

Parameters:
Name Type Description
obj Object

Object to be checked.

True if the object is, false otherwise.

Boolean

# static isEmpty(obj) → {Boolean}

Check and see if the input object is empty, i.e. {}, or not.

Parameters:
Name Type Description
obj Object

Object to be checked.

True if the object is, false otherwise.

Boolean

# static isFunction(obj) → {Boolean}

Check to see if the input parameter is actually a Function object.

Parameters:
Name Type Description
obj Object

Object to be checked.

True if the object is, false otherwise.

Boolean

# static isNull(obj) → {Boolean}

Check to see if the input parameter is equal to null.

Parameters:
Name Type Description
obj Object

Object to be checked.

True if the object is, false otherwise.

Boolean

# static isNumber(obj) → {Boolean}

Check to see if the input parameter is actually a number object.

Parameters:
Name Type Description
obj Object

Object to be checked.

True if the object is, false otherwise.

Boolean

# static isObject(obj) → {Boolean}

Check to see if the input parameter is actually an object.

Parameters:
Name Type Description
obj Object

Object to be checked.

True if the object is, false otherwise.

Boolean

# static isPlainObject(obj) → {Boolean}

Check to see if the input parameter is actually a plain object.

Parameters:
Name Type Description
obj Object

Object to be checked.

True if the object is, false otherwise.

Boolean

# static isProtocolRelativeUrl(url) → {Boolean}

Check to see if the input parameter is actually a String object.

Parameters:
Name Type Description
url String

URL to be checked.

True if the object is, false otherwise.

Boolean

# static isString(obj) → {Boolean}

Check to see if the input parameter is actually a String object.

Parameters:
Name Type Description
obj Object

Object to be checked.

True if the object is, false otherwise.

Boolean

# static isUndefined(obj) → {Boolean}

Check to see if the input parameter is actually undefined.

Parameters:
Name Type Description
obj Object

Object to be checked.

True if the object is, false otherwise.

Boolean

# static relativeProtocolToAbsolute(url) → {String}

Updates from Protocol-relative url type to absolute url based on the current protocol. '//url.com' => 'http(s)://url.com'

Parameters:
Name Type Description
url String

URL to be converted.

Properly formatted url.

String

# static safeRemoveChild(parent, child) → {Node|undefined}

Check both parent and child are Node, and child's parent is the parent defined before removing the child from parent. Doing so will avoid ANY exception because of parent or child is not a Node or when child's parent is not the one defined.

Parameters:
Name Type Description
parent *

Parent Node

child *

Child Node

Reference of the removed child node if succeed, undefined otherwise.

Node | undefined

# static toQueryString({Object}) → {String}

Convert an object's key-value pair to a query string.

Parameters:
Name Type Description
{Object} Object

object - Object to be converted.

Query String.

String
Example
// True-y cases, single object, multiple properties
toQueryString({
  key: 'value',
  number: 1,
  array: ['a', 'b'],
  boolean: true
});
// key=value&number=1&array=1&array=2&boolean=true

// Falsy cases, single property to clearly illustrate the results.
toQueryString({ empty_string: '' }); // empty_string=
toQueryString({ something: undefined }); // something
toQueryString({ or: null }); // or=
toQueryString({ 'long string': 'with space' }); // long%20string=with%20space