remove method

int remove(
  1. String path, {
  2. bool recursive = true,
})

Removes the entry at path and, when recursive (the default), every entry inside it.

Paths are compared by id, so with a case-insensitive policy remove('/A') also removes /a/b.txt. Removing a path root such as / with recursive removes every entry under that root. Returns the number of entries removed; an invalid path removes nothing and returns 0. Runs in O(entries).

Implementation

int remove(String path, {bool recursive = true}) {
  final policy = _options.paths;
  final target = policy.tryParse(path);
  if (target == null) return 0;
  final targetId = policy.idOf(target);
  final removedSelf = _items.remove(targetId) == null ? 0 : 1;
  if (!recursive) return removedSelf;

  final bool Function(_BuilderItem<T> item) isInside;
  if (target.isRoot) {
    final rootKey = _foldWith(policy, target.root);
    isInside = (item) {
      final itemPath = item.path;
      return itemPath != null &&
          !itemPath.isRoot &&
          _foldWith(policy, itemPath.root) == rootKey;
    };
  } else {
    // Ids of paths inside the target extend the target id with '/'.
    final prefix = '${targetId.value}/';
    isInside = (item) => item.key?.value.startsWith(prefix) ?? false;
  }
  final doomed = [
    for (final MapEntry(:key, :value) in _items.entries)
      if (isInside(value)) key,
  ];
  for (final key in doomed) {
    _items.remove(key);
  }
  return removedSelf + doomed.length;
}