chris-website/source/nl/netsoj/chris/blog/utils.d

32 lines
579 B
D
Raw Permalink Normal View History

2020-06-24 13:53:18 +00:00
import std.algorithm;
import std.array;
2020-06-24 08:08:28 +00:00
import std.conv;
import std.datetime;
import dyaml;
string toHumanString(DateTime value) {
return to!string(value.day) ~ "-" ~ to!string(ubyte(value.month)) ~ "-" ~ to!string(value.year);
2020-06-24 08:08:28 +00:00
}
T getOr(T)(Node node, string key, T or) {
if (key in node) {
try {
return node[key].get!T;
} catch (Exception e) {
return or;
}
2020-06-24 08:08:28 +00:00
} else {
return or;
}
}
2020-06-24 13:53:18 +00:00
T[] getArray(T)(Node node, string key, T[] or = []) {
try {
return node.getOr!(Node[])(key, [])
.map!(x => x.as!T).array;
} catch (NodeException e) {
return or;
}
}