nnx static method

String nnx(
  1. dynamic value, {
  2. String? encode,
  3. int maxLength = 0,
  4. bool firstLine = false,
  5. bool pre = false,
})

Converts the given value to a non-null string with the given conditions. *

    • encode - the encoding method. It can be none (output directly),
  • 'json', xml (for HTML/XML) and query (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 &nbsp; (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;
}