84 lines
2.4 KiB
Haxe
84 lines
2.4 KiB
Haxe
package izzcomlib;
|
|
using StringTools;
|
|
|
|
class IzzComLib {
|
|
/**
|
|
* Returns a random integer between x and y, both inclusive.
|
|
* Correctly handles whether parameters are given low-to-high or high-to-low.
|
|
* If parameters are equal, returns x.
|
|
*
|
|
* @param x The lower value.
|
|
* @param y The upper value.
|
|
*/
|
|
public static function randInt(x, y) {
|
|
if (x < y) {
|
|
return Std.random((y+1)-x)+x;
|
|
} else if (y < x) {
|
|
return Std.random((x+1)-y)+y;
|
|
} else {
|
|
return x;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns a random integer between x and y, both inclusive.
|
|
* Will not return a result that matches a value in the `exclude` array.
|
|
*
|
|
* ⚠️ Does not protect against excluding all possible results! The function will loop endlessly! ⚠️
|
|
*
|
|
* @param x The lower value.
|
|
* @param y The upper value.
|
|
* @param exclude Array of values that shouldn't be chosen.
|
|
*/
|
|
public static function randIntExclude(x, y, exclude:Array<Int>) {
|
|
if (x == y) {
|
|
return x; // Very minimal mistake protection
|
|
} else {
|
|
var result:Int = 0;
|
|
|
|
while (true) {
|
|
result = randInt(x, y);
|
|
if (!exclude.contains(result)) {break;}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the current time in 24hr notation, with left-padding (HH:MM).
|
|
*/
|
|
public static function timestamp() {
|
|
return '${StringTools.lpad(Std.string(Date.now().getHours()), "0", 2)}:${StringTools.lpad(Std.string(Date.now().getMinutes()), "0", 2)}';
|
|
}
|
|
|
|
/**
|
|
* Returns the current date, formatted to ISO 8601 (YYYY-MM-DD).
|
|
*/
|
|
public static function datestamp() {
|
|
return '${Date.now().getFullYear()}-${StringTools.lpad(Std.string(Date.now().getMonth()+1), "0", 2)}-${StringTools.lpad(Std.string(Date.now().getDate()), "0", 2)}';
|
|
}
|
|
|
|
/**
|
|
* Returns the name of the current day of the week.
|
|
*/
|
|
public static function printDay() {
|
|
var weekdays:Array<String> = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
|
|
return weekdays[Date.now().getDay()];
|
|
}
|
|
|
|
/**
|
|
* Returns the name of the current month.
|
|
*/
|
|
public static function printMonth() {
|
|
var months:Array<String> = ["January","February","March","April","May","June","July","August","September","October","November","December"];
|
|
return months[Date.now().getMonth()];
|
|
}
|
|
|
|
/**
|
|
* Returns the current day of the month, with left padding.
|
|
*/
|
|
public static function printDate() {
|
|
return StringTools.lpad(Std.string(Date.now().getDate()), "0", 2);
|
|
}
|
|
} |