mirror of
https://github.com/thangisme/notes.git
synced 2024-11-09 16:37:20 -05:00
150 lines
3.1 KiB
JavaScript
150 lines
3.1 KiB
JavaScript
'use strict';
|
|
|
|
var win32 = process && process.platform === 'win32';
|
|
var path = require('path');
|
|
var fileRe = require('filename-regex');
|
|
var utils = module.exports;
|
|
|
|
/**
|
|
* Module dependencies
|
|
*/
|
|
|
|
utils.diff = require('arr-diff');
|
|
utils.unique = require('array-unique');
|
|
utils.braces = require('braces');
|
|
utils.brackets = require('expand-brackets');
|
|
utils.extglob = require('extglob');
|
|
utils.isExtglob = require('is-extglob');
|
|
utils.isGlob = require('is-glob');
|
|
utils.typeOf = require('kind-of');
|
|
utils.normalize = require('normalize-path');
|
|
utils.omit = require('object.omit');
|
|
utils.parseGlob = require('parse-glob');
|
|
utils.cache = require('regex-cache');
|
|
|
|
/**
|
|
* Get the filename of a filepath
|
|
*
|
|
* @param {String} `string`
|
|
* @return {String}
|
|
*/
|
|
|
|
utils.filename = function filename(fp) {
|
|
var seg = fp.match(fileRe());
|
|
return seg && seg[0];
|
|
};
|
|
|
|
/**
|
|
* Returns a function that returns true if the given
|
|
* pattern is the same as a given `filepath`
|
|
*
|
|
* @param {String} `pattern`
|
|
* @return {Function}
|
|
*/
|
|
|
|
utils.isPath = function isPath(pattern, opts) {
|
|
opts = opts || {};
|
|
return function(fp) {
|
|
var unixified = utils.unixify(fp, opts);
|
|
if(opts.nocase){
|
|
return pattern.toLowerCase() === unixified.toLowerCase();
|
|
}
|
|
return pattern === unixified;
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Returns a function that returns true if the given
|
|
* pattern contains a `filepath`
|
|
*
|
|
* @param {String} `pattern`
|
|
* @return {Function}
|
|
*/
|
|
|
|
utils.hasPath = function hasPath(pattern, opts) {
|
|
return function(fp) {
|
|
return utils.unixify(pattern, opts).indexOf(fp) !== -1;
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Returns a function that returns true if the given
|
|
* pattern matches or contains a `filepath`
|
|
*
|
|
* @param {String} `pattern`
|
|
* @return {Function}
|
|
*/
|
|
|
|
utils.matchPath = function matchPath(pattern, opts) {
|
|
var fn = (opts && opts.contains)
|
|
? utils.hasPath(pattern, opts)
|
|
: utils.isPath(pattern, opts);
|
|
return fn;
|
|
};
|
|
|
|
/**
|
|
* Returns a function that returns true if the given
|
|
* regex matches the `filename` of a file path.
|
|
*
|
|
* @param {RegExp} `re`
|
|
* @return {Boolean}
|
|
*/
|
|
|
|
utils.hasFilename = function hasFilename(re) {
|
|
return function(fp) {
|
|
var name = utils.filename(fp);
|
|
return name && re.test(name);
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Coerce `val` to an array
|
|
*
|
|
* @param {*} val
|
|
* @return {Array}
|
|
*/
|
|
|
|
utils.arrayify = function arrayify(val) {
|
|
return !Array.isArray(val)
|
|
? [val]
|
|
: val;
|
|
};
|
|
|
|
/**
|
|
* Normalize all slashes in a file path or glob pattern to
|
|
* forward slashes.
|
|
*/
|
|
|
|
utils.unixify = function unixify(fp, opts) {
|
|
if (opts && opts.unixify === false) return fp;
|
|
if (opts && opts.unixify === true || win32 || path.sep === '\\') {
|
|
return utils.normalize(fp, false);
|
|
}
|
|
if (opts && opts.unescape === true) {
|
|
return fp ? fp.toString().replace(/\\(\w)/g, '$1') : '';
|
|
}
|
|
return fp;
|
|
};
|
|
|
|
/**
|
|
* Escape/unescape utils
|
|
*/
|
|
|
|
utils.escapePath = function escapePath(fp) {
|
|
return fp.replace(/[\\.]/g, '\\$&');
|
|
};
|
|
|
|
utils.unescapeGlob = function unescapeGlob(fp) {
|
|
return fp.replace(/[\\"']/g, '');
|
|
};
|
|
|
|
utils.escapeRe = function escapeRe(str) {
|
|
return str.replace(/[-[\\$*+?.#^\s{}(|)\]]/g, '\\$&');
|
|
};
|
|
|
|
/**
|
|
* Expose `utils`
|
|
*/
|
|
|
|
module.exports = utils;
|