forked from splunk/splunk-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
480 lines (450 loc) · 14.3 KB
/
Copy pathutils.js
File metadata and controls
480 lines (450 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*!*/
// Copyright 2012 Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
(function() {
"use strict";
var fs = require("fs");
var path = require("path");
var root = exports || this;
/**
* Provides various utility functions, which are mostly modeled after
* [Underscore.js](http://documentcloud.github.com/underscore/).
*
* @module splunkjs.Utils
*/
/**
* Binds a function to a specific object.
*
* @example
*
* var obj = {a: 1, b: function() { console.log(a); }};
* var bound = splunkjs.Utils.bind(obj, obj.b);
* bound(); // prints 1
*
* @param {Object} me The object to bind to.
* @param {Function} fn The function to bind.
* @return {Function} The bound function.
*
* @function splunkjs.Utils
*/
root.bind = function(me, fn) {
return function() {
return fn.apply(me, arguments);
};
};
/**
* Strips a string of all leading and trailing whitespace characters.
*
* @example
*
* var a = " aaa ";
* var b = splunkjs.Utils.trim(a); //== "aaa"
*
* @param {String} str The string to trim.
* @return {String} The trimmed string.
*
* @function splunkjs.Utils
*/
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*$/, '');
}
};
/**
* Searches an array for a specific object and returns its location.
*
* @example
*
* var a = ["a", "b', "c"];
* console.log(splunkjs.Utils.indexOf(a, "b")) //== 1
* console.log(splunkjs.Utils.indexOf(a, "d")) //== -1
*
* @param {Array} arr The array to search in.
* @param {Anything} search The object to search for.
* @return {Number} The index of the object (`search`), or `-1` if the object wasn't found.
*
* @function splunkjs.Utils
*/
root.indexOf = function(arr, search) {
for(var i=0; i<arr.length; i++) {
if (arr[i] === search) {
return i;
}
}
return -1;
};
/**
* Indicates whether an array contains a specific object.
*
* @example
*
* var a = {a: 3};
* var b = [{}, {c: 1}, {b: 1}, a];
* var contained = splunkjs.Utils.contains(b, a); // true
*
* @param {Array} arr The array to search in.
* @param {Anything} obj The object to search for.
* @return {Boolean} `true` if the array contains the object, `false` if not.
*
* @function splunkjs.Utils
*/
root.contains = function(arr, obj) {
arr = arr || [];
return (root.indexOf(arr, obj) >= 0);
};
/**
* Indicates whether a string starts with a specific prefix.
*
* @example
*
* var starts = splunkjs.Utils.startsWith("splunk-foo", "splunk-");
*
* @param {String} original The string to search in.
* @param {String} prefix The prefix to search for.
* @return {Boolean} `true` if the string starts with the prefix, `false` if not.
*
* @function splunkjs.Utils
*/
root.startsWith = function(original, prefix) {
var matches = original.match("^" + prefix);
return matches && matches.length > 0 && matches[0] === prefix;
};
/**
* Indicates whether a string ends with a specific suffix.
*
* @example
*
* var ends = splunkjs.Utils.endsWith("foo-splunk", "-splunk");
*
* @param {String} original The string to search in.
* @param {String} suffix The suffix to search for.
* @return {Boolean} `true` if the string ends with the suffix, `false` if not.
*
* @function splunkjs.Utils
*/
root.endsWith = function(original, suffix) {
var matches = original.match(suffix + "$");
return matches && matches.length > 0 && matches[0] === suffix;
};
var toString = Object.prototype.toString;
/**
* Converts an iterable to an array.
*
* @example
*
* function() {
* console.log(arguments instanceof Array); // false
* var arr = console.log(splunkjs.Utils.toArray(arguments) instanceof Array); // true
* }
*
* @param {Arguments} iterable The iterable to convert.
* @return {Array} The converted array.
*
* @function splunkjs.Utils
*/
root.toArray = function(iterable) {
return Array.prototype.slice.call(iterable);
};
/**
* Indicates whether an argument is an array.
*
* @example
*
* function() {
* console.log(splunkjs.Utils.isArray(arguments)); // false
* console.log(splunkjs.Utils.isArray([1,2,3])); // true
* }
*
* @param {Anything} obj The argument to evaluate.
* @return {Boolean} `true` if the argument is an array, `false` if not.
*
* @function splunkjs.Utils
*/
root.isArray = Array.isArray || function(obj) {
return toString.call(obj) === '[object Array]';
};
/**
* Indicates whether an argument is a function.
*
* @example
*
* function() {
* console.log(splunkjs.Utils.isFunction([1,2,3]); // false
* console.log(splunkjs.Utils.isFunction(function() {})); // true
* }
*
* @param {Anything} obj The argument to evaluate.
* @return {Boolean} `true` if the argument is a function, `false` if not.
*
* @function splunkjs.Utils
*/
root.isFunction = function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
};
/**
* Indicates whether an argument is a number.
*
* @example
*
* function() {
* console.log(splunkjs.Utils.isNumber(1); // true
* console.log(splunkjs.Utils.isNumber(function() {})); // false
* }
*
* @param {Anything} obj The argument to evaluate.
* @return {Boolean} `true` if the argument is a number, `false` if not.
*
* @function splunkjs.Utils
*/
root.isNumber = function(obj) {
return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
};
/**
* Indicates whether an argument is a string.
*
* @example
*
* function() {
* console.log(splunkjs.Utils.isString("abc"); // true
* console.log(splunkjs.Utils.isString(function() {})); // false
* }
*
* @param {Anything} obj The argument to evaluate.
* @return {Boolean} `true` if the argument is a string, `false` if not.
*
* @function splunkjs.Utils
*/
root.isString = function(obj) {
return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
};
/**
* Indicates whether an argument is an object.
*
* @example
*
* function() {
* console.log(splunkjs.Utils.isObject({abc: "abc"}); // true
* console.log(splunkjs.Utils.isObject("abc"); // false
* }
*
* @param {Anything} obj The argument to evaluate.
* @return {Boolean} `true` if the argument is an object, `false` if not.
*
* @function splunkjs.Utils
*/
root.isObject = function(obj) {
/*jslint newcap:false */
return obj === Object(obj);
};
/**
* Indicates whether an argument is empty.
*
* @example
*
* function() {
* console.log(splunkjs.Utils.isEmpty({})); // true
* console.log(splunkjs.Utils.isEmpty({a: 1})); // false
* }
*
* @param {Anything} obj The argument to evaluate.
* @return {Boolean} `true` if the argument is empty, `false` if not.
*
* @function splunkjs.Utils
*/
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;
};
/**
* Applies an iterator function to each element in an object.
*
* @example
*
* splunkjs.Utils.forEach([1,2,3], function(el) { console.log(el); }); // 1,2,3
*
* @param {Object|Array} obj An object or array.
* @param {Function} iterator The function to apply to each element: `(element, list, index)`.
* @param {Object} context A context to apply to the function (optional).
*
* @function splunkjs.Utils
*/
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;
}
}
}
}
};
/**
* Extends a given object with all the properties from other source objects.
*
* @example
*
* function() {
* console.log(splunkjs.Utils.extend({foo: "bar"}, {a: 2})); // {foo: "bar", a: 2}
* }
*
* @param {Object} obj The object to extend.
* @param {Object...} sources The source objects from which to take properties.
* @return {Object} The extended object.
*
* @function splunkjs.Utils
*/
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;
};
/**
* Creates a shallow-cloned copy of an object or array.
*
* @example
*
* function() {
* console.log(splunkjs.Utils.clone({foo: "bar"})); // {foo: "bar"}
* console.log(splunkjs.Utils.clone([1,2,3])); // [1,2,3]
* }
*
* @param {Object|Array} obj The object or array to clone.
* @return {Object|Array} The cloned object or array.
*
* @function splunkjs.Utils
*/
root.clone = function(obj) {
if (!root.isObject(obj)) {
return obj;
}
return root.isArray(obj) ? obj.slice() : root.extend({}, obj);
};
/**
* Extracts namespace information from a dictionary of properties. Namespace
* information includes values for _owner_, _app_, and _sharing_.
*
* @param {Object} props The dictionary of properties.
* @return {Object} Namespace information from the properties dictionary.
*
* @function splunkjs.Utils
*/
root.namespaceFromProperties = function(props) {
if (root.isUndefined(props) || root.isUndefined(props.acl)) {
return {
owner: '',
app: '',
sharing: ''
};
}
return {
owner: props.acl.owner,
app: props.acl.app,
sharing: props.acl.sharing
};
};
/**
* Tests whether a value appears in a given object.
*
* @param {Anything} val The value to search for.
* @param {Object} obj The object to search in.
*
* @function splunkjs.Utils
*/
root.keyOf = function(val, obj) {
for (var k in obj) {
if (obj.hasOwnProperty(k) && obj[k] === val) {
return k;
}
}
return undefined;
};
/**
* Finds a version in a dictionary.
*
* @param {String} version The version to search for.
* @param {Object} map The dictionary to search.
* @return {Anything} The value of the dictionary at the closest version match.
*
* @function splunkjs.Utils
*/
root.getWithVersion = function(version, map) {
map = map || {};
var currentVersion = (version + "") || "";
while (currentVersion !== "") {
if (map.hasOwnProperty(currentVersion)) {
return map[currentVersion];
}
else {
currentVersion = currentVersion.slice(
0,
currentVersion.lastIndexOf(".")
);
}
}
return map["default"];
};
/**
* Checks if an object is undefined.
*
* @param {Object} obj An object.
* @return {Boolean} `true` if the object is undefined, `false` if not.
*/
root.isUndefined = function (obj) {
return (typeof obj === "undefined");
};
/**
* Read files in a way that makes unit tests work as well.
*
* @example
*
* // To read `splunk-sdk-javascript/tests/data/empty_data_model.json`
* // from `splunk-sdk-javascript/tests/test_service.js`
* var fileContents = utils.readFile(__filename, "../data/empty_data_model.json");
*
* @param {String} __filename of the script calling this function.
* @param {String} a path relative to the script calling this function.
* @return {String} The contents of the file.
*/
root.readFile = function(filename, relativePath) {
return fs.readFileSync(path.resolve(filename, relativePath)).toString();
};
})();