bind

splunkjs.Utils.bind

Bind a function to a specific object

Syntax

root.bind = function(me, fn)

Parameters

Name Type Description
me Object

Object to bind to

fn Function

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(); // should print 1

trim

splunkjs.Utils.trim

Strip a string of all leading and trailing whitespace.

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"

indexOf

splunkjs.Utils.indexOf

Whether an array contains a specific object

Syntax

root.indexOf = function(arr, search)

Parameters

Name Type Description
arr Array

The array to search in

search Anything

The thing to search for

Return

Number. The index of `search` or `-1` if it 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

contains

splunkjs.Utils.contains

Whether an array contains a specific object

Syntax

root.contains = function(arr, obj)

Parameters

Name Type Description
arr Array

Array to search

obj Anything

Whether the array contains the element

Return

Boolean. Whether the array contains the element

Examples

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

startsWith

splunkjs.Utils.startsWith

Whether a string starts with a specific prefix.

Syntax

root.startsWith = function(original, prefix)

Parameters

Name Type Description
original String

String to search

prefix String

Prefix to search with

Return

Boolean. Whether the string starts with the prefix

Examples

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

endsWith

splunkjs.Utils.endsWith

Whether a string ends with a specific suffix.

Syntax

root.endsWith = function(original, suffix)

Parameters

Name Type Description
original String

String to search

suffix String

Suffix to search with

Return

Boolean. Whether the string ends with the suffix

Examples

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

toArray

splunkjs.Utils.toArray

Convert an iterable to an array.

Syntax

root.toArray = function(iterable)

Parameters

Name Type Description
iterable Arguments

Iterable to conver to an array

Return

Array. The converted array

Examples

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

isArray

splunkjs.Utils.isArray

Whether or not the argument is an array

Syntax

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

Parameters

Name Type Description
obj Anything

Parameter to check whether it is an array

Return

Boolean. Whether or not the passed in parameter was an array

Examples

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

isFunction

splunkjs.Utils.isFunction

Whether or not the argument is a function

Syntax

root.isFunction = function(obj)

Parameters

Name Type Description
obj Anything

Parameter to check whether it is a function

Return

Boolean. Whether or not the passed in parameter was a function

Examples

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

isNumber

splunkjs.Utils.isNumber

Whether or not the argument is a number

Syntax

root.isNumber = function(obj)

Parameters

Name Type Description
obj Anything

Parameter to check whether it is a number

Return

Boolean. Whether or not the passed in parameter was a number

Examples

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

isString

splunkjs.Utils.isString

Whether or not the argument is a string

Syntax

root.isString = function(obj)

Parameters

Name Type Description
obj Anything

Parameter to check whether it is a string

Return

Boolean. Whether or not the passed in parameter was a string

Examples

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

isObject

splunkjs.Utils.isObject

Whether or not the argument is an object

Syntax

root.isObject = function(obj)

Parameters

Name Type Description
obj Anything

Parameter to check whether it is an object

Return

Boolean. Whether or not the passed in parameter was a object

Examples

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

isEmpty

splunkjs.Utils.isEmpty

Whether or not the argument is empty

Syntax

root.isEmpty = function(obj)

Parameters

Name Type Description
obj Anything

Parameter to check whether it is empty

Return

Boolean. Whether or not the passed in parameter was empty

Examples

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

forEach

splunkjs.Utils.forEach

Apply the iterator function to each element in the object

Syntax

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

Parameters

Name Type Description
obj Object,Array

Object/array to iterate over

iterator Function

Function to apply with each element: (element, list, index)

context Object

An optional context to apply the function on

Examples

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

extend

splunkjs.Utils.extend

Extend a given object with all the properties in passed-in objects

Syntax

root.extend = function(obj)

Parameters

Name Type Description
obj Object

Object to extend

sources Object...

Sources to extend from

Return

Object. The extended object

Examples

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

clone

splunkjs.Utils.clone

Create a shallow-cloned copy of the object/array

Syntax

root.clone = function(obj)

Parameters

Name Type Description
obj Object,Array

Object/array to clone

Return

Object,Array. The cloned object/array

Examples

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

namespaceFromProperties

splunkjs.Utils.namespaceFromProperties

Extract namespace information from a properties dictionary

Syntax

root.namespaceFromProperties = function(props)

Parameters

Name Type Description
props Object

Properties dictionary

Return

Object. Namespace information (owner, app, sharing) for the given properties