index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*!
  2. * glob-base <https://github.com/jonschlinkert/glob-base>
  3. *
  4. * Copyright (c) 2015, Jon Schlinkert.
  5. * Licensed under the MIT License.
  6. */
  7. 'use strict';
  8. var path = require('path');
  9. var parent = require('glob-parent');
  10. var isGlob = require('is-glob');
  11. module.exports = function globBase(pattern) {
  12. if (typeof pattern !== 'string') {
  13. throw new TypeError('glob-base expects a string.');
  14. }
  15. var res = {};
  16. res.base = parent(pattern);
  17. res.isGlob = isGlob(pattern);
  18. if (res.base !== '.') {
  19. res.glob = pattern.substr(res.base.length);
  20. if (res.glob.charAt(0) === '/') {
  21. res.glob = res.glob.substr(1);
  22. }
  23. } else {
  24. res.glob = pattern;
  25. }
  26. if (!res.isGlob) {
  27. res.base = dirname(pattern);
  28. res.glob = res.base !== '.'
  29. ? pattern.substr(res.base.length)
  30. : pattern;
  31. }
  32. if (res.glob.substr(0, 2) === './') {
  33. res.glob = res.glob.substr(2);
  34. }
  35. if (res.glob.charAt(0) === '/') {
  36. res.glob = res.glob.substr(1);
  37. }
  38. return res;
  39. };
  40. function dirname(glob) {
  41. if (glob.slice(-1) === '/') return glob;
  42. return path.dirname(glob);
  43. }