2022-06-03 20:30:25 +02:00
|
|
|
package io.gitlab.jfronny.muscript;
|
|
|
|
|
2022-06-29 15:41:44 +02:00
|
|
|
import io.gitlab.jfronny.commons.data.dynamic.DList;
|
|
|
|
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
|
2022-06-03 20:30:25 +02:00
|
|
|
|
2022-06-29 15:41:44 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Random;
|
2022-06-03 20:30:25 +02:00
|
|
|
|
2022-11-24 19:05:51 +01:00
|
|
|
import static io.gitlab.jfronny.commons.data.dynamic.DFinal.of;
|
2022-06-03 20:30:25 +02:00
|
|
|
|
|
|
|
public class StandardLib {
|
|
|
|
private static final Random rnd = new Random();
|
|
|
|
|
|
|
|
public static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
|
|
|
|
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd. MM. yyyy");
|
|
|
|
|
|
|
|
public static ExpressionParameter addTo(ExpressionParameter parameter) {
|
|
|
|
return parameter
|
|
|
|
.set("PI", of(Math.PI))
|
|
|
|
.set("time", of(timeFormat.format(new Date())))
|
|
|
|
.set("date", of(dateFormat.format(new Date())))
|
|
|
|
|
|
|
|
.set("round", of(StandardLib::round))
|
|
|
|
.set("floor", of(StandardLib::floor))
|
|
|
|
.set("ceil", of(StandardLib::ceil))
|
|
|
|
.set("abs", of(StandardLib::abs))
|
|
|
|
.set("random", of(StandardLib::random))
|
|
|
|
|
|
|
|
.set("toUpper", of(StandardLib::toUpper))
|
|
|
|
.set("toLower", of(StandardLib::toLower))
|
|
|
|
.set("contains", of(StandardLib::contains))
|
|
|
|
.set("replace", of(StandardLib::replace));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Numbers
|
2022-06-07 12:19:58 +02:00
|
|
|
public static Dynamic<?> round(DList args) {
|
2022-06-03 20:30:25 +02:00
|
|
|
if (args.size() == 1) {
|
|
|
|
return of(Math.round(args.get(0).asNumber().getValue()));
|
2022-11-24 19:05:51 +01:00
|
|
|
} else if (args.size() == 2) {
|
2022-06-03 20:30:25 +02:00
|
|
|
double x = Math.pow(10, (int) (double) args.get(1).asNumber().getValue());
|
|
|
|
return of(Math.round(args.get(0).asNumber().getValue() * x) / x);
|
2022-11-24 19:05:51 +01:00
|
|
|
} else {
|
2022-06-03 20:30:25 +02:00
|
|
|
throw new IllegalArgumentException("Invalid number of arguments for round: expected 1 or 2 but got " + args.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 12:19:58 +02:00
|
|
|
public static Dynamic<?> floor(DList args) {
|
2022-06-03 20:30:25 +02:00
|
|
|
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for floor: expected 1 but got " + args.size());
|
|
|
|
return of(Math.floor(args.get(0).asNumber().getValue()));
|
|
|
|
}
|
|
|
|
|
2022-06-07 12:19:58 +02:00
|
|
|
public static Dynamic<?> ceil(DList args) {
|
2022-06-03 20:30:25 +02:00
|
|
|
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for ceil: expected 1 but got " + args.size());
|
|
|
|
return of(Math.ceil(args.get(0).asNumber().getValue()));
|
|
|
|
}
|
|
|
|
|
2022-06-07 12:19:58 +02:00
|
|
|
public static Dynamic<?> abs(DList args) {
|
2022-06-03 20:30:25 +02:00
|
|
|
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for abs: expected 1 but got " + args.size());
|
|
|
|
return of(Math.abs(args.get(0).asNumber().getValue()));
|
|
|
|
}
|
|
|
|
|
2022-06-07 12:19:58 +02:00
|
|
|
public static Dynamic<?> random(DList args) {
|
2022-06-03 20:30:25 +02:00
|
|
|
if (args.size() == 0) return of(rnd.nextDouble());
|
|
|
|
else if (args.size() == 2) {
|
|
|
|
double min = args.get(0).asNumber().getValue();
|
|
|
|
double max = args.get(1).asNumber().getValue();
|
|
|
|
return of(min + (max - min) * rnd.nextDouble());
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Invalid number of arguments for random: expected 0 or 2 but got " + args.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strings
|
2022-06-07 12:19:58 +02:00
|
|
|
public static Dynamic<?> toUpper(DList args) {
|
2022-06-03 20:30:25 +02:00
|
|
|
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for toUpper: expected 1 but got " + args.size());
|
|
|
|
return of(args.get(0).asString().getValue().toUpperCase());
|
|
|
|
}
|
|
|
|
|
2022-06-07 12:19:58 +02:00
|
|
|
public static Dynamic<?> toLower(DList args) {
|
2022-06-03 20:30:25 +02:00
|
|
|
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for toLower: expected 1 but got " + args.size());
|
|
|
|
return of(args.get(0).asString().getValue().toLowerCase());
|
|
|
|
}
|
|
|
|
|
2022-06-07 12:19:58 +02:00
|
|
|
public static Dynamic<?> contains(DList args) {
|
2022-06-03 20:30:25 +02:00
|
|
|
if (args.size() != 2) throw new IllegalArgumentException("Invalid number of arguments for contains: expected 2 but got " + args.size());
|
|
|
|
return of(args.get(0).asString().getValue().contains(args.get(1).asString().getValue()));
|
|
|
|
}
|
|
|
|
|
2022-06-07 12:19:58 +02:00
|
|
|
public static Dynamic<?> replace(DList args) {
|
2022-06-03 20:30:25 +02:00
|
|
|
if (args.size() != 3) throw new IllegalArgumentException("Invalid number of arguments for replace: expected 3 but got " + args.size());
|
|
|
|
return of(args.get(0).asString().getValue().replace(args.get(1).asString().getValue(), args.get(2).asString().getValue()));
|
|
|
|
}
|
|
|
|
}
|