code from amapei
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package tools;
|
||||
|
||||
/**
|
||||
* Some utility functions for arrays
|
||||
*/
|
||||
class ArrayTool
|
||||
{
|
||||
/**
|
||||
* shuffle (randomize) an array
|
||||
*/
|
||||
//public static function shuffle<T>(arr:Array<T>):Array<T>
|
||||
//{
|
||||
//if (arr!=null) {
|
||||
//for (i in 0...arr.length) {
|
||||
//var j = Std.random(arr.length);
|
||||
//var a = arr[i];
|
||||
//var b = arr[j];
|
||||
//arr[i] = b;
|
||||
//arr[j] = a;
|
||||
//}
|
||||
//}
|
||||
//return arr;
|
||||
//}
|
||||
|
||||
/**
|
||||
* Group a list of objects by date
|
||||
* @param objs List of objects
|
||||
* @param dateParamName Name of the object field which is a date
|
||||
* @return
|
||||
*/
|
||||
public static function groupByDate<T>(objs:Array<T>,dateFieldName:String):Map<String,Array<T>>{
|
||||
|
||||
var out = new Map<String, Array<T> >();
|
||||
for ( o in objs){
|
||||
var d : Date = Reflect.field(o, dateFieldName);
|
||||
var group = out.get(d.toString());
|
||||
if (group == null) group = new Array<T>();
|
||||
group.push(o);
|
||||
out.set(d.toString(), group);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
public static function mapLength<T>(m:Map<T,Dynamic>):Int{
|
||||
var i = 0;
|
||||
for (x in m) i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package tools;
|
||||
|
||||
/**
|
||||
* Date tool
|
||||
* @author fbarbut
|
||||
*/
|
||||
class DateTool
|
||||
{
|
||||
|
||||
public static function now():Date{
|
||||
return Date.now();
|
||||
}
|
||||
|
||||
public static function deltaDays(d:Date,n:Int):Date{
|
||||
return DateTools.delta(d, n * 1000 * 60 * 60 * 24.0);
|
||||
}
|
||||
|
||||
public static function setHourMinute(d:Date, hour:Int, minute:Int):Date{
|
||||
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), hour, minute, 0);
|
||||
}
|
||||
|
||||
public static function setDateMonth(d:Date, date:Int, month:Int):Date{
|
||||
return new Date(d.getFullYear(), month, date, d.getHours(), d.getMinutes(), 0);
|
||||
}
|
||||
|
||||
|
||||
public static function getLastHourRange(?now:Date){
|
||||
if(now==null) now = Date.now();
|
||||
var HOUR = 1000.0 * 60 * 60;
|
||||
var to = setHourMinute(now,now.getHours(),0);
|
||||
var from = DateTools.delta(to, -HOUR );
|
||||
return {from:from,to:to};
|
||||
}
|
||||
|
||||
public static function getLastMinuteRange(?now:Date){
|
||||
if(now==null) now = Date.now();
|
||||
var MIN = 1000.0 * 60;
|
||||
var to = setHourMinute(now,now.getHours(),now.getMinutes());
|
||||
var from = DateTools.delta(to, -MIN );
|
||||
return {from:from,to:to};
|
||||
}
|
||||
|
||||
public static function getWhichNthDayOfMonth(date:Date):Int {
|
||||
return Math.floor((date.getDate() - 1) / 7) + 1;
|
||||
}
|
||||
|
||||
public static function getNthDayOfMonth(year:Int,month:Int,dayOfWeek:Int,n:Int):Date {
|
||||
//dayOfWeek and getDay() follow the same value system: from 0 (Sunday) to 6 (Saturday)
|
||||
return deltaDays(new Date(year,month,1+7*n, 0, 0, 0), -new Date(year,month,8-(dayOfWeek+1), 0, 0, 0).getDay()-1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package tools;
|
||||
|
||||
|
||||
class FloatTool{
|
||||
|
||||
/**
|
||||
Tries to fix buddy float comparison in Neko...
|
||||
**/
|
||||
public static function isEqual(a:Float,b:Float){
|
||||
a = Math.round(a*10000);
|
||||
b = Math.round(b*10000);
|
||||
return a==b;
|
||||
}
|
||||
|
||||
public static function isInt(f:Float){
|
||||
return isEqual(f , Math.round(f) );
|
||||
}
|
||||
|
||||
//prevent float comparison bug in Neko
|
||||
public static function clean(f:Float){
|
||||
return Math.round(f*100)/100;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package tools;
|
||||
import Common.UserOrder;
|
||||
|
||||
/**
|
||||
* Utility to work on sys.db.Object lists
|
||||
* @author fbarbut
|
||||
*/
|
||||
class ObjectListTool
|
||||
{
|
||||
|
||||
/**
|
||||
* Get a list of IDs from an object list
|
||||
*/
|
||||
public static function getIds( objs:Iterable<sys.db.Object> ):Array<Int>{
|
||||
var out = new Array<Int>();
|
||||
for ( o in objs ) out.push(untyped o.id);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* deduplicate objects on IDs
|
||||
*/
|
||||
public static function deduplicate<T>(objs:Iterable<T>):Array<T>{
|
||||
var out = new Map<Int,T>();
|
||||
for ( u in objs) out.set( untyped u.id, u );
|
||||
return Lambda.array(out);
|
||||
}
|
||||
|
||||
|
||||
public static function toIdMap<T>(objs:Iterable<T>):Map<Int,T>{
|
||||
var out = new Map<Int,T>();
|
||||
for ( u in objs) out.set( untyped u.id, u );
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deduplicate user orders. (merge orders on same product from a same user)
|
||||
* @param orders
|
||||
* @return
|
||||
*/
|
||||
public static function deduplicateOrders(orders:Array<UserOrder>):Array<UserOrder>{
|
||||
|
||||
var out = new Map<String,UserOrder>();
|
||||
|
||||
for ( o in orders){
|
||||
|
||||
var key = o.userId + "-" + o.userId2 + "-" + o.productId;
|
||||
var x = out.get(key);
|
||||
if ( x == null){
|
||||
x = o;
|
||||
}else{
|
||||
//null safety
|
||||
if (x.fees == null) x.fees = 0;
|
||||
if (o.fees == null) o.fees = 0;
|
||||
|
||||
//merge
|
||||
x.quantity += o.quantity;
|
||||
x.fees += o.fees;
|
||||
x.subTotal += o.subTotal;
|
||||
x.total += o.total;
|
||||
}
|
||||
|
||||
out.set(key, x);
|
||||
}
|
||||
|
||||
return Lambda.array(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deduplicate distributions by key (date+placeId)
|
||||
* @param distribs
|
||||
*/
|
||||
public static function deduplicateDistribsByKey(distribs:Iterable<db.Distribution>){
|
||||
|
||||
var out = new Map<String,db.Distribution>();
|
||||
for ( d in distribs) out.set(d.getKey(), d);
|
||||
return Lambda.array(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Group distributions by key (date+placeId)
|
||||
* @param distribs
|
||||
*/
|
||||
public static function groupDistribsByKey(distribs:Iterable<db.Distribution>){
|
||||
|
||||
var out = new Map<String,Array<db.Distribution>>();
|
||||
for ( d in distribs) {
|
||||
|
||||
var v = out.get(d.getKey());
|
||||
if (v == null) v = [];
|
||||
v.push(d);
|
||||
out.set(d.getKey(), v);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Groupe orders by multidistrib key (date+placeId)
|
||||
*/
|
||||
public static function groupOrdersByKey(ucs:Iterable<db.UserContract>){
|
||||
|
||||
var out = new Map<String,Array<db.UserContract>>();
|
||||
for ( uc in ucs) {
|
||||
var k = uc.distribution.getKey();
|
||||
var v = out.get(k);
|
||||
if (v == null) v = [];
|
||||
v.push(uc);
|
||||
out.set(k, v);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Group distributions by group and day, order by day
|
||||
*/
|
||||
public static function groupDistributionsByGroupAndDay(dists:Iterable<db.Distribution>){
|
||||
|
||||
var out = new Map<String,Array<db.Distribution>>();
|
||||
for ( d in dists){
|
||||
|
||||
var k = d.date.toString().substr(0, 10) + "-" + d.contract.amap.id;
|
||||
|
||||
var x = out[k];
|
||||
if (x == null) x = [];
|
||||
x.push(d);
|
||||
out[k] = x;
|
||||
|
||||
}
|
||||
|
||||
//sort keys
|
||||
var keys = [];
|
||||
for ( k in out.keys()) keys.push(k);
|
||||
keys.sort(function(a, b){ if (a > b) return 1 else return -1; });
|
||||
|
||||
var out2 = [];
|
||||
for ( k in keys) out2.push(out[k]);
|
||||
|
||||
return out2;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package tools;
|
||||
|
||||
/**
|
||||
* ...
|
||||
* @author fbarbut
|
||||
*/
|
||||
class StockTool
|
||||
{
|
||||
|
||||
/**
|
||||
* Stock dispatching between groups.
|
||||
*
|
||||
* i.e we got 10kg of potatoes to dispatch with 3 groups.
|
||||
* I dont want to have 3,3333 kg for each group , but something like [3,3,4]
|
||||
*/
|
||||
public static function dispatch(stock:Int, groups:Int){
|
||||
|
||||
var out = [];
|
||||
|
||||
var modulo = stock % groups;
|
||||
|
||||
stock -= modulo;
|
||||
|
||||
var s = Math.round(stock / groups);
|
||||
|
||||
for ( i in 0...groups) out.push(s);
|
||||
|
||||
for (i in 0...modulo){
|
||||
out[i % out.length]++;
|
||||
}
|
||||
|
||||
return out;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user