escape static method

String escape(
  1. Object? value, [
  2. bool quote = true
])

Implementation

static String escape(Object? value, [bool quote = true]) {
  if (value is int) {
    if (value >= 0 && value <= 0xd7ff ||
        value >= 0xe000 && value <= 0x10ffff) {
      value = String.fromCharCode(value);
    } else {
      return value.toString();
    }
  } else if (value is! String) {
    return value.toString();
  }

  final map = {
    '\b': '\\b',
    '\f': '\\f',
    '\n': '\\n',
    '\r': '\\r',
    '\t': '\\t',
    '\v': '\\v',
  };
  var result = value.toString();
  for (final key in map.keys) {
    result = result.replaceAll(key, map[key]!);
  }

  if (quote) {
    result = "'$result'";
  }

  return result;
}