@jsxtools/glob

1.0.0 • Public • Published

glob

npm version build status issue tracker pull requests

glob lets you convert glob patterns into regular expressions. It has no dependencies and is 1120 bytes, or 471 bytes gzipped. It works in all browsers and all versions of Node.

glob is a code-golfing fork of the amazing globrex.

Usage

Add glob to your project:

npm install @jsxtools/glob

Use glob to convert glob patterns into regular expressions:

glob = require('@jsxtools/glob')

// match JS files either within "path/to/" or a subdirectory of "path/to/"
match1 = glob('path/to/{*,*/*}.js')

// these paths will match
match1.test('path/to/file.js')
match1.test('path/to/lib/file.js')

// these paths will NOT match
match1.test('path/to/file.js.map')
match1.test('path/to/lib/deeper/file.js')
match1.test('path/to/file.js/lib')

// match paths that start with "p" followed by any letter followed by "ck"
match2 = glob('p[a-z]ck')

// these will match
match2.test('pack')
match2.test('puck')

// these will NOT match
match2.test('pck')
match2.test('pluck')
match2.test('p-ck')

glob supports single character matching.

glob('/path/to/file?.txt').test('/path/to/file1.txt') // true
glob('/path/to/file?.txt').test('/path/to/file2.txt') // true
glob('/path/to/file?.txt').test('/path/to/file3.txt') // true
glob('/path/to/file?.txt').test('/path/to/fileZ.txt') // true

glob('/path/to/file?.txt').test('/path/to/file11.txt') // FALSE
glob('/path/to/file?.txt').test('/path/to/file.txt') // FALSE

glob supports matching ranges of characters.

glob('/path/to/[a-c]').test('/path/to/a') // true
glob('/path/to/[a-c]').test('/path/to/b') // true
glob('/path/to/[a-c]').test('/path/to/c') // true

glob('/path/to/[a-c]').test('/path/to/d') // FALSE
glob('/path/to/[a-c]').test('/path/to/e') // FALSE

glob supports group matching.

glob('{*.doc,*.pdf}').test('file.doc') // true
glob('{*.doc,*.pdf}').test('file.pdf') // true
glob('{*.doc,*.pdf}').test('file.xxx.pdf') // true

glob('{*.doc,*.pdf}').test('file.pdf.xxx') // FALSE

glob supports globstar patterns.

glob('/path/*/index.js').test('/path/to/index.js') // true
glob('/path/*/index.js').test('/path/of/index.js') // true

glob('/path/*/index.js').test('/path/to/the/index.js') // FALSE
glob('/path/*/index.js').test('/path/of/the/index.js') // FALSE
glob('/path/*/index.js').test('/path/index.js') // FALSE
glob('/path/*/index.js').test('/index.js') // FALSE

glob supports posix-style paths and does not support windows-style paths.

I recommend you detect the windows environment and manage this conversion yourself:

// whether the environment is windows
const isWin = process.platform === 'win32'

// returns a windows-style path converted into a posix-style path
const win2pos = win32path => win32path.replace(/^(\w+):|\\/g, '/$1')

// returns any path conditionally converted into a posix-style path
const all2pos = anypath => isWin ? win2pos(anypath) : anypath

// becomes "/Users/THX1138/images/../files"
all2pos('/Users/THX1138/images/../files')

// becomes "/C/Users/THX1138/images/../files"
all2pos('C:\\Users\\THX1138\\images\\..\\files')

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i @jsxtools/glob

    Weekly Downloads

    10

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    11.1 kB

    Total Files

    4

    Last publish

    Collaborators

    • jonathantneal