indent

Last updated:

0 purchases

indent Image
indent Images
Add to Cart

Description:

indent

indent #



Change indentation in multiline Dart strings while preserving the existing relative indentation.
A GIF speaks more than a thousand words:

You can run the example app yourself by running cd example && pub get && webdev serve from the project root.
Usage #
For convenience, the library adds the following extension members on Dart's String class.
You can also wrap a string with the Indentation class and call methods on that - this is what the extension methods do under the hood.
unindent() #
If you found this library from a Google search, you're probably looking for the unindent() method.
It's the use case this library was originally created for.
For example, this:
import 'package:indent/indent.dart';

print('''
Hello
there
World!
'''.unindent());
copied to clipboard
outputs this:
Hello
there
World!
copied to clipboard
It gets rid of the common indentation while preserving the relative indentation. This is like Kotlin's trimIndent() or Java 12's align().
trimMargin([String marginPrefix = '|']) #
Trims the leading whitespace followed by the marginPrefix from each line.
For example, this:
import 'package:indent/indent.dart';

print('''
| Hello
|there
| World!
'''.trimMargin());
copied to clipboard
outputs this:
Hello
there
World!
copied to clipboard
Behaves just like trimMargin in Kotlin.
indent(int indentationLevel) #
Indents a string with the desired indentation level while preserving relative indentation.
For example, this:
import 'package:indent/indent.dart';

print('''
Hello
World
'''.indent(2));
copied to clipboard
prints:
Hello
World
copied to clipboard
If the starting indentation level is higher than the desired one, the value will be unindented accordingly.
This:
import 'package:indent/indent.dart';

print('''
Hello
World
'''.indent(2));
copied to clipboard
also prints:
Hello
World
copied to clipboard
(calling indent(0) is equal to calling unindent().)
indentBy(int howMuch) #
Changes the indentation level in a string by howMuch.
A positive value for howMuch adds indentation.
For example, this:
import 'package:indent/indent.dart';

print('''
Hello
World
'''.indentBy(4));
copied to clipboard
prints this:
Hello
World
copied to clipboard
When a negative value for howMuch is given, indentation is removed accordingly.
This:
import 'package:indent/indent.dart';

print('''
Hello
World
'''.indentBy(-4));
copied to clipboard
prints this:
Hello
World
copied to clipboard
getIndentationLevel() #
Returns the common indentation level in a string.
For example, this:
import 'package:indent/indent.dart';

final int indentationLevel= '''
Hello
World
'''.getIndentationLevel();
copied to clipboard
returns 2 as the two spaces before World is the lowest common indentation in the string.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.