API Docs for: 0.6.0
Show:

sgvizler.util Class

Defined in: src/util.js:2

A helpful set of static utility functions: type checking variables, generic get-setter, get-setting values in hierarchial objects, array functions, DOM manipulation, and inheritance.

Dependencies:

  • jQuery

Methods

createHTMLElement

(
  • elementType
  • [attributes]
  • [children]
)
Object protected

Defined in src/util.js:285

Available since 0.6.0

Creates an HTML element according to a custom made "array syntax". Used to make HTML DOM manipulation more code compact.

Parameters:

  • elementType String

    The type of element to create, e.g., "div" or "h1".

  • [attributes] Object optional

    Object of attribute--value's to be added to the element.

  • [children] Array | Primitive optional

    An array of children to be added to the element; each element in the children array is an array of three elements, one for each parameter of this method. If this argument is a primitive, then it is inserted as a text node.

Returns:

Object:

Element (ready for insertion into DOM.)

Example:

createHTMLElement('ul', { 'class': "myClass", 'id': "myID" }, [ ['li', null, "One" ],
                                                                ['li', { 'id': "ABC" } , 2 ],
                                                                ['li', null, true] ] );

will create the HTML element:

<ul id="myID" class="myClass">
  <li>One</li>
  <li id="ABC">2</li>
  <li>true</li>
</ul>

getObjectByPath

(
  • path
  • [object=window]
  • [create=false]
)
Object protected

Defined in src/util.js:183

Available since 0.6.0

Gets the object located at path from object. path is given in dot notation.

Parameters:

Returns:

Object:

Returns the object/value located at the path of object; otherwise, if create is true, it is created.

Example:

getObjectByPath('sgvizler.visualization.Table', registry, true)

returns the object located at registry['sgvizler']['visualization']['Table'] if it exists; otherwise, since 'create' === true, the path and (empty) object is created and returned.

getset

(
  • attr
  • [value]
  • setObject
  • returnObject
)
Any protected

Defined in src/util.js:139

Available since 0.6.0

Generic set/get method. If value is defined, then the attribute/property attr of setObject is set to value and returnObject is returned. Otherwise, the (value of) attr attribute/property is returned. Useful for a casading pattern.

Parameters:

  • attr String

    The name of the property to get/set.

  • [value] Object optional

    The value to set.

  • setObject Object

    The object for which the property shall be set/get.

  • returnObject Object

    The object to return if value is undefined.

Returns:

Any:

Either returnObject or setObject[attr]

Example:

getset('age', 55, person.myArray, person)

sets person.myArray.age = 55 and returns person.

getset('age', undefined, person.myArray, person)

returns person.myArray.age.

inherit

(
  • Child
  • Parent
)
protected

Defined in src/util.js:114

Available since 0.6.0

Establish "classical inheritance" from Parent to Child. Child is linked to the Parent's prototype through a new proxy object. This means the Child has a prototype object of its own, and access to the Parent's prototype.

Taken from book "JavaScript Patterns".

Parameters:

isArray

(
  • input
)
Boolean protected

Defined in src/util.js:81

Available since 0.6.0

Checks if input is an array.

Parameters:

Returns:

Boolean:

True iff input is an array.

isBoolean

(
  • input
)
Boolean protected

Defined in src/util.js:45

Available since 0.6.0

Checks if input is a boolean.

Parameters:

Returns:

Boolean:

True iff input is a boolean.

isFunction

(
  • input
)
Boolean protected

Defined in src/util.js:70

Available since 0.6.0

Checks if input is a function.

Parameters:

Returns:

Boolean:

True iff input is a function.

isInArray

(
  • item
  • array
)
Boolean protected

Defined in src/util.js:219

Available since 0.6.0

Checks if a an array contains a given element.

Parameters:

Returns:

Boolean:

True iff array contains an element item.

isNumber

(
  • input
)
Boolean protected

Defined in src/util.js:33

Available since 0.6.0

Checks if input is a number.

Parameters:

Returns:

Boolean:

True iff input is a number.

isPrimitive

(
  • input
)
Boolean protected

Defined in src/util.js:57

Available since 0.6.0

Checks if input is a primitive, i.e., either a string, a number or a boolean.

Parameters:

Returns:

Boolean:

True iff input is a string, a number or a boolean.

isString

(
  • input
)
Boolean protected

Defined in src/util.js:21

Available since 0.6.0

Checks if input is a string.

Parameters:

Returns:

Boolean:

True iff input is a string.

isURL

(
  • input
)
Boolean protected

Defined in src/util.js:92

Available since 0.6.0

Checks if input is a URL.

Parameters:

Returns:

Boolean:

True iff input is a URL.

removeDuplicates

(
  • array
)
Array protected

Defined in src/util.js:232

Available since 0.6.0

Removes duplicates from an array.

Parameters:

Returns:

Array:

The input array with duplicates removed.

Example:

removeDuplicates([1, 1, 1, 2, 4, 3, 2]);  // returns [1, 2, 4, 3]

startsWith

(
  • string
  • prefix
)
Boolean protected

Defined in src/util.js:167

Available since 0.6.0

Checks if a string starts with (is the prefix of) an other string.

Parameters:

Returns:

Boolean:

True iff prefix is the prefix of string.

Example:

startsWith("Hal", "Hallo!");  // returns true
startsWith("hal", "Hallo!");  // returns false

toArray

(
  • input
)
Array protected

Defined in src/util.js:253

Available since 0.6.0

Converts input to an array. If input is undefined, then an empty array is returned. If input is primitive, then it is put in an (empty) array. If input /is/ an array, then the input is simply returned.

Useful for converting input to other methods to arrays.

Parameters:

  • input Undefined | Primitive | Array

Returns:

Array:

An array representation of input.

Example:

toArray(undefined);       // returns []
toArray('myString');      // returns ['myString']
toArray([1, 2, 3]);       // returns [1, 2, 3]
toArray(function () {});  // throws TypeError