Check if UNIX Path has Hidden Directory or File

This snippet of code uses regular expression to test if a path string contains any hidden directory. The use-case was derived from the difficulty in determining whether directory is hidden or not using NodeJS path module. Owing to the possibility of their existence within a long path string, this function returns false if any directory or the terminal file of a path has a name that starts with a dot.

/**
 * Checks whether a path starts with or contains a hidden file or a folder.
 *
 * @param {string} source - The path of the file that needs to be validated.
 * returns {boolean} - `true` if the source is blacklisted and otherwise `false`.
 *
 * @example
 * console.log(isUnixHiddenPath('~/documents')); // prints `false`
 * console.log(isUnixHiddenPath('~/documents/file.txt')); // prints `false`
 * console.log(isUnixHiddenPath('~/source/repo/.gitignore')); // prints `true`
 * console.log(isUnixHiddenPath('~/source/repo/.git/config')); // prints `true` 
 */ 
var isUnixHiddenPath = function (path) { 
    return (/(^|./).+[^/.]/g).test(path); 
};

A relevant StackOverflow question is located at http://stackoverflow.com/questions/8905680/nodejs-check-for-hidden-files/ and a gist is at https://gist.github.com/shamasis/7704671

Leave a Reply