Bind a function to a specific object
Bind a function to a specific object
Name | Type | Description |
---|---|---|
me | Object | Object to bind to |
fn | Function | Function to bind |
var obj = {a: 1, b: function() { console.log(a); }};
var bound = splunkjs.Utils.bind(obj, obj.b);
bound(); // should print 1
root.bind = function(me, fn) {
return function() {
return fn.apply(me, arguments);
};
};
Strip a string of all leading and trailing whitespace.
Name | Type | Description |
---|---|---|
str | String | The string to trim |
var a = " aaa ";
var b = splunkjs.Utils.trim(a); //== "aaa"
root.trim = function(str) {
str = str || "";
if (String.prototype.trim) {
return String.prototype.trim.call(str);
}
else {
return str.replace(/^\\s\\s*/, '').replace(/\\s\\s*$/, '');
}
};
Whether an array contains a specific object
Name | Type | Description |
---|---|---|
arr | Array | The array to search in |
search | Anything | The thing to search for |
var a = ["a", "b', "c"];
console.log(splunkjs.Utils.indexOf(a, "b")) //== 1
console.log(splunkjs.Utils.indexOf(a, "d")) //== -1
root.indexOf = function(arr, search) {
for(var i=0; i<arr.length; i++) {
if (arr[i] === search) {
return i;
}
}
return -1;
};
Whether an array contains a specific object
Name | Type | Description |
---|---|---|
arr | Array | Array to search |
obj | Anything | Whether the array contains the element |
var a = {a: 3};
var b = [{}, {c: 1}, {b: 1}, a];
var contained = splunkjs.Utils.contains(b, a); // should be tree
root.contains = function(arr, obj) {
arr = arr || [];
return (root.indexOf(arr, obj) >= 0);
};
Whether a string starts with a specific prefix.
Name | Type | Description |
---|---|---|
original | String | String to search |
prefix | String | Prefix to search with |
var starts = splunkjs.Utils.startsWith("splunk-foo", "splunk-");
root.startsWith = function(original, prefix) {
var matches = original.match("^" + prefix);
return matches && matches.length > 0 && matches[0] === prefix;
};
Whether a string ends with a specific suffix.
Name | Type | Description |
---|---|---|
original | String | String to search |
suffix | String | Suffix to search with |
var ends = splunkjs.Utils.endsWith("foo-splunk", "-splunk");
root.endsWith = function(original, suffix) {
var matches = original.match(suffix + "$");
return matches && matches.length > 0 && matches[0] === suffix;
};
var toString = Object.prototype.toString;
Convert an iterable to an array.
Name | Type | Description |
---|---|---|
iterable | Arguments | Iterable to conver to an array |
function() {
console.log(arguments instanceof Array); // false
var arr = console.log(splunkjs.Utils.toArray(arguments) instanceof Array); // true
}
root.toArray = function(iterable) {
return Array.prototype.slice.call(iterable);
};
Whether or not the argument is an array
Name | Type | Description |
---|---|---|
obj | Anything | Parameter to check whether it is an array |
function() {
console.log(splunkjs.Utils.isArray(arguments)); // false
console.log(splunkjs.Utils.isArray([1,2,3])); // true
}
root.isArray = Array.isArray || function(obj) {
return toString.call(obj) === '[object Array]';
};
Whether or not the argument is a function
Name | Type | Description |
---|---|---|
obj | Anything | Parameter to check whether it is a function |
function() {
console.log(splunkjs.Utils.isFunction([1,2,3]); // false
console.log(splunkjs.Utils.isFunction(function() {})); // true
}
root.isFunction = function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
};
Whether or not the argument is a number
Name | Type | Description |
---|---|---|
obj | Anything | Parameter to check whether it is a number |
function() {
console.log(splunkjs.Utils.isNumber(1); // true
console.log(splunkjs.Utils.isNumber(function() {})); // false
}
root.isNumber = function(obj) {
return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
};
Whether or not the argument is a string
Name | Type | Description |
---|---|---|
obj | Anything | Parameter to check whether it is a string |
function() {
console.log(splunkjs.Utils.isString("abc"); // true
console.log(splunkjs.Utils.isString(function() {})); // false
}
root.isString = function(obj) {
return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
};
Whether or not the argument is an object
Name | Type | Description |
---|---|---|
obj | Anything | Parameter to check whether it is an object |
function() {
console.log(splunkjs.Utils.isObject({abc: "abc"}); // true
console.log(splunkjs.Utils.isObject("abc"); // false
}
root.isObject = function(obj) {
return obj === Object(obj);
};
Whether or not the argument is empty
Name | Type | Description |
---|---|---|
obj | Anything | Parameter to check whether it is empty |
function() {
console.log(splunkjs.Utils.isEmpty({})); // true
console.log(splunkjs.Utils.isEmpty({a: 1})); // false
}
root.isEmpty = function(obj) {
if (root.isArray(obj) || root.isString(obj)) {
return obj.length === 0;
}
for (var key in obj) {
if (this.hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
};
Apply the iterator function to each element in the object
Name | Type | Description |
---|---|---|
obj | Object,Array | Object/array to iterate over |
iterator | Function | Function to apply with each element: |
context | Object | An optional context to apply the function on |
splunkjs.Utils.forEach([1,2,3], function(el) { console.log(el); }); // 1,2,3
root.forEach = function(obj, iterator, context) {
if (obj === null) {
return;
}
if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
if (i in obj && iterator.call(context, obj[i], i, obj) === {}) {
return;
}
}
} else {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (iterator.call(context, obj[key], key, obj) === {}) {
return;
}
}
}
}
};
Extend a given object with all the properties in passed-in objects
Name | Type | Description |
---|---|---|
obj | Object | Object to extend |
sources | Object... | Sources to extend from |
function() {
console.log(splunkjs.Utils.extend({foo: "bar"}, {a: 2})); // {foo: "bar", a: 2}
}
root.extend = function(obj) {
root.forEach(Array.prototype.slice.call(arguments, 1), function(source) {
for (var prop in source) {
obj[prop] = source[prop];
}
});
return obj;
};
Create a shallow-cloned copy of the object/array
Name | Type | Description |
---|---|---|
obj | Object,Array | Object/array to clone |
function() {
console.log(splunkjs.Utils.clone({foo: "bar"})); // {foo: "bar"}
console.log(splunkjs.Utils.clone([1,2,3])); // [1,2,3]
}
root.clone = function(obj) {
if (!root.isObject(obj)) {
return obj;
}
return root.isArray(obj) ? obj.slice() : root.extend({}, obj);
};
Extract namespace information from a properties dictionary
Name | Type | Description |
---|---|---|
props | Object | Properties dictionary |
root.namespaceFromProperties = function(props) {
return {
owner: props.acl.owner,
app: props.acl.app,
sharing: props.acl.sharing
};
};
})();