# Liquid.utils and other helper functions

The framework contains some helper functions, mainly within the Liquid.utils namespace. In particular, the Liquid.utils.Dictionary() is extremely widely used within the framework.

&#x20;

### 7.1      Liquid.utils.Dictionary() <a href="#id-5.1_fxb.utils.dictionary" id="id-5.1_fxb.utils.dictionary"></a>

The Liquid.utils.Dictionary class is a dictionary/hashtable which is extremely widely used within the Liquid Charts Pro framework. For example, the Liquid.Instruments collection is a dictionary, and so is each Liquid.Message which is sent to an OnMessage() handler.

&#x20;

The dictionary's properties and methods are as follows:

&#x20;

| Object           | Description                                                                                                                                                                                                                                                              |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| length           | Property returning the number of items in the dictionary                                                                                                                                                                                                                 |
| set(key, object) | Sets a key/value pair in the dictionary                                                                                                                                                                                                                                  |
| remove(key)      | Removes a key/value pair from the dictionary. (Trying to remove a non-existent key is not an error; it just silently does nothing.)                                                                                                                                      |
| get(key)         | Returns a value from the dictionary based on its key, or null if the key is not present                                                                                                                                                                                  |
| has(key)         | Tests whether a key exists in the dictionary                                                                                                                                                                                                                             |
| is(keyOrArray)   | Can take either a single key as a parameter, or an array of keys. Passing a single key is simply equivalent to has(). Passing an array means "does the dictionary contain at least one of these keys?". In other words, using is() with an array does an is-any-of test. |
| keys()           | Returns all the keys in the dictionary as an array                                                                                                                                                                                                                       |
| values()         | Returns all the values in the dictionary as an array                                                                                                                                                                                                                     |
| clear()          | Empties the dictionary                                                                                                                                                                                                                                                   |
| forEach(fn)      | Iterator. See description below.                                                                                                                                                                                                                                         |

&#x20;

The dictionary's forEach() is similar to standard Javascript Array.forEach() except that it allows early exit, by returning a non-null from the iteration function. For example, you can scan the dictionary and terminate at the first match, avoiding unnecessary further iteration through the dictionary:

&#x20;

// See if there is any object in the dictionary which has some particular value

var bHasValueWithProperty = false;

myDictionary.forEach(function (key, object) {

&#x20;      if (object.someProperty == someValue) {

&#x20;                  // Set flag

&#x20;                  bHasValueWithProperty = true;

&#x20;

&#x20;                  // Exit from iteration, and avoid unnecessary further looping

&#x20;                  return true;

&#x20;      }

});

&#x20;

Any non-null return value which terminates the iteration is passed back as the return value from forEach(). Therefore, the following is possible:

&#x20;

// See if there is any object in the dictionary which has some particular value.

// On return, matchingObject will contain either null or the first matching item.

var matchingObject = myDictionary.forEach(function (key, object) {

&#x20;      if (object.someProperty == someValue) {

&#x20;                  // Exit from iteration, and avoid unnecessary further looping.

&#x20;                  // The non-null value becomes the return value from the forEach().

&#x20;                  return object;

&#x20;      }

});

&#x20;

Note: it is safe to modify the contents of the dictionary during forEach() iteration. For example:

&#x20;

// Create a dictionary with 10 items

var x = new Liquid.utils.Dictionary();

for (var i = 1; i <= 10; i++) x.set(i, i);

&#x20;

// Gets called 9 times because item 3 is removed on the first pass (where k = 1)

x.forEach (function (k, v) {

&#x20;  if (x.has(3)) x.remove(3); // Remove item 3 on first iteration

});

&#x20;

### 7.2      Liquid.utils.Clone() <a href="#id-5.2_fxb.utils.clone" id="id-5.2_fxb.utils.clone"></a>

Liquid.utils.Clone() is a simple function for doing a deep clone of an object, but excluding any functions within the object (making it safe for serialisation). The current internal implementation of Liquid.utils.Clone(x) is simply JSON.parse(JSON.stringify(x)), but this may change in future if a faster alternative becomes available.

&#x20;

### 7.3      Liquid.utils.IsEmpty() <a href="#toc104559783" id="toc104559783"></a>

Simple helper function which tests whether an object x is an empty object, {}. For example:

&#x20;

var bIsEmpty = Liquid.utils.IsEmpty(someObject);

&#x20;

### 7.4      Liquid.utils.GenerateGuid() <a href="#id-7.4_fxb.utils.generateguid" id="id-7.4_fxb.utils.generateguid"></a>

Liquid.utils.GenerateGuid() creates an ID (32 hexadecimal characters) which is guaranteed to be unique within the current browser session, and is not guaranteed but is overwhelmingly likely to be globally unique.

&#x20;

### 7.5      Liquid.utils.GenerateHash() <a href="#toc58316709" id="toc58316709"></a>

Liquid.utils.GenerateHash(string) generates a hash of a string as a 32-bit integer. The hashing algorithm is subject to change, but is currently murmur32.

&#x20;

### 7.6      \$$() <a href="#toc104559786" id="toc104559786"></a>

For widgets only – because scripts and UDIXes are web workers, and have no HTML document – the framework creates a \$$() short-hand for accessing DOM nodes. For example:

&#x20;

\$$("btnSave").disabled = true;

&#x20;

\$$() is simply a short-hand alternative to document.getElementById(x), though its full functionality is slightly broader. If there is no DOM node with ID x, then \$$() goes on to try document.getElementsByClassName(x). There are three possible results from getElementsByClassName():

&#x20;

·      Empty array. \$$() returns null.

·      Array with single item. \$$() returns that single item (not the array).

·      Array with multiple items. \$$() returns the array.

&#x20;

To put all that another way:

&#x20;

·      If there is a single node with class=x, then \$$(x) is equivalent to document.getElementsByClassName(x)\[0]

·      If there are multiple nodes with class=x, then \$$(x) is equivalent to document.getElementsByClassName(x)
