carvable 1.2.1 copy "carvable: ^1.2.1" to clipboard
carvable: ^1.2.1 copied to clipboard

Allows you to remove and change parts of something, without modifying the original, builder-like. Carve objects such as Strings and AstNodes, or implement the generic interface.

Pub.dev package GitHub repository

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() );

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

And import the package:

import 'package:carvable/carvable.dart';

Usage #

Get the carvable object of a String.

final CarvableString carvable = 'abcd'.carvable;

You may add carving zones, such as a carving range:

carvable.remove(1, 3);

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'

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));
}

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, '');
}

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'
}

Contributing #

Contributions are welcome! Please open an issue or pull request if you find a bug or have a feature request.

1
likes
150
pub points
0%
popularity

Publisher

verified publisherdrafakiller.com

Allows you to remove and change parts of something, without modifying the original, builder-like. Carve objects such as Strings and AstNodes, or implement the generic interface.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

analyzer

More

Packages that depend on carvable