UNPKG

39.5 kBHTMLView Raw
1<!DOCTYPE html>
2
3<html lang="en">
4<head>
5 <meta charset="utf-8">
6 <meta name="viewport" content="width=device-width">
7 <title>CrossBrowdy API documentation Source: CrossBase/general/CB_Arrays.js</title>
8
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
13
14 <link type="text/css" rel="stylesheet" href="styles/site.cosmo.css">
15
16</head>
17
18<body style="min-width:800px; overflow-wrap:break-word; word-wrap:break-word; word-break:break-word; line-break:strict; hyphens:none; -webkit-hyphens:none; -moz-hyphens:none;">
19
20<div class="navbar navbar-default navbar-fixed-top ">
21<div class="container">
22 <div class="navbar-header">
23 <a class="navbar-brand" href="index.html">CrossBrowdy API documentation</a>
24 <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
25 <span class="icon-bar"></span>
26 <span class="icon-bar"></span>
27 <span class="icon-bar"></span>
28 </button>
29 </div>
30 <div class="navbar-collapse collapse" id="topNavigation">
31 <ul class="nav navbar-nav">
32
33 <li class="dropdown">
34 <a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b class="caret"></b></a>
35 <ul class="dropdown-menu inline">
36 <li><a href="CB_Arrays.html">CB_Arrays</a></li><li><a href="CB_AudioDetector.html">CB_AudioDetector</a></li><li><a href="CB_Client.html">CB_Client</a></li><li><a href="CB_Collisions.html">CB_Collisions</a></li><li><a href="CB_Configuration.html">CB_Configuration</a></li><li><a href="CB_Configuration.CrossBase.html">CB_Configuration.CrossBase</a></li><li><a href="CB_Configuration.CrossBrowdy.html">CB_Configuration.CrossBrowdy</a></li><li><a href="CB_Controllers.html">CB_Controllers</a></li><li><a href="CB_Controllers_Proprietary.html">CB_Controllers_Proprietary</a></li><li><a href="CB_Controllers_Proprietary.WII.html">CB_Controllers_Proprietary.WII</a></li><li><a href="CB_Controllers_Proprietary.WII_U.html">CB_Controllers_Proprietary.WII_U</a></li><li><a href="CB_Device.html">CB_Device</a></li><li><a href="CB_Device.AmbientLight.html">CB_Device.AmbientLight</a></li><li><a href="CB_Device.Battery.html">CB_Device.Battery</a></li><li><a href="CB_Device.Location.html">CB_Device.Location</a></li><li><a href="CB_Device.Motion.html">CB_Device.Motion</a></li><li><a href="CB_Device.Orientation.html">CB_Device.Orientation</a></li><li><a href="CB_Device.Proximity.html">CB_Device.Proximity</a></li><li><a href="CB_Device.Vibration.html">CB_Device.Vibration</a></li><li><a href="CB_Elements.html">CB_Elements</a></li><li><a href="CB_Events.html">CB_Events</a></li><li><a href="CB_Keyboard.html">CB_Keyboard</a></li><li><a href="CB_Keyboard.chars.html">CB_Keyboard.chars</a></li><li><a href="CB_Keyboard.extended.html">CB_Keyboard.extended</a></li><li><a href="CB_Keyboard.keys.html">CB_Keyboard.keys</a></li><li><a href="CB_Modules.html">CB_Modules</a></li><li><a href="CB_Mouse.html">CB_Mouse</a></li><li><a href="CB_Mouse.CursorImage.html">CB_Mouse.CursorImage</a></li><li><a href="CB_Net.html">CB_Net</a></li><li><a href="CB_Net.Fetch.html">CB_Net.Fetch</a></li><li><a href="CB_Net.REST.html">CB_Net.REST</a></li><li><a href="CB_Net.Sockets.html">CB_Net.Sockets</a></li><li><a href="CB_Net.Sockets.SockJS.html">CB_Net.Sockets.SockJS</a></li><li><a href="CB_Net.XHR.html">CB_Net.XHR</a></li><li><a href="CB_Pointer.html">CB_Pointer</a></li><li><a href="CB_Screen.html">CB_Screen</a></li><li><a href="CB_Speaker.html">CB_Speaker</a></li><li><a href="CB_Touch.html">CB_Touch</a></li><li><a href="CB_baseSymbols.html">CB_baseSymbols</a></li>
37 </ul>
38 </li>
39
40 <li class="dropdown">
41 <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
42 <ul class="dropdown-menu inline">
43 <li><a href="CB_AudioFile.html">CB_AudioFile</a></li><li><a href="CB_AudioFileCache.html">CB_AudioFileCache</a></li><li><a href="CB_AudioFileSprites.html">CB_AudioFileSprites</a></li><li><a href="CB_AudioFileSpritesPool.html">CB_AudioFileSpritesPool</a></li><li><a href="CB_AudioFile_API.AAPI.html">CB_AudioFile_API.AAPI</a></li><li><a href="CB_AudioFile_API.ACMP.html">CB_AudioFile_API.ACMP</a></li><li><a href="CB_AudioFile_API.SM2.html">CB_AudioFile_API.SM2</a></li><li><a href="CB_AudioFile_API.WAAPI.html">CB_AudioFile_API.WAAPI</a></li><li><a href="CB_Canvas.html">CB_Canvas</a></li><li><a href="CB_GraphicSprites.html">CB_GraphicSprites</a></li><li><a href="CB_GraphicSpritesScene.html">CB_GraphicSpritesScene</a></li>
44 </ul>
45 </li>
46
47 <li class="dropdown">
48 <a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a>
49 <ul class="dropdown-menu inline">
50 <li><a href="global.html#CB_BASE_NAME">CB_BASE_NAME</a></li><li><a href="global.html#CB_CREDITS_DEFAULT">CB_CREDITS_DEFAULT</a></li><li><a href="global.html#CB_NAME">CB_NAME</a></li><li><a href="global.html#CB_OPTIONS">CB_OPTIONS</a></li><li><a href="global.html#CB_VERSION">CB_VERSION</a></li><li><a href="global.html#CB_addCredits">CB_addCredits</a></li><li><a href="global.html#CB_baseToBase">CB_baseToBase</a></li><li><a href="global.html#CB_baseToInt">CB_baseToInt</a></li><li><a href="global.html#CB_br2nl">CB_br2nl</a></li><li><a href="global.html#CB_brToNl">CB_brToNl</a></li><li><a href="global.html#CB_combineArraysOrObjects">CB_combineArraysOrObjects</a></li><li><a href="global.html#CB_combineAutomatically">CB_combineAutomatically</a></li><li><a href="global.html#CB_combineJSON">CB_combineJSON</a></li><li><a href="global.html#CB_combineURIParameters">CB_combineURIParameters</a></li><li><a href="global.html#CB_combineURLParameters">CB_combineURLParameters</a></li><li><a href="global.html#CB_console">CB_console</a></li><li><a href="global.html#CB_copyObject">CB_copyObject</a></li><li><a href="global.html#CB_countDecimalDigits">CB_countDecimalDigits</a></li><li><a href="global.html#CB_countDecimalPart">CB_countDecimalPart</a></li><li><a href="global.html#CB_countDecimals">CB_countDecimals</a></li><li><a href="global.html#CB_countIntegerDigits">CB_countIntegerDigits</a></li><li><a href="global.html#CB_countIntegerPart">CB_countIntegerPart</a></li><li><a href="global.html#CB_credits">CB_credits</a></li><li><a href="global.html#CB_forEach">CB_forEach</a></li><li><a href="global.html#CB_forceString">CB_forceString</a></li><li><a href="global.html#CB_getBase64StringObject">CB_getBase64StringObject</a></li><li><a href="global.html#CB_getCookie">CB_getCookie</a></li><li><a href="global.html#CB_getDatum">CB_getDatum</a></li><li><a href="global.html#CB_getJSONPropertyValue">CB_getJSONPropertyValue</a></li><li><a href="global.html#CB_getLZStringObject">CB_getLZStringObject</a></li><li><a href="global.html#CB_getValueIndex">CB_getValueIndex</a></li><li><a href="global.html#CB_getValuePath">CB_getValuePath</a></li><li><a href="global.html#CB_includeJSFile">CB_includeJSFile</a></li><li><a href="global.html#CB_indexOf">CB_indexOf</a></li><li><a href="global.html#CB_init">CB_init</a></li><li><a href="global.html#CB_intToBase">CB_intToBase</a></li><li><a href="global.html#CB_isArray">CB_isArray</a></li><li><a href="global.html#CB_isEmail">CB_isEmail</a></li><li><a href="global.html#CB_isFileLocal">CB_isFileLocal</a></li><li><a href="global.html#CB_isString">CB_isString</a></li><li><a href="global.html#CB_lastIndexOf">CB_lastIndexOf</a></li><li><a href="global.html#CB_ltrim">CB_ltrim</a></li><li><a href="global.html#CB_nl2br">CB_nl2br</a></li><li><a href="global.html#CB_nlToBr">CB_nlToBr</a></li><li><a href="global.html#CB_numberFormat">CB_numberFormat</a></li><li><a href="global.html#CB_numberOfDecimalDigits">CB_numberOfDecimalDigits</a></li><li><a href="global.html#CB_numberOfDecimals">CB_numberOfDecimals</a></li><li><a href="global.html#CB_numberOfIntegerDigits">CB_numberOfIntegerDigits</a></li><li><a href="global.html#CB_parseJSON">CB_parseJSON</a></li><li><a href="global.html#CB_parseString">CB_parseString</a></li><li><a href="global.html#CB_regularExpressionString">CB_regularExpressionString</a></li><li><a href="global.html#CB_renderString">CB_renderString</a></li><li><a href="global.html#CB_replaceAll">CB_replaceAll</a></li><li><a href="global.html#CB_rtrim">CB_rtrim</a></li><li><a href="global.html#CB_scriptPath">CB_scriptPath</a></li><li><a href="global.html#CB_scriptPathCalculate">CB_scriptPathCalculate</a></li><li><a href="global.html#CB_setCookie">CB_setCookie</a></li><li><a href="global.html#CB_setDatum">CB_setDatum</a></li><li><a href="global.html#CB_sizeOf">CB_sizeOf</a></li><li><a href="global.html#CB_sizeof">CB_sizeof</a></li><li><a href="global.html#CB_stringifyJSON">CB_stringifyJSON</a></li><li><a href="global.html#CB_symmetricCall">CB_symmetricCall</a></li><li><a href="global.html#CB_symmetricCallClear">CB_symmetricCallClear</a></li><li><a href="global.html#CB_this">CB_this</a></li><li><a href="global.html#CB_trim">CB_trim</a></li>
51 </ul>
52 </li>
53
54 </ul>
55
56 <div class="col-sm-3 col-md-3">
57 <form class="navbar-form" role="search">
58 <div class="input-group">
59 <input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
60 <div class="input-group-btn">
61 <button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
62 </div>
63 </div>
64 </form>
65 </div>
66
67 </div>
68
69</div>
70</div>
71
72
73<div class="container" id="toc-content" style="width:100%;">
74<div class="row" style="width:100%;">
75
76
77 <div class="col-md-12">
78
79 <div id="main">
80
81
82 <h1 class="page-title">Source: CrossBase/general/CB_Arrays.js</h1>
83
84<section>
85 <article>
86 <pre
87 class="sunlight-highlight-javascript linenums">/**
88 * @file Arrays management. Contains the {@link CB_Arrays} static class.
89 * @author Joan Alba Maldonado &lt;workindalian@gmail.com>
90 * @license Creative Commons Attribution 4.0 International. See more at {@link https://crossbrowdy.com/about#what_is_the_crossbrowdy_copyright_and_license}.
91 */
92
93
94/**
95 * Static class to manage arrays. It will return itself if it is tried to be instantiated.
96 * @namespace
97 * @borrows CB_lastIndexOf as lastIndexOf
98 * @borrows CB_indexOf as indexOf
99 * @borrows CB_forEach as forEach
100 * @borrows CB_isArray as isArray
101 * @borrows CB_sizeOf as sizeOf
102 * @borrows CB_replaceAll as replaceAll
103 * @borrows CB_trim as trim
104 * @borrows CB_rtrim as rtrim
105 * @borrows CB_ltrim as ltrim
106 * @borrows CB_combineArraysOrObjects as combine
107 */
108var CB_Arrays = function() { return CB_Arrays; };
109{
110 CB_Arrays.initialized = false; //It will tells whether the object has been initialized or not.
111
112
113 //Initializes all values:
114 CB_Arrays.init = function()
115 {
116 if (CB_Arrays.initialized) { return CB_Arrays; }
117
118 //The object has been initialized:
119 CB_Arrays.initialized = true;
120
121 //TODO.
122
123 return CB_Arrays;
124 }
125
126
127 CB_Arrays.lastIndexOf = function(array, searchElement, fromIndex)
128 {
129 return CB_lastIndexOf(array, searchElement, fromIndex, false);
130 }
131
132
133 //Returns the first index of a given element in an array (starting from an index if desired):
134 CB_Arrays.indexOf = function(array, searchElement, fromIndex)
135 {
136 return CB_indexOf(array, searchElement, fromIndex, false);
137 }
138
139
140 //Tells whether an variable given is an array or not:
141 CB_Arrays.isArray = function(variable)
142 {
143 return CB_isArray(variable, false);
144 }
145
146
147 /**
148 * Alias for {@link CB_Arrays.sizeOf}.
149 * @function CB_Arrays.sizeof
150 * @see {@link CB_Arrays.sizeOf}
151 */
152 /**
153 * Alias for {@link CB_Arrays.sizeOf}.
154 * @function CB_Arrays.count
155 * @see {@link CB_Arrays.sizeOf}
156 */
157 //Returns the size of an object given (does not need to be an array!):
158 CB_Arrays.sizeOf = CB_Arrays.sizeof = CB_Arrays.count = function(object, onlyOwn)
159 {
160 return CB_sizeof(object, onlyOwn);
161 }
162
163
164 //Returns the given array of strings with the desired occurrences replaced. Calls itself recursively and calls the CB_replaceAll function internally.
165 CB_Arrays.replaceAll = function(stringOrArray, stringOrArrayFind, stringReplace, caseInsensitive) //Either "stringOrArray" or "stringOrArrayFind" can also be an array of arrays of strings (as many levels as you wish).
166 {
167 return CB_replaceAll(stringOrArray, stringOrArrayFind, stringReplace, caseInsensitive);
168 }
169
170
171 //Trims all the strings that contains a given array (undesired strings can be set or otherwise will be spaces, etc.):
172 CB_Arrays.trim = function(array, undesiredStrings)
173 {
174 return CB_trim(array, undesiredStrings);
175 }
176
177
178 //Trims all the strings from the right side that contains a given array (undesired strings can be set or otherwise will be spaces, etc.):
179 CB_Arrays.rtrim = function(array, undesiredStrings)
180 {
181 return CB_rtrim(array, undesiredStrings);
182 }
183
184
185 //Trims all the strings from the left side that contains a given array (undesired strings can be set or otherwise will be spaces, etc.):
186 CB_Arrays.ltrim = function(array, undesiredStrings)
187 {
188 return CB_ltrim(array, undesiredStrings);
189 }
190
191
192 //Returns a combined array or object from two arrays or objects:
193 CB_Arrays.combine = function(arrayOrObjectA, arrayOrObjectB, avoidDuplicatedValuesInArray)
194 {
195 return CB_combineArraysOrObjects(arrayOrObjectA, arrayOrObjectB, avoidDuplicatedValuesInArray);
196 }
197
198
199 //Performs a desired function for each element of a given array (changing the value of "this" if desired):
200 CB_Arrays.forEach = function(array, fun, thisObject)
201 {
202 return CB_forEach(array, fun, thisObject, false);
203 }
204
205
206 /**
207 * Callback that is used for each iteration when looping the array. Being "this" the value itself.
208 * @memberof CB_Arrays
209 * @callback CB_Arrays.executeFunctionAll_ON_LOOP_CALLBACK
210 * @param {*} item - Element (item) which belongs to the index which is being checked in the current iteration of the given array.
211 * @param {integer} index - Index which is being checked in the current iteration.
212 * @param {array} array - Whole array which is being looped.
213 * @param {integer} delay - The "delayBetweenEach" used for this loop.
214 * @returns {number} When used as a function to calculate the delay, it should return the delay desired as a number.
215 */
216 /**
217 * Callback that is used when finishes all iterations after looping the array. Being "this" the array itself.
218 * @memberof CB_Arrays
219 * @callback CB_Arrays.executeFunctionAll_ON_FINISH_CALLBACK
220 * @param {array} array - Whole array which was being looped.
221 * @param {integer} itemsAffected - The number of times that the "functionEach" callback was called (the most likely, matches the number of elements unless they are undefined or null).
222 * @param {integer} delayMaximum - The maximum "delay" used.
223 */
224 /**
225 * Object used by the {@link CB_Arrays#executeFunctionAll} method when the "returnSetTimeoutsArray" parameter is set to true.
226 * @memberof CB_Arrays
227 * @typedef {Object} CB_Arrays.executeFunctionAll_OBJECT
228 * @property {*} item - The element affected.
229 * @property {integer} setTimeoutReturningValue - The returning value of calling the [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} internally or null if it was not called, depending on the "delayBetweenEach" parameter.
230 * @property {number} setTimeoutDelay - The value used as the second parameter when calling the [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} internally or zero if it was not called, depending on the "delayBetweenEach" parameter.
231 */
232
233 /**
234 * Alias for {@link CB_Arrays#executeFunctionAll}.
235 * @function CB_Arrays#executeAll
236 * @see {@link CB_Arrays#executeFunctionAll}
237 */
238 /**
239 * Alias for {@link CB_Arrays#executeFunctionAll}.
240 * @function CB_Arrays#forEachDelay
241 * @see {@link CB_Arrays#executeFunctionAll}
242 */
243 /**
244 * Performs a desired action, using the provided function, on all the existing elements of a given array. Elements which are undefined or null will be skipped without calling the "functionEach" function.
245 * @function
246 * @param {array} array - A numeric array containing the elements that we want to loop.
247 * @param {CB_Arrays.executeFunctionAll_ON_LOOP_CALLBACK} functionEach - Function that will be called for each element of the given array. As the first parameter it receives the element of the "array" provided being looped, as the second parameter the position of this element in the "array" provided, the third parameter is the array being looped and the fourth parameter will be the "delayBetweenEach" being used, being "this" the element itself.
248 * @param {number|CB_Arrays.executeFunctionAll_ON_LOOP_CALLBACK} [delayBetweenEach=0] - If a value greater than zero is used, it will be used as the delay desired between each call to the "functionEach" function (calling them using the [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} function internally). If not provided or the value is 0 (zero) or lower, each call to the "functionEach" function will be performed immediately one after the other. If a function is provided, it will be called with the same parameters as the "functionEach" function and its returning value will be used as the delay (executed every loop for each item).
249 * @param {boolean} [returnSetTimeoutsArray=false] - Defines whether we want the method to return an integer or a numeric array with information of each [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} call. Returning an array with information of each [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} call is only useful when the [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} function is called internally, which happens when the "delayBetweenEach" parameter is greater than 0 (zero).
250 * @param {boolean} [delayBetweenEachAffectsFirst=false] - If set to true, the desired delay (if any) will also affect the first call to the "functionEach" function.
251 * @param {CB_Arrays.executeFunctionAll_ON_FINISH_CALLBACK} [functionFinish] - Function that will be called for when it has finished looping all the items. The first parameter will be the array which was looped, the second parameter will be the number of times that the "functionEach" callback was called (the most likely, matches the number of elements unless they are undefined or null), and the third parameter will be the maximum "delay" used, being "this" the array itself.
252 * @returns {integer|array} If the "returnSetTimeoutsArray" parameter is set to false, it will return the number of calls to the "functionEach" function that were performed (which should be the same number as the elements given in the "array" parameter). Otherwise, if the "returnSetTimeoutsArray" is set to true, it will return a numeric array with a {@link CB_Arrays.executeFunctionAll_OBJECT} object for each element given. The length of this array will also be the number of calls to the "functionEach" function that were performed. Note that if a value greater than 0 (zero) for the "delayBetweenEach" parameter has been provided, perhaps not all calls of the "functionEach" function will have been performed yet when exiting this method because of the asynchronous nature of the [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} function.
253 */
254 CB_Arrays.executeFunctionAll = CB_Arrays.executeAll = CB_Arrays.forEachDelay = function(array, functionEach, delayBetweenEach, returnSetTimeoutsArray, delayBetweenEachAffectsFirst, functionFinish)
255 {
256 if (typeof(functionEach) !== "function" || !CB_isArray(array))
257 {
258 if (typeof(functionFinish) === "function") { functionFinish.call(array, array, 0, 0); }
259 return returnSetTimeoutsArray ? [] : 0;
260 }
261 if (typeof(delayBetweenEach) !== "function" &amp;&amp; (typeof(delayBetweenEach) === "undefined" || delayBetweenEach === null || isNaN(delayBetweenEach) || delayBetweenEach &lt; 0))
262 {
263 delayBetweenEach = 0;
264 }
265
266 var setTimeoutsInformation = [];
267
268 var arrayLength = array.length;
269 var y = 0;
270 var setTimeoutReturningValue = null;
271 var setTimeoutDelay = 0;
272 var delay;
273 var setTimeoutDelayMax = 0;
274 for (var x = 0; x &lt; arrayLength; x++)
275 {
276 //If the object exists:
277 if (typeof(array[x]) !== "undefined" &amp;&amp; array[x] !== null)
278 {
279 delay = typeof(delayBetweenEach) === "function" ? delayBetweenEach() : delayBetweenEach;
280 delay = parseInt(delay) || 0;
281 //Executes the given function ("this" parameters will point to the current object):
282 if (delay === 0)
283 {
284 functionEach.call(array[x], array[x], x, array, delay); //"x" is the position of the object in the array.
285 }
286 else
287 {
288 new function(x, delay) //Closure to get unique value of "x" variable for each loop.
289 {
290 setTimeoutDelay = delayBetweenEachAffectsFirst ? delay * (y + 1) : delay * y;
291 setTimeoutReturningValue = setTimeout
292 (
293 function()
294 {
295 functionEach.call(array[x], array[x], x, array, delay); //"x" is the position of the object in the array.
296 },
297 setTimeoutDelay
298 );
299 setTimeoutDelayMax = setTimeoutDelay > setTimeoutDelayMax ? setTimeoutDelay : setTimeoutDelayMax;
300 }(x, delay);
301 }
302 setTimeoutsInformation[setTimeoutsInformation.length] = { "item" : array[x], "setTimeoutReturningValue" : setTimeoutReturningValue, "setTimeoutDelay" : setTimeoutDelay };
303 y++;
304 }
305 }
306
307 if (typeof(functionFinish) === "function")
308 {
309 if (setTimeoutDelayMax === 0) { functionFinish.call(array, array, y, setTimeoutDelayMax); }
310 else { setTimeout(function() { functionFinish.call(array, array, y, setTimeoutDelayMax); }, setTimeoutDelayMax + 1); }
311 }
312
313 if (returnSetTimeoutsArray) { return setTimeoutsInformation; }
314 else { return y; }
315 }
316
317
318 /**
319 * Returns an array copied from the given one. It will also make a copy of the arrays found in the values (if any), calling itself recursively.
320 * @function
321 * @param {array} array - The array whose values we want to copy.
322 * @returns {object} Returns an array copied from the given one. Returns an empty array if the given "array" was not an array.
323 */
324 CB_Arrays.copy = function(array)
325 {
326 if (!CB_isArray(array)) { return []; }
327 //Removes possible duplicated values or not desired:
328 var newArray = [];
329 for (var x = array.length - 1; x >= 0; x--)
330 {
331 newArray[x] = CB_isArray(array[x]) ? CB_Arrays.copy(array[x]) : array[x];
332 }
333 return newArray;
334 }
335
336
337 /**
338 * Alias for {@link CB_Arrays.insertElement}.
339 * @function CB_Arrays.insert
340 * @see {@link CB_Arrays.insertElement}
341 */
342 /**
343 * Alias for {@link CB_Arrays.insertElement}.
344 * @function CB_Arrays.insertElementByPosition
345 * @see {@link CB_Arrays.insertElement}
346 */
347 /**
348 * Inserts an element in the desired position of a given an array. Elements which are placed after it will be moved a position to the right (increasing their index).
349 * @function
350 * @param {array} array - The array whose element we want to delete.
351 * @param {integer} [index=0] - Position of the element in the given array that we want to remove.
352 * @param {*} item - Element (item) which belongs to the index which is being checked in the current iteration of the given array.
353 * @param {CB_Arrays.removeDuplicated_PURGE_FUNCTION} [onInsert] - Function to call if the element is inserted, after inserting it.
354 * @returns {array} Returns the new array (with the element inserted if it was possible). If no valid array is given, it will return an empty array.
355 */
356 CB_Arrays.insertElement = CB_Arrays.insertElementByPosition = CB_Arrays.insert = function(array, index, element, onInsert)
357 {
358 index = parseInt(index);
359 index = isNaN(index) ? 0 : index;
360 if (!CB_isArray(array)) { return []; }
361 if (index &lt; 0) { index *= -1; } //It must be a positive integer.
362 if (index > array.length) { index = array.length; }
363 array.splice(index, 0, element);
364 if (typeof(onInsert) === "function") { onInsert.call(element, element, index, array); }
365 return array;
366 }
367
368
369 /**
370 * Callback that is used as the "purgeFunction" parameter of the {@link CB_Arrays.removeDuplicated} function. Being "this" the current element (item). It should return false when we want to keep the value or true otherwise.
371 * @memberof CB_Arrays
372 * @callback CB_Arrays.removeDuplicated_PURGE_FUNCTION
373 * @param {*} item - Element (item) which belongs to the index which is being checked in the current iteration used in {@link CB_Arrays.removeDuplicated}.
374 * @param {integer} index - Index which is being checked in the current iteration used in {@link CB_Arrays.removeDuplicated}.
375 * @param {array} array - Whole array which is being checked.
376 * @returns {boolean} It should return false when we want to keep the value or true otherwise.
377 */
378
379 /**
380 * Deletes duplicated and/or not desired values (with a checking function to purge) from a numeric array. Values can be of any type. Internally, loops through the given array backwards (from the last index to the first one).
381 * @function
382 * @param {array} array - The array whose values we want to purge.
383 * @param {CB_Arrays.removeDuplicated_PURGE_FUNCTION} [purgeFunction] - Callback that will be called for each item, being "this" the current item. It should return false when we want to keep the value or true otherwise. If the "ignoreDuplicated" parameter is set to true, all duplicated elements will be removed regardless of the returning value of the "purgeFunction" function.
384 * @param {boolean} [ignoreDuplicated=false] - If it is set to true, it will keep duplicated values (unless the given "purgeFunction" purge them).
385 * @returns {array} Returns the array purged. If no valid array is given, it will return an empty array.
386 */
387 CB_Arrays.removeDuplicated = function(array, purgeFunction, ignoreDuplicated)
388 {
389 if (!CB_isArray(array)) { return []; }
390 //Removes possible duplicated values or not desired:
391 var keysDuplicatedChecker = [];
392 for (var x = array.length - 1; x >= 0; x--)
393 {
394 //if (typeof(purgeFunction) === "function" &amp;&amp; purgeFunction(array[x]) || (typeof(purgeFunction) !== "function" || !ignoreDuplicated) &amp;&amp; CB_indexOf(keysDuplicatedChecker, array[x]) !== -1)
395 if (typeof(purgeFunction) === "function" &amp;&amp; purgeFunction.call(array[x], array[x], x, array) || !ignoreDuplicated &amp;&amp; CB_indexOf(keysDuplicatedChecker, array[x]) !== -1)
396 {
397 array[x] = undefined;
398 array.splice(x, 1);
399 continue;
400 }
401 keysDuplicatedChecker[keysDuplicatedChecker.length] = array[x];
402 }
403 return array;
404 }
405
406
407 /**
408 * Alias for {@link CB_Arrays.removeElementByPosition}.
409 * @function CB_Arrays.removeElementByIndex
410 * @see {@link CB_Arrays.removeElementByPosition}
411 */
412 /**
413 * Deletes an element from an array which is placed in the desired position. Elements which were after it will be moved a position to the left (decreasing their index).
414 * @function
415 * @param {array} array - The array whose element we want to delete.
416 * @param {integer} [index=0] - Position of the element in the given array that we want to remove.
417 * @param {CB_Arrays.removeDuplicated_PURGE_FUNCTION} [onRemove] - Function to call if the element is removed, before removing it.
418 * @returns {array} Returns the new array (with the element removed if it was possible). If no valid array is given, it will return an empty array.
419 */
420 CB_Arrays.removeElementByPosition = CB_Arrays.removeElementByIndex = function(array, index, onRemove)
421 {
422 index = parseInt(index);
423 index = isNaN(index) ? 0 : index;
424 if (index &lt; 0) { index *= -1; } //It must be a positive integer.
425 return CB_Arrays.removeDuplicated(array, function(value, position, array) { if (position === index) { if (typeof(onRemove) === "function") { onRemove.call(value, value, position, array); } return true; } return false; }, true);
426 }
427
428
429 /**
430 * Deletes a given element from an array. All occurrences will be deleted. Elements which were after a removed element will be moved a position to the left (decreasing their index).
431 * @function
432 * @param {array} array - The array whose element we want to delete.
433 * @param {*} [element=undefined] - The element we want to remove. All occurrences will be deleted. Note that it is type sensitive.
434 * @param {CB_Arrays.removeDuplicated_PURGE_FUNCTION} [onRemove] - Function to call if the element is removed, before removing it.
435 * @returns {array} Returns the new array (with the element removed if it was possible). If no valid array is given, it will return an empty array.
436 */
437 CB_Arrays.removeElement = function(array, element)
438 {
439 return CB_Arrays.removeDuplicated(array, function(value, position, array) { if (value === element) { if (typeof(onRemove) === "function") { onRemove.call(value, value, position, array); } return true; } return false; }, true);
440 }
441
442
443 /**
444 * Deletes the given elements from an array. All occurrences will be deleted. Elements which were after a removed element will be moved a position to the left (decreasing their index).
445 * @function
446 * @param {array} array - The array whose element we want to delete.
447 * @param {array} elements - An array with the elements we want to remove. All occurrences will be deleted. Note that it is type sensitive.
448 * @returns {array} Returns the new array (with the element removed if it was possible). If no valid array is given, it will return an empty array.
449 */
450 CB_Arrays.removeElements = function(array, elements)
451 {
452 if (!CB_isArray(elements)) { elements = []; }
453 return CB_Arrays.removeDuplicated(array, function(value, position, array) { return CB_Arrays.lastIndexOf(elements, value, position) !== -1; }, true);
454 }
455
456
457
458 /**
459 * Sorts the values of an array (using the native [Array.sort]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort} method).
460 * @function
461 * @param {array} array - The array whose elements we want to sort.
462 * @param {boolean} [reversed=false] - Defines whether to sort in the reverse order or not. Only applies when comparingFunction is not provided.
463 * @param {function} [comparingFunction] - Comparing function with the same rules as the native [Array.sort]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort} method. If provided, the "reversed" parameter will be ignored.
464 * @returns {array} Returns the array ordered. If another value which is not an array is given, it will be returned again.
465 */
466 CB_Arrays.sort = function(array, reversed, comparingFunction)
467 {
468 if (typeof(array) === "undefined" || array === null || !CB_Arrays.isArray(array)) { return array; }
469 if (typeof(comparingFunction) !== "function")
470 {
471 if (reversed) { comparingFunction = function(a, b) { return b-a; }; }
472 else { comparingFunction = function(a, b) { return a-b; }; }
473 }
474 return array.sort(comparingFunction);
475 }
476
477
478 /**
479 * Sorts an array using the [bubble sort (sinking sort) method]{@link https://en.wikipedia.org/wiki/Bubble_sort}. Internally, it uses the ">" operator for comparing values as they will be treated as numbers.
480 * @function
481 * @param {array} array - The array whose elements we want to sort.
482 * @param {boolean} [reversed=false] - Defines whether to sort in the reverse order or not.
483 * @todo Think about accepting a comparing function (as the "sort" method).
484 * @returns {array} Returns the array ordered. If another value which is not an array is given, it will be returned again.
485 */
486 CB_Arrays.bsort = function(array, reversed, comparingFunction) //NOTE: think about accepting a comparing function.
487 {
488 if (typeof(array) === "undefined" || array === null || !CB_Arrays.isArray(array)) { return array; }
489 var arrayLength = array.length;
490 for (var x = 1; x &lt; arrayLength; x++)
491 {
492 for (var y = 0; y &lt; arrayLength-x; y++)
493 {
494 if (!reversed &amp;&amp; array[y] > array[y+1] || reversed &amp;&amp; array[y] &lt; array[y+1])
495 {
496 var aux = array[y];
497 array[y] = array[y+1];
498 array[y+1] = aux;
499 }
500 }
501 }
502 return array;
503 }
504
505
506 //Sorts an array using merge sort method:
507 CB_Arrays.msort = function(array, reversed, comparingFunction) //NOTE: think about accepting a comparing function.
508 {
509 if (typeof(array) === "undefined" || array === null || !CB_Arrays.isArray(array)) { return array; }
510
511 //TODO.
512
513 array = CB_Arrays.sort(array, reversed, comparingFunction); //DELETE THIS!
514
515 return array;
516 }
517
518
519 //Sorts an array using quick sort method:
520 CB_Arrays.qsort = function(array, reversed, comparingFunction) //NOTE: think about accepting a comparing function.
521 {
522 if (typeof(array) === "undefined" || array === null || !CB_Arrays.isArray(array)) { return array; }
523
524 //TODO.
525
526 array = CB_Arrays.sort(array, reversed, comparingFunction); //DELETE THIS!
527
528 return array;
529 }
530
531
532 //Sorts an array using selection sort method:
533 CB_Arrays.ssort = function(array, reversed, comparingFunction) //NOTE: think about accepting a comparing function.
534 {
535 if (typeof(array) === "undefined" || array === null || !CB_Arrays.isArray(array)) { return array; }
536
537 //TODO.
538
539 array = CB_Arrays.sort(array, reversed, comparingFunction); //DELETE THIS!
540
541 return array;
542 }
543
544
545 //Sorts an array using insertion sort method:
546 CB_Arrays.isort = function(array, reversed, comparingFunction) //NOTE: think about accepting a comparing function.
547 {
548 if (typeof(array) === "undefined" || array === null || !CB_Arrays.isArray(array)) { return array; }
549
550 //TODO.
551
552 array = CB_Arrays.sort(array, reversed, comparingFunction); //DELETE THIS!
553
554 return array;
555 }
556
557
558 //Sorts an array using cocktail sort (bidirectional bubble) method:
559 CB_Arrays.csort = function(array, reversed, comparingFunction) //NOTE: think about accepting a comparing function.
560 {
561 if (typeof(array) === "undefined" || array === null || !CB_Arrays.isArray(array)) { return array; }
562
563 //TODO.
564
565 array = CB_Arrays.sort(array, reversed, comparingFunction); //DELETE THIS!
566
567 return array;
568 }
569
570
571}</pre>
572 </article>
573</section>
574
575
576
577
578
579 </div>
580 </div>
581
582 <div class="clearfix"></div>
583
584
585
586</div>
587</div>
588
589
590 <div class="modal fade" id="searchResults">
591 <div class="modal-dialog">
592 <div class="modal-content">
593 <div class="modal-header">
594 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
595 <h4 class="modal-title">Search results</h4>
596 </div>
597 <div class="modal-body"></div>
598 <div class="modal-footer">
599 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
600 </div>
601 </div><!-- /.modal-content -->
602 </div><!-- /.modal-dialog -->
603 </div>
604
605
606<footer>
607
608
609 <span class="copyright">
610 <a href="printable/" target="_blank">See a more printer-friendly version</a><hr /><span style="color:#000000">© <address style="display:inline; font-style:normal;"><a href="https://crossbrowdy.com/" target="_blank">CrossBrowdy</a> API documentation</address> by <a href="https://joanalbamaldonado.com/" target="_blank">Joan Alba Maldonado</a> - <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank">Creative Commons Attribution 4.0 International</a><br />DocStrap Copyright © 2012-2015 The contributors to the JSDoc3 and DocStrap projects.</span>
611 </span>
612
613<span class="jsdoc-message">
614 Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a>
615
616 on Wed Mar 22nd 2023
617
618 using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
619</span>
620</footer>
621
622<script src="scripts/docstrap.lib.js"></script>
623<script src="scripts/toc.js"></script>
624
625 <script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>
626
627
628<script>
629$( function () {
630 $( "[id*='$']" ).each( function () {
631 var $this = $( this );
632
633 $this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
634 } );
635
636 $( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
637 var $this = $( this );
638
639 var example = $this.find( "code" );
640 exampleText = example.html();
641 var lang = /{@lang (.*?)}/.exec( exampleText );
642 if ( lang && lang[1] ) {
643 exampleText = exampleText.replace( lang[0], "" );
644 example.html( exampleText );
645 lang = lang[1];
646 } else {
647 var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
648 lang = langClassMatch ? langClassMatch[1] : "javascript";
649 }
650
651 if ( lang ) {
652
653 $this
654 .addClass( "sunlight-highlight-" + lang )
655 .addClass( "linenums" )
656 .html( example.html() );
657
658 }
659 } );
660
661 Sunlight.highlightAll( {
662 lineNumbers : true,
663 showMenu : true,
664 enableDoclinks : true
665 } );
666
667 $.catchAnchorLinks( {
668 navbarOffset: 10
669 } );
670 $( "#toc" ).toc( {
671 anchorName : function ( i, heading, prefix ) {
672 return $( heading ).attr( "id" ) || ( prefix + i );
673 },
674 selectors : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
675 showAndHide : false,
676 smoothScrolling: true
677 } );
678
679 $( "#main span[id^='toc']" ).addClass( "toc-shim" );
680 $( '.dropdown-toggle' ).dropdown();
681
682 $( "table" ).each( function () {
683 var $this = $( this );
684 $this.addClass('table');
685 } );
686
687} );
688</script>
689
690
691
692<!--Navigation and Symbol Display-->
693
694<script>
695 $( function () {
696 $( '#main' ).localScroll( {
697 offset : { top : 60 } //offset by the height of your header (give or take a few px, see what works for you)
698 } );
699 $( "dt.name" ).each( function () {
700 var $this = $( this ).find("h4");
701 var icon = $( "<i/>" ).addClass( "icon-plus-sign" ).addClass( "pull-right" ).addClass( "icon-white" );
702 var dt = $(this);
703 var children = dt.next( "dd" );
704
705 dt.prepend( icon ).css( {cursor : "pointer"} );
706 dt.addClass( "member-collapsed" ).addClass( "member" );
707
708
709 children.hide();
710
711 dt.children().on( "click", function () {
712 children = dt.next( "dd" );
713 children.slideToggle( "fast", function () {
714
715 if ( children.is( ":visible" ) ) {
716 icon.addClass( "icon-minus-sign" ).removeClass( "icon-plus-sign" ).removeClass( "icon-white" );
717 dt.addClass( "member-open" ).animate( "member-collapsed" );
718 } else {
719 icon.addClass( "icon-plus-sign" ).removeClass( "icon-minus-sign" ).addClass( "icon-white" );
720 dt.addClass( "member-collapsed" ).removeClass( "member-open" );
721 }
722 } );
723 } );
724
725 } );
726 } );
727</script>
728
729
730<!--Google Analytics-->
731
732
733
734 <script type="text/javascript">
735 $(document).ready(function() {
736 SearcherDisplay.init();
737 });
738 </script>
739
740
741</body>
742</html>