0 purchases
carvable
Carvable #
Allows you to remove and change parts of something, without modifying the original, builder-like.
The carvable object returns a result with the changes applied.
With this, it's possible to hide sections of a string that would be created by objects.
For instance, hiding child nodes inside AstNode.
final AstNode node;
print( node.remove(node.childEntities.elementAt(0)).apply() );
copied to clipboard
The position of the child could change at any time, and it would still hide the correct section in the string.
Features #
Carvable generic interface
Carvable String
Carvable Analyzer objects, such as AstNode, Element and LibraryElement
Getting Started #
dart pub add carvable
copied to clipboard
And import the package:
import 'package:carvable/carvable.dart';
copied to clipboard
Usage #
Get the carvable object of a String.
final CarvableString carvable = 'abcd'.carvable;
copied to clipboard
You may add carving zones, such as a carving range:
carvable.remove(1, 3);
copied to clipboard
The range is inclusive in the first value and exclusive in the second value.
After carving all zones, you can apply to get the result:
print(carvable.apply()); // 'ad'
copied to clipboard
Carvable #
Carvable objects are objects which receive carvings to be used to delimit how the result will be modified, builder-like.
This object may accept any carving type, but might not apply all of them, depending on the Carvable implementation.
After adding the carvings you may apply those to get the result.
class CarvableStringExample extends Carvable<String, Carving<String, String>> {
final String input = 'abcd';
@override
String apply() => carvings.fold(input, (value, element) => element.apply(value));
}
copied to clipboard
Carvings #
Carvings are data structures with the information needed to carve a part of the object.
They can also carve themselves into an object, using the apply() method.
They have an input type, and a result type. Those delimit what the carving will do to the object, and what objects accept.
For instance, a string carving will have an input and result of String, and that way can be chained.
class CarvingRemove extends Carving<String, String> {
final int start;
final int end;
CarvingRemove(this.start, this.end);
@override
String apply(String input) => input.replaceRange(start, end, '');
}
copied to clipboard
Example #
Carvable String (/example/main.dart)
import 'package:carvable/carvable.dart';
void main() {
final carvable = CarvableString('abcd');
carvable.remove(1, 2);
print(carvable.apply()); // 'acd'
print('abcde'.carvable.remove(1, 2).remove(3, 4).apply()); // 'ace'
}
copied to clipboard
Contributing #
Contributions are welcome! Please open an issue or pull request if you find a bug or have a feature request.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.