bind

splunkjs.Utils.bind

Binds a function to a specific object.

Syntax

root.bind = function(me, fn)

Parameters

Name Type Description
me Object

The object to bind to.

fn Function

The function to bind.

Return

Function.

The bound function.

Examples

 var obj = {a: 1, b: function() { console.log(a); }};
 var bound = splunkjs.Utils.bind(obj, obj.b);
 bound(); // prints 1

clone

splunkjs.Utils.clone

Creates a shallow-cloned copy of an object or array.

Syntax

root.clone = function(obj)

Parameters

Name Type Description
obj Object,Array

The object or array to clone.

Return

Object,Array.

The cloned object or array.

Examples

 function() { 
     console.log(splunkjs.Utils.clone({foo: "bar"})); // {foo: "bar"}
     console.log(splunkjs.Utils.clone([1,2,3])); // [1,2,3]
 }

contains

splunkjs.Utils.contains

Indicates whether an array contains a specific object.

Syntax

root.contains = function(arr, obj)

Parameters

Name Type Description
arr Array

The array to search in.

obj Anything

The object to search for.

Return

Boolean.

true if the array contains the object, false if not.

Examples

 var a = {a: 3};
 var b = [{}, {c: 1}, {b: 1}, a];
 var contained = splunkjs.Utils.contains(b, a); // true

endsWith

splunkjs.Utils.endsWith

Indicates whether a string ends with a specific suffix.

Syntax

root.endsWith = function(original, suffix)

Parameters

Name Type Description
original String

The string to search in.

suffix String

The suffix to search for.

Return

Boolean.

true if the string ends with the suffix, false if not.

Examples

 var ends = splunkjs.Utils.endsWith("foo-splunk", "-splunk");

extend

splunkjs.Utils.extend

Extends a given object with all the properties from other source objects.

Syntax

root.extend = function(obj)

Parameters

Name Type Description
obj Object

The object to extend.

sources Object...

The source objects from which to take properties.

Return

Object.

The extended object.

Examples

 function() { 
     console.log(splunkjs.Utils.extend({foo: "bar"}, {a: 2})); // {foo: "bar", a: 2}
 }

forEach

splunkjs.Utils.forEach

Applies an iterator function to each element in an object.

Syntax

root.forEach = function(obj, iterator, context)

Parameters

Name Type Description
obj Object,Array

An object or array.

iterator Function

The function to apply to each element: (element, list, index).

context Object

A context to apply to the function (optional).

Examples

 splunkjs.Utils.forEach([1,2,3], function(el) { console.log(el); }); // 1,2,3

getWithVersion

splunkjs.Utils.getWithVersion

Finds a version in a dictionary.

Syntax

root.getWithVersion = function(version, map)

Parameters

Name Type Description
version String

The version to search for.

map Object

The dictionary to search.

Return

Anything.

The value of the dictionary at the closest version match.

indexOf

splunkjs.Utils.indexOf

Searches an array for a specific object and returns its location.

Syntax

root.indexOf = function(arr, search)

Parameters

Name Type Description
arr Array

The array to search in.

search Anything

The object to search for.

Return

Number.

The index of the object (search), or -1 if the object wasn't found.

Examples

 var a = ["a", "b', "c"];
 console.log(splunkjs.Utils.indexOf(a, "b")) //== 1
 console.log(splunkjs.Utils.indexOf(a, "d")) //== -1

isArray

splunkjs.Utils.isArray

Indicates whether an argument is an array.

Syntax

root.isArray = Array.isArray || function(obj)

Parameters

Name Type Description
obj Anything

The argument to evaluate.

Return

Boolean.

true if the argument is an array, false if not.

Examples

 function() { 
     console.log(splunkjs.Utils.isArray(arguments)); // false
     console.log(splunkjs.Utils.isArray([1,2,3])); // true
 }

isEmpty

splunkjs.Utils.isEmpty

Indicates whether an argument is empty.

Syntax

root.isEmpty = function(obj)

Parameters

Name Type Description
obj Anything

The argument to evaluate.

Return

Boolean.

true if the argument is empty, false if not.

Examples

 function() { 
     console.log(splunkjs.Utils.isEmpty({})); // true
     console.log(splunkjs.Utils.isEmpty({a: 1})); // false
 }

isFunction

splunkjs.Utils.isFunction

Indicates whether an argument is a function.

Syntax

root.isFunction = function(obj)

Parameters

Name Type Description
obj Anything

The argument to evaluate.

Return

Boolean.

true if the argument is a function, false if not.

Examples

 function() { 
     console.log(splunkjs.Utils.isFunction([1,2,3]); // false
     console.log(splunkjs.Utils.isFunction(function() {})); // true
 }

isNumber

splunkjs.Utils.isNumber

Indicates whether an argument is a number.

Syntax

root.isNumber = function(obj)

Parameters

Name Type Description
obj Anything

The argument to evaluate.

Return

Boolean.

true if the argument is a number, false if not.

Examples

 function() { 
     console.log(splunkjs.Utils.isNumber(1); // true
     console.log(splunkjs.Utils.isNumber(function() {})); // false
 }

isObject

splunkjs.Utils.isObject

Indicates whether an argument is an object.

Syntax

root.isObject = function(obj)

Parameters

Name Type Description
obj Anything

The argument to evaluate.

Return

Boolean.

true if the argument is an object, false if not.

Examples

 function() { 
     console.log(splunkjs.Utils.isObject({abc: "abc"}); // true
     console.log(splunkjs.Utils.isObject("abc"); // false
 }

isString

splunkjs.Utils.isString

Indicates whether an argument is a string.

Syntax

root.isString = function(obj)

Parameters

Name Type Description
obj Anything

The argument to evaluate.

Return

Boolean.

true if the argument is a string, false if not.

Examples

 function() { 
     console.log(splunkjs.Utils.isString("abc"); // true
     console.log(splunkjs.Utils.isString(function() {})); // false
 }

keyOf

splunkjs.Utils.keyOf

Tests whether a value appears in a given object.

Syntax

root.keyOf = function(val, obj)

Parameters

Name Type Description
val Anything

The value to search for.

obj Object

The object to search in.

namespaceFromProperties

splunkjs.Utils.namespaceFromProperties

Extracts namespace information from a dictionary of properties. Namespace information includes values for owner, app, and sharing.

Syntax

root.namespaceFromProperties = function(props)

Parameters

Name Type Description
props Object

The dictionary of properties.

Return

Object.

Namespace information from the properties dictionary.

parallel

splunkjs.Utils.parallel

Runs multiple functions (tasks) in parallel. Each task takes the function as a parameter. When all tasks have been completed or if an error occurs, the function returns a combined results of all tasks.

Note: Tasks might not be run in the same order as they appear in the array, but the results will be returned in that order.

Syntax

root.parallel = async function (tasks, fromMap)

Parameters

Name Type Description
tasks Function

An array of functions.

fromMap Boolean

set to true when method call is made from parallerMap function. (optional)

Examples

 let [err, one, two] = await Utils.parallel([
     function() {
         return [null, 1];
     },
     function() {
         return [null, 2, 3];
     }]
 );
 console.log(err); // == null
 console.log(one); // == 1
 console.log(two); // == [1,2]

parallelEach

splunkjs.Utils.parallelEach

Applies an asynchronous function over each element in an array, in parallel. If an error occurs, the function returns an error.

Syntax

root.parallelEach = async function (vals, fn)

Parameters

Name Type Description
vals Array

An array of values.

fn Function

A function (possibly asynchronous) to apply to each element.

Examples

 var total = 0;
 let err = await Utils.parallelEach(
     [1, 2, 3],
     async function(val, idx) { 
         var go = function() {
             total += val;
         };
         if (idx === 1) {
             await Utils.sleep(100);
             go();
         }
         else {
             go();
         }
     });
 console.log(total); // == 6

parallelMap

splunkjs.Utils.parallelMap

Runs an asynchronous function (mapping it) over each element in an array, in parallel. When all tasks have been completed or if an error occurs, function returns the resulting array.

Syntax

root.parallelMap = async function (vals, fn)

Parameters

Name Type Description
vals Array

An array of values.

fn Function

A function (possibly asynchronous) to apply to each element.

Examples

 let [err, vals] = await Utils.parallelMap(
     [1, 2, 3],
     async function(val, idx) { 
         if (val === 2) {
             await Utils.sleep(100);
             return [null, val+1];
          }
         else {
             return [null, val + 1];
         }
     });
 console.log(vals); // == [2,3,4]

series

splunkjs.Utils.series

Runs multiple functions (tasks) in series. Each task takes the function as a parameter. When all tasks have been completed or if an error occurs, the function returns the combined results of all tasks in the order they were run.

Syntax

root.series = async function (tasks, fromMap)

Parameters

Name Type Description
tasks Function

An array of functions.

fromMap Boolean

set to true when method call is made from seriesMap function. (optional)

Examples

 var keeper = 0;
 let [err, one, two] = awiat Utils.series([
     async function() {
         await Utils.sleep(10);
         console.log(keeper++); // == 0
         return [null, 1];
     },
     function() {
         console.log(keeper++); // == 1
         return [null, 2, 3];
     }]
 );
 console.log(err); // == null
 console.log(one); // == 1
 console.log(two); // == [2, 3]

seriesEach

splunkjs.Utils.seriesEach

Applies an asynchronous function over each element in an array, in series. If an error occurs, the function returns an error.

Syntax

root.seriesEach = async function (vals, fn)

Parameters

Name Type Description
vals Array

An array of values.

fn Function

A function (possibly asynchronous)to apply to each element.

Examples

 var results = [1, 3, 6];
 var total = 0;
 let err = await Utils.seriesEach(
     [1, 2, 3],
     function(val, idx) { 
         total += val;
         console.log(total === results[idx]); //== true
     });
 console.log(total); //== 6

seriesMap

splunkjs.Utils.seriesMap

Runs an asynchronous function (mapping it) over each element in an array, in series. When all tasks have been completed or if an error occurs, function returns the resulting array.

Syntax

root.seriesMap = async function (vals, fn)

Parameters

Name Type Description
vals Array

An array of values.

fn Function

A function (possibly asynchronous) to apply to each element.

Examples

 var keeper = 1;
 let [err, vals] = await Utils.seriesMap(
     [1, 2, 3],
     function(val, idx) { 
         console.log(keeper++); // == 1, then 2, then 3
         return [null, val + 1];
     }
 );
 console.log(vals); // == [2,3,4];

sleep

splunkjs.Utils.sleep

can make a function to pause execution for a fixed amount of time

Syntax

root.sleep = function (ms)

Parameters

Name Type Description
ms Number

The timeout period, in milliseconds.

Examples

 await Utils.sleep(1000);

startsWith

splunkjs.Utils.startsWith

Indicates whether a string starts with a specific prefix.

Syntax

root.startsWith = function(original, prefix)

Parameters

Name Type Description
original String

The string to search in.

prefix String

The prefix to search for.

Return

Boolean.

true if the string starts with the prefix, false if not.

Examples

 var starts = splunkjs.Utils.startsWith("splunk-foo", "splunk-");

toArray

splunkjs.Utils.toArray

Converts an iterable to an array.

Syntax

root.toArray = function(iterable)

Parameters

Name Type Description
iterable Arguments

The iterable to convert.

Return

Array.

The converted array.

Examples

 function() { 
     console.log(arguments instanceof Array); // false
     var arr = console.log(splunkjs.Utils.toArray(arguments) instanceof Array); // true
 }

trim

splunkjs.Utils.trim

Strips a string of all leading and trailing whitespace characters.

Syntax

root.trim = function(str)

Parameters

Name Type Description
str String

The string to trim.

Return

String.

The trimmed string.

Examples

 var a = " aaa ";
 var b = splunkjs.Utils.trim(a); //== "aaa"

whilst

splunkjs.Utils.whilst

Runs an asynchronous while loop.

Syntax

root.whilst = async function(condition, body

Parameters

Name Type Description
condition Function

A function that returns a boolean indicating whether the condition has been met.

body Function

A function that runs the body of the loop.

Examples

 let i = 0;
 try {
     await Utils.whilst(
         function() { return i++ < 3; },
         async function() {
             await Utils.sleep(0);
         });
 } catch(err) {
     console.log(err);
 }