├── .gitignore ├── .npmignore ├── .travis.yml ├── .editorconfig ├── .jshintrc ├── LICENSE ├── package.json ├── index.js ├── README.md └── test └── main.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.log 3 | node_modules 4 | build 5 | *.node 6 | coverage 7 | *.orig 8 | .idea 9 | sandbox -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.log 3 | node_modules 4 | build 5 | *.node 6 | coverage 7 | *.orig 8 | .idea 9 | sandbox -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.11" 5 | after_script: 6 | - npm run coveralls -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "camelcase": true, 3 | "curly": true, 4 | "eqeqeq": true, 5 | "freeze": true, 6 | "indent": 2, 7 | "newcap": false, 8 | "quotmark": "single", 9 | "maxdepth": 3, 10 | "maxstatements": 50, 11 | "maxlen": 80, 12 | "eqnull": true, 13 | "funcscope": true, 14 | "strict": true, 15 | "undef": true, 16 | "unused": true, 17 | "node": true, 18 | "mocha": true 19 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Fractal 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glob2base", 3 | "description": "Extracts a base path from a node-glob instance", 4 | "version": "0.0.12", 5 | "homepage": "http://github.com/wearefractal/glob2base", 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/wearefractal/glob2base.git" 9 | }, 10 | "author": "Fractal (http://wearefractal.com/)", 11 | "main": "./index.js", 12 | "files": [ 13 | "index.js", 14 | "lib" 15 | ], 16 | "dependencies": { 17 | "find-index": "^0.1.1" 18 | }, 19 | "devDependencies": { 20 | "coveralls": "^2.6.1", 21 | "istanbul": "^0.3.2", 22 | "jshint": "^2.4.1", 23 | "jshint-stylish": "^1.0.0", 24 | "mocha": "^2.0.0", 25 | "mocha-lcov-reporter": "^0.0.1", 26 | "glob": "^4.0.0", 27 | "should": "^4.0.0" 28 | }, 29 | "scripts": { 30 | "lint": "jshint index.js --reporter node_modules/jshint-stylish/stylish.js --exclude node_modules", 31 | "test": "npm run-script lint && mocha --reporter spec", 32 | "coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage" 33 | }, 34 | "engines": { 35 | "node": ">= 0.10" 36 | }, 37 | "licenses": [ 38 | { 39 | "type": "MIT", 40 | "url": "http://github.com/wearefractal/glob2base/raw/master/LICENSE" 41 | } 42 | ] 43 | } 44 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path'); 4 | var findIndex = require('find-index'); 5 | 6 | var flattenGlob = function(arr){ 7 | var out = []; 8 | var flat = true; 9 | for(var i = 0; i < arr.length; i++) { 10 | if (typeof arr[i] !== 'string') { 11 | flat = false; 12 | break; 13 | } 14 | out.push(arr[i]); 15 | } 16 | 17 | // last one is a file or specific dir 18 | // so we pop it off 19 | if (flat) { 20 | out.pop(); 21 | } 22 | return out; 23 | }; 24 | 25 | var flattenExpansion = function(set) { 26 | var first = set[0]; 27 | var toCompare = set.slice(1); 28 | 29 | // find index where the diff is 30 | var idx = findIndex(first, function(v, idx){ 31 | if (typeof v !== 'string') { 32 | return true; 33 | } 34 | 35 | var matched = toCompare.every(function(arr){ 36 | return v === arr[idx]; 37 | }); 38 | 39 | return !matched; 40 | }); 41 | 42 | return first.slice(0, idx); 43 | }; 44 | 45 | var setToBase = function(set) { 46 | // normal something/*.js 47 | if (set.length <= 1) { 48 | return flattenGlob(set[0]); 49 | } 50 | // has expansion 51 | return flattenExpansion(set); 52 | }; 53 | 54 | module.exports = function(glob) { 55 | var set = glob.minimatch.set; 56 | var baseParts = setToBase(set); 57 | var basePath = path.normalize(baseParts.join(path.sep))+path.sep; 58 | return basePath; 59 | }; 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # glob2base [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Support us][gittip-image]][gittip-url] [![Build Status][travis-image]][travis-url] [![Coveralls Status][coveralls-image]][coveralls-url] 2 | 3 | 4 | ## Information 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
Packageglob2base
DescriptionExtracts a base path from a node-glob instance
Node Version>= 0.10
19 | 20 | ## Usage 21 | 22 | The module is a function that takes in a node-glob instance and returns a string. Basically it just gives you everything before any globbing/matching happens. 23 | 24 | ```javascript 25 | var glob2base = require('glob2base'); 26 | var glob = require('glob'); 27 | 28 | // js/ 29 | glob2base(new glob.Glob('js/**/*.js')); 30 | 31 | // css/test/ 32 | glob2base(new glob.Glob('css/test/{a,b}/*.css')); 33 | 34 | // pages/whatever/ 35 | glob2base(new glob.Glob('pages/whatever/index.html')); 36 | ``` 37 | 38 | [gittip-url]: https://www.gittip.com/WeAreFractal/ 39 | [gittip-image]: http://img.shields.io/gittip/WeAreFractal.svg 40 | 41 | [downloads-image]: http://img.shields.io/npm/dm/glob2base.svg 42 | [npm-url]: https://npmjs.org/package/glob2base 43 | [npm-image]: http://img.shields.io/npm/v/glob2base.svg 44 | 45 | [travis-url]: https://travis-ci.org/wearefractal/glob2base 46 | [travis-image]: http://img.shields.io/travis/wearefractal/glob2base.svg 47 | 48 | [coveralls-url]: https://coveralls.io/r/wearefractal/glob2base 49 | [coveralls-image]: http://img.shields.io/coveralls/wearefractal/glob2base/master.svg 50 | -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | var glob2base = require('../'); 2 | var glob = require('glob'); 3 | var path = require('path'); 4 | var should = require('should'); 5 | require('mocha'); 6 | 7 | describe('glob2base', function() { 8 | it('should get a base name', function() { 9 | var globber = new glob.Glob('js/*.js', {cwd: __dirname}); 10 | glob2base(globber).should.equal('js/'); 11 | }); 12 | 13 | it('should get a base name from a nested glob', function() { 14 | var globber = new glob.Glob('js/**/test/*.js', {cwd: __dirname}); 15 | glob2base(globber).should.equal('js/'); 16 | }); 17 | 18 | it('should get a base name from a flat file', function() { 19 | var globber = new glob.Glob('js/test/wow.js', {cwd: __dirname}); 20 | glob2base(globber).should.equal('js/test/'); 21 | }); 22 | 23 | it('should get a base name from character class pattern', function() { 24 | var globber = new glob.Glob('js/t[a-z]st}/*.js', {cwd: __dirname}); 25 | glob2base(globber).should.equal('js/'); 26 | }); 27 | 28 | it('should get a base name from brace , expansion', function() { 29 | var globber = new glob.Glob('js/{src,test}/*.js', {cwd: __dirname}); 30 | glob2base(globber).should.equal('js/'); 31 | }); 32 | 33 | it('should get a base name from brace .. expansion', function() { 34 | var globber = new glob.Glob('js/test{0..9}/*.js', {cwd: __dirname}); 35 | glob2base(globber).should.equal('js/'); 36 | }); 37 | 38 | it('should get a base name from extglob', function() { 39 | var globber = new glob.Glob('js/t+(wo|est)/*.js', {cwd: __dirname}); 40 | glob2base(globber).should.equal('js/'); 41 | }); 42 | 43 | it('should get a base name from a complex brace glob', function() { 44 | var globber = new glob.Glob('lib/{components,pages}/**/{test,another}/*.txt', {cwd: __dirname}); 45 | glob2base(globber).should.equal('lib/'); 46 | 47 | var globber2 = new glob.Glob('js/test/**/{images,components}/*.js', {cwd: __dirname}); 48 | glob2base(globber2).should.equal('js/test/'); 49 | 50 | var globber3 = new glob.Glob('ooga/{booga,sooga}/**/dooga/{eooga,fooga}', {cwd: __dirname}); 51 | glob2base(globber3).should.equal('ooga/'); 52 | }); 53 | }); 54 | --------------------------------------------------------------------------------