wrap method

String wrap(
  1. String content, {
  2. int? braceCount,
  3. String? startDeliminator,
  4. String? endDeliminator,
})

Wraps the content with mustache

eg: {{#snakeCase}}This is the content{{/snakeCase}}

Implementation

String wrap(
  String content, {
  int? braceCount,
  String? startDeliminator,
  String? endDeliminator,
}) {
  assert(
    braceCount == null || braceCount >= 2 && braceCount <= 3,
    'braceCount must be 2 or 3',
  );

  assert(
    startDeliminator == null || startDeliminator.length == 1,
    'startDeliminator must be 1 character',
  );

  assert(
    endDeliminator == null || endDeliminator.length == 1,
    'endDeliminator must be 1 character',
  );

  assert(
    (startDeliminator == null) == (endDeliminator == null),
    'startDeliminator and endDeliminator must be null or not null',
  );

  if (startDeliminator != null && endDeliminator != null) {
    assert(
      startDeliminator != endDeliminator,
      'startDeliminator and endDeliminator must be different',
    );
  }

  final startBraces = (startDeliminator ?? '{') * 2;
  final endBraces = (endDeliminator ?? '}') * 2;

  if (isFormat) {
    final isWrapped = wrappedPattern.hasMatch(content);

    var wrappedContent = content;

    if (!isWrapped) {
      braceCount ??= Constants.kDefaultBraces;

      final startBraces = (startDeliminator ?? '{') * braceCount;
      final endBraces = (endDeliminator ?? '}') * braceCount;
      wrappedContent = '$startBraces$content$endBraces';
    }

    return '$startBraces#$name$endBraces$wrappedContent$startBraces/$name$endBraces';
  }

  assert(
    !wrappedPattern.hasMatch(content),
    'Content must not be wrapped with {{{}}} when not formatting content',
  );

  if (isIf) {
    return '$startBraces#$content$endBraces';
  }

  if (isIfNot) {
    return '$startBraces^$content$endBraces';
  }

  if (isEndIf) {
    return '$startBraces/$content$endBraces';
  }

  return content;
}