ignores method

bool ignores(
  1. String path
)

Returns true if path is ignored by the patterns used to create this Ignore instance, assuming those patterns are placed at ..

The path must be a relative path, not starting with ./, ../, and must end in slash (/) if it is directory.

Example:

import 'package:ignore/ignore.dart';

void main() {
  final ignore = Ignore([
    '*.o',
  ]);

  print(ignore.ignores('main.o')); // true
  print(ignore.ignores('main.c')); // false
  print(ignore.ignores('lib/')); // false
  print(ignore.ignores('lib/helper.o')); // true
  print(ignore.ignores('lib/helper.c')); // false
}

Implementation

bool ignores(String path) {
  ArgumentError.checkNotNull(path, 'path');
  if (path.isEmpty) {
    throw ArgumentError.value(path, 'path', 'must be not empty');
  }
  if (path.startsWith('/') ||
      path.startsWith('./') ||
      path.startsWith('../') ||
      path == '.' ||
      path == '..') {
    throw ArgumentError.value(
      path,
      'path',
      'must be relative, and not start with "./", "../"',
    );
  }
  if (_rules.isEmpty) {
    return false;
  }
  final pathWithoutSlash =
      path.endsWith('/') ? path.substring(0, path.length - 1) : path;
  return listFiles(
    beneath: pathWithoutSlash,
    includeDirs: true,
    // because we are listing below pathWithoutSlash
    listDir: (dir) {
      // List the next part of path:
      if (dir == pathWithoutSlash) return [];
      final startOfNext = dir.isEmpty ? 0 : dir.length + 1;
      final nextSlash = path.indexOf('/', startOfNext);
      return [path.substring(startOfNext, nextSlash)];
    },
    ignoreForDir: (dir) => dir == '.' || dir.isEmpty ? this : null,
    isDir: (candidate) =>
        candidate == '.' ||
        candidate.isEmpty ||
        path.length > candidate.length && path[candidate.length] == '/',
  ).isEmpty;
}