-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.js
39 lines (36 loc) · 1.31 KB
/
util.js
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
'use strict';
const Util = {
/**
* Checks if obj is an object and it has no properties
* @param {Object|*} obj the variable to check.
* @return {boolean} false if obj is an object and has values, otherwise return true.
*/
isEmptyObject(obj) {
return obj === undefined || obj === null || typeof obj !== 'object' || Object.keys(obj).length < 1;
},
/**
* Adds `allIndicesOf(substring)` function to the string prototype
*/
polyfillAllIndicesOf() {
/**
* Gets the indices of a string in another string.
* Finds the first index of the substring in the string using String.indexOf,
* then calls String.substring(index) to cut out the already searched part of the string.
* This repeats until there are no more instances of the substring.
* @param {String} substring the string to get the indices of
* @return {Array<Number>} indices of the substring in the string
*/
String.prototype.allIndicesOf = function(substring) {
let indices = [this.indexOf(substring)];
do {
let prevIndex = indices[indices.length - 1] + 1;
var index = this.substring(prevIndex).indexOf(substring);
if (index > -1) {
indices.push(index + prevIndex);
}
} while (index > -1);
return indices;
};
}
};
module.exports = Util;