sgvizler.util Class
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
Item Index
Methods
createHTMLElement
-
elementType
-
[attributes]
-
[children]
Creates an HTML element according to a custom made "array syntax". Used to make HTML DOM manipulation more code compact.
Parameters:
-
elementType
StringThe type of element to create, e.g., "div" or "h1".
-
[attributes]
Object optionalObject of attribute--value's to be added to the element.
-
[children]
Array | Primitive optionalAn 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:
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]
Gets the object located at path
from object
. path
is given in dot notation.
Returns:
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
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:
Returns:
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
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".
isArray
-
input
Checks if input
is an array.
Parameters:
-
input
Object
Returns:
True iff input
is an array.
isBoolean
-
input
Checks if input
is a boolean.
Parameters:
-
input
Object
Returns:
True iff input
is a boolean.
isFunction
-
input
Checks if input
is a function.
Parameters:
-
input
Object
Returns:
True iff input
is a function.
isInArray
-
item
-
array
Checks if a an array contains a given element.
Parameters:
-
item
Any -
array
Array
Returns:
True iff array
contains an element item
.
isNumber
-
input
Checks if input
is a number.
Parameters:
-
input
Object
Returns:
True iff input
is a number.
isPrimitive
-
input
Checks if input
is a primitive, i.e., either a string,
a number or a boolean.
Parameters:
-
input
Object
Returns:
True iff input
is a string, a number or a boolean.
isString
-
input
Checks if input
is a string.
Parameters:
-
input
Object
Returns:
True iff input
is a string.
isURL
-
input
Checks if input
is a URL.
Parameters:
-
input
Object
Returns:
True iff input
is a URL.
removeDuplicates
-
array
Removes duplicates from an array.
Parameters:
-
array
Array
Returns:
The input array with duplicates removed.
Example:
removeDuplicates([1, 1, 1, 2, 4, 3, 2]); // returns [1, 2, 4, 3]
startsWith
-
string
-
prefix
Checks if a string starts with (is the prefix of) an other string.
Returns:
True iff prefix
is the prefix of string
.
Example:
startsWith("Hal", "Hallo!"); // returns true
startsWith("hal", "Hallo!"); // returns false
toArray
-
input
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:
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