index.js 658 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. var path = require('path');
  3. function containsPath(fp, segment) {
  4. if (typeof fp !== 'string' || typeof segment !== 'string') {
  5. throw new TypeError('contains-path expects file paths to be a string.');
  6. }
  7. var prefix = '(^|\\/)';
  8. if (segment.indexOf('./') === 0 || segment.charAt(0) === '/') {
  9. prefix = '^';
  10. }
  11. var re = new RegExp(prefix + normalize(segment).join('\\/') + '($|\\/)');
  12. fp = normalize(fp).join('/');
  13. return re.test(fp);
  14. }
  15. /**
  16. * Normalize slashes
  17. */
  18. function normalize(str) {
  19. str = path.normalize(str);
  20. return str.split(/[\\\/]+/);
  21. }
  22. /**
  23. * Expose `containsPath`
  24. */
  25. module.exports = containsPath;