Namespace: utils

utils

Provide some utilities.

Dependencies:

None

Methods

(static) contains(array, key, value) → {Boolean}

Indicates if one of the object key match the specified value in the array of objects.

 const array = [
     {id: 1, name: "First", subObject: { subId: 4 }}, 
     {id: 2, name: "Second", subObject: { subId: 5 }}, 
     {id: 3, name: "Third", subObject: { subId: 6 }}
 ];
 portal.utils.contains(array, "id", 2); // => Return true
 portal.utils.contains(array, "id", 5); // => Return false
 portal.utils.contains(array, "name", "First"); // => Return true
 portal.utils.contains(array, "name", 1); // => Return false
 portal.utils.contains(array, "subObject.subId", 4); // => Return true
 portal.utils.contains(array, "subObject.subId", 8); // => Return false
Parameters:
Name Type Description
array Array

Array on which to search.

key String

Key on which to search.

value Object

Value to search.

Returns:

True if the object is find.

Type
Boolean

(static) escapeRegExp(str) → {String}

Escape the string for regular expression

Parameters:
Name Type Description
str String

Regular expression to escape

Returns:

Escaped regular expression

Type
String

(static) find(array, key, value, ignoreCaseopt) → {Object}

Find an object using object key which matches the specified value in the array of objects.

 const array = [
     {id: 1, name: "First", subObject: { subId: 4 }}, 
     {id: 2, name: "Second", subObject: { subId: 5 }}, 
     {id: 3, name: "Third", subObject: { subId: 6 }}
 ];
 portal.utils.find(array, "id", 2); // => Return {id: 2, name : "Second"...}
 portal.utils.find(array, "id", 5); // => Return undefined
 portal.utils.find(array, "name", "First"); // => Return {id: 1, name : "First"...}
 portal.utils.find(array, "name", 1); // => Return undefined
 portal.utils.find(array, "subObject.subId", 4); // => Return {id: 1, name : "First"...}
 portal.utils.find(array, "subObject.subId", 8); // => Return undefined
Parameters:
Name Type Attributes Default Description
array Array

Array on which to search.

key String

Key on which to search.

value Object

Value to search.

ignoreCase Boolean <optional>
false

Ignore the case of the value

Returns:

Returns the object found.

Type
Object

(static) fixUrlSlashes(url) → {String}

Fix URL slashes.

Make sure there is no double slashes in URL.

 const root = "http://my/root/url/";
 const rel = "/rel/url/from/root/";
 portal.fixUrlSlashes(root + rel);
 // => "http://my/root/url/rel/url/from/root/"
Parameters:
Name Type Description
url String

URL to fix.

Returns:

Modified URL.

Type
String

(static) getDataFromKey(item, key) → {String|Array.<String>}

Retrieve the data of the item using the specified key.

 portal.utils.getDataFromKey({
     "key" : "123",
     "key2" : {
         "subKey" : "456"
     }
 }, "key");
 // Produce "123"

 portal.utils.getDataFromKey({
     key : "123",
     key2 : {
         subKey : "456"
     }
 }, "key2.subKey");
 // Produce "456"

 portal.utils.getDataFromKey({
     "lastName" : "The name",
     "membership" : [{
         "id" : 1,
         "name" "First name"
     }, {
         "id" : 2,
         "name" "Second name"
     }]
 }, "membership[].name");
 // Produce ["First name", "Second name"]
Parameters:
Name Type Description
item Object

Item that hold the value to retrieve

key String

String that represent the key of the object

Returns:

Value(s) of the specified key

Type
String | Array.<String>

(static) isMatchGlob(text, pattern) → {Boolean}

Compares the string against a given pattern.

Parameters:
Name Type Description
text String

Text to verify

pattern String

The Globpattern to match, where "*" means any sequence of characters, and "?" means any single character.

Returns:

true if the text match the Glob pattern

Type
Boolean

(static) setDisableState(element, disableopt)

Set the disable state on the specified DOM element.

This method modify the disabled attribute but also disabled CSS class name.

 // Consider:
 <button id="myElement">I'm a button</button>

 const myElement = document.getElementById("myElement");

 // Disable
 portal.utils.setDisableState(myElement, true); 
 // <button id="myElement" class="disabled" disabled="disabled">I'm a button</button>

 // Enable
 portal.utils.setDisableState(myElement, false);
 // <button id="myElement">I'm a button</button>
Parameters:
Name Type Attributes Default Description
element HTMLElement

Element to mark as disabled

disable Boolean <optional>
false

Disable or not the element