Last active
December 10, 2015 05:28
-
-
Save honzabrecka/4387881 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ArrayUtil | |
* @author Jan Břečka | |
* @langversion 3.0 | |
*/ | |
/** | |
* Clones an array. | |
*/ | |
function clone(a:Array):Array | |
{ | |
var t:Array = []; | |
for (var i:uint = 0; i < a.length; i++) | |
t[i] = a[i]; | |
return t; | |
} | |
/** | |
* Returns a random item. | |
*/ | |
function randomItem(a:Array):* | |
{ | |
return a[Math.floor(Math.random() * a.length)]; | |
} | |
/** | |
* Checks if is item in array. When yes returns true else false. | |
*/ | |
function isIn(a:Array, t:*):Boolean | |
{ | |
for (var i:uint = 0; i < a.length; i++) | |
{ | |
if (a[i] == t) | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Shuffle items. | |
*/ | |
function shuffle(a:Array):Array | |
{ | |
var b:Array = clone(a); | |
var temp:Array = []; | |
while (b.length > 0) | |
temp.push(b.splice(Math.round(Math.random() * (b.length - 1)), 1)[0]); | |
return temp; | |
} | |
/** | |
* Removes duplicate items. | |
*/ | |
function unique(a:Array):Array | |
{ | |
var temp:Array = []; | |
var i:uint; | |
for (i = 0; i < a.length; i++) | |
{ | |
if (!isIn(temp, a[i])) | |
temp.push( a[i] ); | |
} | |
return temp; | |
} | |
/** | |
* Remove item from array. | |
* | |
* @param multi Use on every presence or only at first. | |
*/ | |
function removeItem(a:Array, t:*, multi:Boolean=false):Array | |
{ | |
var temp:Array = clone( a ); | |
for (var i:uint = 0; i < temp.length; i++) | |
{ | |
if (temp[i] == t) | |
{ | |
temp.splice(i, 1); | |
if (!multi) | |
break; | |
} | |
} | |
return temp; | |
} | |
/** | |
* Every item recalculates by user function. | |
*/ | |
function collect(a:Array, t:Function):Array | |
{ | |
var temp:Array = []; | |
for each (var it:* in a) | |
{ | |
if (!(it is Function)) | |
temp.push( t(it) ); | |
} | |
return temp; | |
} | |
/** | |
* Join items with a string. | |
*/ | |
function implode(a:Array, glue:String):String | |
{ | |
var res:String = ""; | |
for (var i:uint = 0; i < a.length; i++) | |
{ | |
res += String(a[i]); | |
if (i != a.length - 1) | |
res += glue; | |
} | |
return res; | |
} | |
//------------------------- | |
// Math functions | |
function sum(a:Array):Number | |
{ | |
var res:Number = 0; | |
for (var i:uint = 0; i < a.length; i++) | |
{ | |
if (a[i] is Number) | |
res += a[i]; | |
} | |
return res; | |
} | |
function max(a:Array):Number | |
{ | |
var res:Number = -Number.MAX_VALUE; | |
for (var i:uint = 0; i < a.length; i++) | |
{ | |
if (a[i] > res) | |
res = a[i]; | |
} | |
return res; | |
} | |
function min(a:Array):Number | |
{ | |
var res:Number = Number.MAX_VALUE; | |
for (var i:uint = 0; i < a.length; i++) | |
{ | |
if (a[i] < res) | |
res = a[i]; | |
} | |
return res; | |
} | |
function average(a:Array):Number | |
{ | |
var res:Number = 0; | |
for (var i:uint = 0; i < a.length; i++) | |
{ | |
if (a[i] is Number) | |
res += a[i]; | |
} | |
return res / a.length; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* DateUtil | |
* @author Jan Břečka | |
* @langversion 3.0 | |
*/ | |
function plus(time:uint):Date | |
{ | |
var d:Date = new Date(); | |
d.setTime( d.getTime() + time ); | |
return d; | |
} | |
function minus(time:uint):Date | |
{ | |
var d:Date = new Date(); | |
d.setTime( d.getTime() - time ); | |
return d; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* NumberUtil | |
* @author Jan Břečka | |
* @langversion 3.0 | |
*/ | |
function randomInRange(from:uint, to:uint):uint | |
{ | |
return Math.round( Math.random() * (from - to) + to ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment