chris-website/source/nl/netsoj/chris/blog/utils.d
2021-11-18 19:28:24 +01:00

34 lines
615 B
D

module nl.netsoj.chris.blog.utils;
import std.algorithm;
import std.array;
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);
}
T getOr(T)(Node node, string key, T or) {
if (key in node) {
try {
return node[key].get!T;
} catch (Exception e) {
return or;
}
} else {
return or;
}
}
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;
}
}