nnx static method
Converts the given value to a non-null string with the given conditions. *
-
encode
- the encoding method. It can benone
(output directly),
- 'json',
xml
(for HTML/XML) andquery
(for query string). - If omitted,
xml
is assumed, i.e, < will be converted to & and so on. -
maxLength
: limit the number of characters being output.
- If non positive (default), the whole string will be output.
-
firstLine
: output only the first non-empty line (default: false).
-
pre
: whether to replace whitespace with
(default: false).
- It is meaningful only if encode is
xml
.
Implementation
static String nnx(value, {String? encode, int maxLength = 0,
bool firstLine = false, bool pre = false}) {
String str = encode == "json" ? json(value):
value != null ? value.toString(): "";
if (firstLine) {
for (int i = 0;;) {
final j = str.indexOf('\n', i);
if (j < 0) {
str = str.substring(i);
break;
}
if (j > i) {
str = str.substring(i, j);
break;
}
++i;
}
}
if (maxLength > 0 && maxLength > str.length)
str = maxLength < 3 ? "...": str.substring(0, maxLength - 3) + "...";
switch (encode) {
case "none":
case "json":
break;
case "query":
str = Uri.encodeQueryComponent(str);
break;
default: //xml/html
str = XmlUtil.encodeNS(str, pre: pre);
break;
}
return str;
}