node-glob - Globbing for Node
One day, in irc://irc.freenode.net#node.js...
[4:46pm] _ry: it would be good to
have a file system Glob functionality (to get an array of
filenames)
// 9 months later...
(You can also install it by doing node-waf configure build and then
linking or copying the folder into your project's node_modules
directory.)
Wtf's a "glob"?
A glob is a pattern-matching syntax that shells use. Like when you do
rm *.js, the *.js is a glob.
You can do nifty things with them.
Supported Environments
- Macintosh OS X (Darwin)
- FreeBSD
- NetBSD
- Linux
- Solaris
If it doesn't work on one of those environments, please post a bug.
Usage
This is a binding to
glob(3) and
fnmatch(3). It includes
a statically compiled port of the NetBSD glob and fnmatch
programs, so it might not exactly match what #include <fnmatch.h>
or #include <glob.h> implements on your system.
To load the library:
var glob = require("glob")
Methods
glob
Search through the filesystem asynchronously.
Params
- pattern: String
- flags: int, Optional (see below)
- cb: function
Return
NOTHING!
Example
glob(pattern, flags, function (er, matches) {
// if an error occurred, it's in er.
// otherwise, "matches" is an array of filenames.
...
})
globSync
Search through the filesystem synchronously
Params
- pattern: String
- flags: int, Optional (see below)
Return
Array of strings that match.
Example
var matches = glob.globSync(pattern, flags) // throws on error
fnmatch
Test if a string matches a pattern. (no i/o performed)
Params
- pattern: String
- str: String to test
- flags: int, Optional (see below)
Example
var isMatch = glob.fnmatch(pattern, str, flags)
Flags
The behavior of glob and fnmatch are modified by several bitwise flags.
The flags are defined on the main glob object. If you have an integer, and want to look up which flag it corresponds to, you can look it up on the glob/globSync functions if it is a glob flag, or on the fnmatch function if it is an fnmatch flag.
That is, fnmatch[fnmatch.FNM_CASEFOLD] === 'FNM_CASEFOLD'.
GLOB_DEFAULTUsed if no flags are passed toglob()orglobSync(). Equivalent ofGLOB_BRACE|GLOB_LIMIT|GLOB_STAR|GLOB_MARK|GLOB_TILDE.FNM_DEFAULTUsed if no flags are passed tofnmatch(). Equivalent ofFNM_PATHNAME|FNM_PERIOD.GLOB_MARKAppend / to matching directories.GLOB_NOCHECKReturn pattern itself if nothing matches.GLOB_NOSORTDon't sort.GLOB_NOESCAPEDisable backslash escaping.GLOB_NOSPACEMalloc call failed.GLOB_ABORTEDUnignored error.GLOB_NOMATCHNo match, and GLOB_NOCHECK was not set.GLOB_NOSYSImplementation does not support function.GLOB_BRACEExpand braces ala csh.GLOB_NOMAGICGLOB_NOCHECK without magic chars (csh).GLOB_LIMITLimit memory used by matches to ARG_MAXGLOB_TILDEExpand tilde names from the passwd file.GLOB_PERIODAllow metachars to match leading periods.GLOB_NO_DOTDIRSMake . and .. vanish from wildcards.GLOB_STARUse glob**to recurse directoriesGLOB_QUOTEsource compatibilityFNM_NOMATCHMatch failed.FNM_NOSYSFunction not implemented.FNM_NORESOut of resourcesFNM_NOESCAPEDisable backslash escaping.FNM_PATHNAMESlash must be matched by slash.FNM_PERIODPeriod must be matched by period.FNM_CASEFOLDPattern is matched case-insensitiveFNM_LEADING_DIRIgnore / after Imatch.