-
Notifications
You must be signed in to change notification settings - Fork 0
/
utiljs.js
156 lines (137 loc) · 4.56 KB
/
utiljs.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
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
'use strict';
var arrayUtil = {}; // Utilities for working with JS arrays
var stringUtil = {}; // Utilities for working with JS strings
var objectUtil = {}; // Utilities for working with JS objects
/**
* Filter an array by removing elements that are found in the other array.
* @param {Array} array Array to filter.
* @param {Array} arrayToRemove Values to remove from the first array.
* @return {Array} A new array with the filtered elements.
*/
arrayUtil.filterArray = function(array, arrayToRemove) {
return array.filter(function(value) {
return arrayToRemove.indexOf(value) < 0;
});
};
/**
* Remove the specified item from an array.
*/
arrayUtil.remove = function(array, item) {
var index = array.indexOf(item);
if (index != -1) {
array.splice(index, 1);
}
};
/**
* @param {Array} array Array to shuffle.
* @return {Array} A shuffled copy of the array.
*/
arrayUtil.shuffle = function(array) {
array = array.slice(0);
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};
/**
* @param {Array} array Array to shuffle.
* @param {number} maxSubsetLength Maximum subset to return.
* @return {Array} A random subset of the array containing at most maxSubsetLength elements.
*/
arrayUtil.randomSubset = function(array, maxSubsetLength) {
var shuffled = arrayUtil.shuffle(array);
return shuffled.splice(0, maxSubsetLength);
};
/**
* Set a property in all elements in an array to a certain value.
* @param {Array} array Array to edit.
* @param {string} key Property to set in all elements.
* @param {Object} value A value to set to the property in all elements.
*/
arrayUtil.setPropertyInAll = function(array, key, value) {
for (var i = 0; i < array.length; ++i) {
array[i][key] = value;
}
};
/**
* Stable sort an array in place.
* @param {Array} array Array to sort.
* @param {function} compareFunction Function as in Array.prototype.sort.
* @return {Array} The sorted array.
*/
arrayUtil.stableSort = function(array, compareFunction) {
if (array.length < 2) {
return array;
}
var merge = function(left, right) {
var result = [];
var l = 0;
var r = 0;
while (l < left.length && r < right.length) {
if (compareFunction(left[l], right[r]) <= 0) {
result.push(left[l]);
++l;
} else {
result.push(right[r]);
++r;
}
}
result = result.concat(left.slice(l));
result = result.concat(right.slice(r));
return result;
};
var middle = Math.floor(array.length / 2);
var left = array.slice(0, middle);
var right = array.slice(middle);
arrayUtil.stableSort(left, compareFunction);
arrayUtil.stableSort(right, compareFunction);
var spliceParams = [0, array.length]; // First two parameters of splice()
var merged = merge(left, right);
spliceParams = spliceParams.concat(merged);
array.splice.apply(array, spliceParams);
return array;
};
/**
* @param {string} string Input string.
* @return {string} String with the first letter capitalized.
*/
stringUtil.capitalizeFirstLetter = function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
/**
* Initialize an object with default values.
* @param {Object} obj Object to set properties on.
* @param {Object} defaults Default properties. Every property needs to have a default value here.
* @param {Object} options Options to override the default properties.
*/
objectUtil.initWithDefaults = function(obj, defaults, options) {
for(var key in defaults) {
if (!options.hasOwnProperty(key)) {
obj[key] = defaults[key];
} else {
obj[key] = options[key];
}
}
};
/**
* Request fullscreen on a given element.
*/
var requestFullscreen = function(elem) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen();
}
};