UNPKG

1.09 kBJavaScriptView Raw
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
8'use strict';
9
10var path = require('path');
11var parent = require('glob-parent');
12var isGlob = require('is-glob');
13
14module.exports = function globBase(pattern) {
15 if (typeof pattern !== 'string') {
16 throw new TypeError('glob-base expects a string.');
17 }
18
19 var res = {};
20 res.base = parent(pattern);
21 res.isGlob = isGlob(pattern);
22
23 if (res.base !== '.') {
24 res.glob = pattern.substr(res.base.length);
25 if (res.glob.charAt(0) === '/') {
26 res.glob = res.glob.substr(1);
27 }
28 } else {
29 res.glob = pattern;
30 }
31
32 if (!res.isGlob) {
33 res.base = dirname(pattern);
34 res.glob = res.base !== '.'
35 ? pattern.substr(res.base.length)
36 : pattern;
37 }
38
39 if (res.glob.substr(0, 2) === './') {
40 res.glob = res.glob.substr(2);
41 }
42 if (res.glob.charAt(0) === '/') {
43 res.glob = res.glob.substr(1);
44 }
45 return res;
46};
47
48function dirname(glob) {
49 if (glob.slice(-1) === '/') return glob;
50 return path.dirname(glob);
51}