├── index.js ├── bower.json ├── glob.min.js ├── package.json ├── test.js ├── LICENSE ├── README.md └── glob.js /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./glob.js').glob; -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glob.js", 3 | "main": "glob.min.js", 4 | "version": "0.0.1", 5 | "homepage": "https://github.com/ichuan/glob.js", 6 | "authors": [ 7 | "ichuan " 8 | ], 9 | "description": "UNIX globbing (wildcard matching) for javascript", 10 | "keywords": [ 11 | "glob", 12 | "wildcard" 13 | ], 14 | "license": "MIT" 15 | } 16 | -------------------------------------------------------------------------------- /glob.min.js: -------------------------------------------------------------------------------- 1 | /* 2 | * glob.js: UNIX globbing (wildcard matching) for javascript 3 | * yc 4 | */(function(){var e={},t=typeof exports!="undefined"?exports:window,n=function(t){return e[t]?e[t]:(reg=t.replace(/[.+^$()|{}]/g,function(e,t,n){return n[t-1]==="\\"?e:"\\"+e}),reg=reg.replace(/[?*]/g,function(e,t,n){return n[t-1]==="\\"?e:e==="?"?".":".*"}),reg=reg.replace(/\\([dDsSwWtrnvfbB0cxu])/g,"$1"),e[t]=new RegExp("^"+reg+"$"))},r=function(e,t){return t.match(typeof e=="string"?n(e):e)};t.glob={make:n,match:r}})(); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glob.js", 3 | "version": "0.1.0", 4 | "description": "UNIX globbing (wildcard matching) for javascript", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/ichuan/glob.js.git" 12 | }, 13 | "keywords": [ 14 | "glob" 15 | ], 16 | "author": "ichuan ", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/ichuan/glob.js/issues" 20 | }, 21 | "homepage": "https://github.com/ichuan/glob.js" 22 | } 23 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var glob= require('./glob').glob 2 | , assert = require('assert'); 3 | 4 | // Test 5 | // ? and * 6 | assert.ok(glob.match('abcd\\?de', 'abcd?de')) 7 | assert.ok(glob.match('abcd?de', 'abcdxde')) 8 | assert.ok(glob.match('abcd\\*de', 'abcd*de')) 9 | assert.ok(glob.match('abcd*de', 'abcdsdg93klg9slgde')) 10 | // char group 11 | assert.ok(glob.match('ab[cf]d*de', 'abfdsdg93klg9slgde')) 12 | // special chars 13 | assert.ok(glob.match('ab.cd(w|9)x', 'ab.cd(w|9)x')) 14 | assert.ok(!glob.match('ab.cd(w|9)x', 'ab.cdwx')) 15 | // normal escapings 16 | assert.ok(glob.match('abc\\me', 'abcme')) 17 | assert.ok(glob.match('abc\\\\e', 'abc\\e')) 18 | // special escapings 19 | assert.ok(glob.match('abc\\nme', 'abcnme')) 20 | assert.ok(glob.match('abc\\dme', 'abcdme')) 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 yc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Intro 2 | 3 | UNIX globbing (wildcard matching) for javascript 4 | 5 | 6 | ## Usage 7 | 8 | ### nodejs 9 | 10 | ```bash 11 | $ npm install glob.js 12 | ``` 13 | 14 | ```javascript 15 | var glob= require('glob.js'); 16 | glob.match('pattern', 'string') // ... 17 | ``` 18 | ### browser 19 | 20 | ```html 21 | 22 | 25 | ``` 26 | 27 | ## Fetures 28 | 29 | ### Match exactly one unknown character 30 | ```javascript 31 | glob.match('ab?d', 'abcd') // ok 32 | glob.match('ab\\?d', 'ab?d') // ok 33 | ``` 34 | ### Match any number of unknown characters 35 | ```javascript 36 | glob.match('ab*d', 'abcaeg@3d') // ok 37 | glob.match('ab\\*d', 'ab*d') // ok 38 | ``` 39 | ### Match a character as part of a group of characters 40 | ```javascript 41 | glob.match('ab[cd]e', 'abce') // ok 42 | ``` 43 | ### Escape character 44 | ```javascript 45 | glob.match('ab\\nc', 'abnc') // ok 46 | glob.match('ab\\mc', 'abmc') // ok 47 | ``` 48 | 49 | ### Combining 50 | ```javascript 51 | glob.match("ab?c*[df]\\d|.q", 'ab1c234fd|.q') // ok 52 | ``` 53 | 54 | ## Install 55 | 56 | ```bash 57 | bower install glob.js 58 | ``` 59 | -------------------------------------------------------------------------------- /glob.js: -------------------------------------------------------------------------------- 1 | /* 2 | * glob.js: UNIX globbing (wildcard matching) for javascript 3 | * yc 4 | */ 5 | 6 | (function () { 7 | var caches = {} 8 | , context = typeof(exports) !== 'undefined' ? exports : window; 9 | 10 | // make str pattern to regexp 11 | // http://en.wikipedia.org/wiki/Glob_(programming) 12 | var make = function (str) { 13 | if (caches[str]) { 14 | return caches[str]; 15 | } 16 | 17 | // special regexp chars (., +, etc.) 18 | reg = str.replace(/[.+^$()|{}]/g, function (match, offset, s) { 19 | return s[offset - 1] === '\\' ? match : '\\' + match; 20 | }); 21 | // ? and * 22 | reg = reg.replace(/[?*]/g, function (match, offset, s) { 23 | if (s[offset - 1] === '\\') { 24 | return match; 25 | } 26 | return match === '?' ? '.' : '.*'; 27 | }); 28 | // special regexp escapings (\d, \S, etc.) 29 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp 30 | reg = reg.replace(/\\([dDsSwWtrnvfbB0cxu])/g, '$1'); 31 | return caches[str] = new RegExp('^' + reg + '$'); 32 | }; 33 | 34 | // test if a pattern (str or regexp) matches a str 35 | var match = function (pattern, str) { 36 | return str.match(typeof(pattern) === 'string' ? make(pattern) : pattern); 37 | }; 38 | 39 | context.glob = { 40 | make: make, 41 | match: match 42 | }; 43 | })(); 44 | --------------------------------------------------------------------------------