muScript: extend stdlib
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
5a1329b884
commit
5e3dae3ef9
|
@ -6,8 +6,8 @@ import io.gitlab.jfronny.muscript.data.dynamic.additional.*;
|
|||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.gitlab.jfronny.muscript.data.dynamic.additional.DFinal.of;
|
||||
|
||||
|
@ -65,6 +65,7 @@ public class StandardLib {
|
|||
.set("flatMap", StandardLib::flatMap)
|
||||
.set("fold", StandardLib::fold)
|
||||
.set("forEach", StandardLib::forEach)
|
||||
.set("toObject", StandardLib::toObject)
|
||||
|
||||
.set("callableObject", StandardLib::callableObject)
|
||||
.set("enum", StandardLib::enum_);
|
||||
|
@ -122,6 +123,7 @@ public class StandardLib {
|
|||
public static DBool contains(DList args) {
|
||||
if (args.size() != 2) throw new IllegalArgumentException("Invalid number of arguments for contains: expected 2 but got " + args.size());
|
||||
if (args.get(0) instanceof DList list) return of(list.getValue().contains(args.get(1)));
|
||||
if (args.get(0) instanceof DObject object) return of(object.getValue().containsKey(args.get(1).asString().getValue()));
|
||||
else return of(args.get(0).asString().getValue().contains(args.get(1).asString().getValue()));
|
||||
}
|
||||
|
||||
|
@ -137,12 +139,14 @@ public class StandardLib {
|
|||
|
||||
public static DNumber len(DList args) {
|
||||
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for len: expected 1 but got " + args.size());
|
||||
Dynamic<?> d = args.get(0);
|
||||
return of(d instanceof DString ds ? ds.getValue().length() : d.asList().size());
|
||||
if (args.get(0) instanceof DString string) return of(string.getValue().length());
|
||||
if (args.get(0) instanceof DObject object) return of(object.getValue().size());
|
||||
return of(args.get(0).asList().size());
|
||||
}
|
||||
|
||||
public static DBool isEmpty(DList args) {
|
||||
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for isEmpty: expected 1 but got " + args.size());
|
||||
if (args.get(0) instanceof DObject object) return of(object.getValue().isEmpty());
|
||||
return of(args.get(0).asList().isEmpty());
|
||||
}
|
||||
|
||||
|
@ -182,6 +186,22 @@ public class StandardLib {
|
|||
return result;
|
||||
}
|
||||
|
||||
public static DObject toObject(DList args) {
|
||||
if (args.size() != 3) throw new IllegalArgumentException("Invalid number of arguments for args: expected 3 but got " + args.size());
|
||||
DCallable keyMapper = args.get(1).asCallable();
|
||||
DCallable valueMapper = args.get(2).asCallable();
|
||||
return of(args.get(0)
|
||||
.asList()
|
||||
.getValue()
|
||||
.stream()
|
||||
.collect(Collectors.<Dynamic<?>, String, Dynamic<?>, LinkedHashMap<String, Dynamic<?>>>toMap(
|
||||
a -> keyMapper.call(a).asString().getValue(),
|
||||
valueMapper::call,
|
||||
(a, b) -> b,
|
||||
LinkedHashMap::new
|
||||
)));
|
||||
}
|
||||
|
||||
// Objects
|
||||
public static DCallableObject callableObject(DList args) {
|
||||
if (args.size() != 2) throw new IllegalArgumentException("Invalid number of arguments for callableObject: expected 2 but got " + args.size());
|
||||
|
|
Loading…
Reference in New Issue