Need private packages and team management tools?Check out npm Teams »

find-files-by-patterns

1.1.1 • Public • Published

find-files-by-patterns

npm Information Page Node Version Documentation Build Status Coverage Status Maintainability MIT license


Use the iteration protocol to find files upwards and downwards in the file system.


Installation

Using npm:

npm install find-files-by-patterns

Assuming this file system, where the current working directory is /Documents/project:

/
└── Documents
    ├── data.csv
    └── project
        ├── node_modules
        └── package.json

In Node.js:

const { findFile, upwardDirectories,
  ofBasename, downwardFiles } = require("find-files-by-patterns");
 
(async () => {
  const dataFile = await findFile(upwardDirectories(), ofBasename("data.csv"));
  console.log(dataFile); //=> `/Documents/data.csv`
 
  for await (const file of downwardFiles("/Documents")) {
    console.log(file);
  }
  //=> `/Documents/data.csv`
  //=> `/Documents/project`
  //=> `/Documents/project/node_modules` ...
  //=> `/Documents/project/package.json`
})();
 

Table of Contents

API

Complete list of exported functions

Finders:

Files:

Directories:

Filters:

Iterable readdir:

Iterable utilities:

findFile and findFileSync

Finds the first file that matches all of the given filters in the given directories. If no directory is supplied, then the current working directory is read.

Specifications

type Filter<T> = (element: T) => Promise<boolean>;
type FilterSync<T> = (element: T) => boolean;
 
findFile(
  ...testsArray<Filter<string> | FilterSync<string>>
)Promise<string | null>;
 
findFile(
  directoriesstring | AsyncIterable<string> | Iterable<string>,
  ...testsArray<Filter<string> | FilterSync<string>>
)Promise<string | null>;
 
findFileSync(...testsArray<FilterSync<string>>)string | null;
 
findFileSync(
  directoriesstring | Iterable<string>,
  ...testsArray<FilterSync<string>>
)string | null;

findAllFiles and findAllFilesSync

Finds all the files that match all of the given filters in the given directories. If no directory is supplied, then the current working directory is read.

Specifications

type Filter<T> = (element: T) => Promise<boolean>;
type FilterSync<T> = (element: T) => boolean;
 
findAllFiles(
  ...testsArray<Filter<string> | FilterSync<string>>
)Promise<string[]>;
 
findAllFiles(
  directoriesstring | AsyncIterable<string> | Iterable<string>,
  ...testsArray<Filter<string> | FilterSync<string>>
)Promise<string[]>;
 
findAllFilesSync(...testsArray<FilterSync<string>>)string[];
 
findAllFilesSync(
  directoriesstring | Iterable<string>,
  ...testsArray<FilterSync<string>>
)string[];

findOnlyFile and findOnlyFileSync

Finds the first and only file in its directory that matches all of the given filters. If no directory is supplied, then the current working directory is read.

Specifications

type Filter<T> = (element: T) => Promise<boolean>;
type FilterSync<T> = (element: T) => boolean;
 
findOnlyFile(
  ...testsArray<Filter<string> | FilterSync<string>>
)Promise<string | null>;
 
findOnlyFile(
  directoriesstring | AsyncIterable<string> | Iterable<string>,
  ...testsArray<Filter<string> | FilterSync<string>>
)Promise<string | null>;
 
findOnlyFileSync(...testsArray<FilterSync<string>>)string | null;
 
findOnlyFileSync(
  directoriesstring | Iterable<string>,
  ...testsArray<FilterSync<string>>
)string | null;

downwardDirectories and downwardDirectoriesSync

Returns an iterable over the existing downward files from a start path. Symbolic links are followed and handled such that no directory is traversed twice.

Specifications

downwardDirectories()AsyncIterable<string>;
 
downwardDirectories(maximumDepthnumber)AsyncIterable<string>;
 
downwardDirectories(startDirectorystring)AsyncIterable<string>;
 
downwardDirectories(startDirectorystring,
                    maximumDepthnumber)AsyncIterable<string>;
 
downwardDirectoriesSync()Iterable<string>;
 
downwardDirectoriesSync(maximumDepthnumber)Iterable<string>;
 
downwardDirectoriesSync(startDirectorystring)Iterable<string>;
 
downwardDirectoriesSync(startDirectorystring,
                        maximumDepthnumber)Iterable<string>;

Example

Assuming this file system, where the current working directory is /Documents:

/
└── Documents
    ├── Images
    |   └── image.jpeg
    └── project
        ├── node_modules
        └── package.json
const { downwardDirectoriesSync } = require("find-files-by-patterns");
 
[
  ...downwardDirectoriesSync()
]; /*=> `[ '/Documents/Images',
           '/Documents/project',
           '/Documents/project/node_modules' ]`*/
[...downwardDirectoriesSync(1)]; /*=> `[ '/Documents/Images',
                                         '/Documents/project' ]`*/
[...downwardDirectoriesSync(
  "/Documents/project"
)]; //=> `[ '/Documents/project/node_modules' ]`
[...downwardDirectoriesSync(
  "/Documents", 1
)]; //=> `[ '/Documents/Images', '/Documents/project' ]`

upwardDirectories and upwardDirectoriesSync

Returns an iterable over the existing directories upwards from a start path.

Specifications

upwardDirectories()AsyncIterable<string>;
 
upwardDirectories(startPathstring)AsyncIterable<string>;
 
upwardDirectories(startPathstring,
                  maximumHeightnumber)AsyncIterable<string>;
 
upwardDirectories(startPathstring, endPathstring)AsyncIterable<string>;
 
upwardDirectoriesSync()Iterable<string>;
 
upwardDirectoriesSync(startPathstring)Iterable<string>;
 
upwardDirectoriesSync(startPathstring,
                      maximumHeightnumber)Iterable<string>;
 
upwardDirectoriesSync(startPathstring, endPathstring)Iterable<string>;

Example

Assuming this file system, where the current working directory is /Documents/project:

/
└── Documents
    ├── Images
    |   └── image.jpeg
    └── project
        ├── node_modules
        └── package.json
const { upwardDirectoriesSync } = require("find-files-by-patterns");
 
[...upwardDirectoriesSync()]; //=> `[ '/Documents', '/' ]`
[...upwardDirectoriesSync(1)]; //=> `[ '/Documents' ]`
[...upwardDirectoriesSync(
  "/Documents/Images/image.jpeg"
)]; //=> `[ '/Documents/Images', '/Documents', '/' ]`
[...upwardDirectoriesSync(
  "/Documents/Images/image.jpeg", 2
)]; //=> `[ '/Documents/Images', '/Documents' ]`
[...upwardDirectoriesSync(
  "/Documents/Images/image.jpeg", "/Documents"
)]; //=> `[ '/Documents/Images', '/Documents' ]`

downwardFiles and downwardFilesSync

Returns and iterable over the files in each downward directory yielded by downwardDirectories or downwardDirectoriesSync.

Specifications

downwardFiles()AsyncIterable<string>;
 
downwardFiles(maximumDepthnumber)AsyncIterable<string>;
 
downwardFiles(startDirectorystring)AsyncIterable<string>;
 
downwardFiles(startDirectorystring,
              maximumDepthnumber)AsyncIterable<string>;
 
downwardFilesSync()Iterable<string>;
 
downwardFilesSync(maximumDepthnumber)Iterable<string>;
 
downwardFilesSync(startDirectorystring)Iterable<string>;
 
downwardFilesSync(startDirectorystring,
                  maximumDepthnumber)Iterable<string>;

upwardFiles and upwardFilesSync

Returns and iterable over the files in each upward directory yielded by upwardDirectories or upwardDirectoriesSync.

Specifications

upwardFiles()AsyncIterable<string>;
 
upwardFiles(startPathstring)AsyncIterable<string>;
 
upwardFiles(startPathstring, maximumHeightnumber)AsyncIterable<string>;
 
upwardFiles(startPathstring, endPathstring)AsyncIterable<string>;
 
upwardFilesSync()Iterable<string>;
 
upwardFilesSync(startPathstring)Iterable<string>;
 
upwardFilesSync(startPathstring, maximumHeightnumber)Iterable<string>;
 
upwardFilesSync(startPathstring, endPathstring)Iterable<string>;

ofBasename, ofName, ofDirname and ofExtname

Determines whether or not a path's basename, name, dirname or extname matches any of a sequence of segment testers. A segment tester can be a string, a regular expression or a function.

  • If it is a string, then the segment must correspond to it for the path to match.
  • If it is a regular expression, then the segment must test against it for the path to match.
  • If it is a function, then the segment must make it return true for the path to match.

Specifications

type SegmentTester = string | RegExp | ((segment: string) => boolean);
 
ofBasename(...testsSegmentTester[])FilterSync<string>;
 
ofName(...testsSegmentTester[])FilterSync<string>;
 
ofDirname(...testsSegmentTester[])FilterSync<string>;
 
ofExtname(...testsSegmentTester[])FilterSync<string>;

Example

const { ofBasename, ofName,
        ofDirname, ofExtname } = require("find-files-by-patterns");
 
const isMarkdownFile = ofExtname(".md", ".markdown");
const isIndexFile = ofName("index");
const isDataFilename = ofBasename(/^data/);
const isInDocuments = ofDirname("/Documents");
 
isMarkdownFile("/Documents/article.md"); //=> `true`
isMarkdownFile("/Documents/data.json"); //=> `false`
isIndexFile("/Documents/index.html"); //=> `true`
isIndexFile("/Documents/index.md"); //=> `true`
isDataFilename("/Documents/data.json"); //=> `true`
isInDocuments("/Documents/data.json"); //=> `true`
isInDocuments("/Documents/src/index.js"); //=> `false`

hasPathSegments

Determines whether or not all the paths segments of a path match a sequence of segment testers. A segment tester can be a string, a regular expression or a function.

  • If it is a string, then the segment must correspond to it for the path to match.
  • If it is a regular expression, then the segment must test against it for the path to match.
  • If it is a function, then the segment must make it return true for the path to match.

Specifications

type SegmentTester = string | RegExp | ((segment: string) => boolean);
 
hasPathSegments(...testsSegmentTester[])FilterSync<string>;

Example

const { hasPathSegments } = require("find-files-by-patterns");
 
const isIgnored = hasPathSegments(segment => segment.startsWith("_"));
 
isIgnored("project/src/_ignored.json"); //=> `true`
 
isIgnored("project/_ignored/data.json"); //=> `true`
 
isIgnored("project/src/data.yaml"); //=> `false`

isFile and isFileSync

Determines whether or not a path exists on the file system and is a file.

Specifications

isFile(pathstring)Promise<boolean>;
 
isFileSync(pathstring)boolean;

isDirectory and isDirectorySync

Determines whether or not a path exists on the file system and is a directory.

Specifications

isDirectory(pathstring)Promise<boolean>;
 
isDirectorySync(pathstring)boolean;

hasFile and hasFileSync

Returns a filter which determines whether or not a path is a directory that has a file which matches a filter.

Specifications

hasFile(testFilter<string> | FilterSync<string>)Filter<string>;
 
hasFileSync(testFilterSync<string>)FilterSync<string>;

Example

Assuming this file system, where the current working directory is /Documents/project:

/
└── Documents
    ├── Images
    |   └── image.jpeg
    └── project
        ├── node_modules
        └── package.json
const { findFileSync, upwardDirectoriesSync,
  hasFileSync, ofBasename } = require("find-files-by-patterns");
 
const imagesDirectory = findFileSync(
  upwardDirectoriesSync(),
  hasFileSync(ofBasename("image.jpeg"))
); //=> `/Documents/Images`

readdir and readdirSync

Returns an iterable over the fully qualified file names in the given directory.

Specifications

readdir(directorystring)AsyncIterable<string>;
 
readdirSync(directorystring)Iterable<string>;

readdirs and readdirsSync

Returns an iterable over the fully qualified file names in the given sequence of directories.

Specifications

readdirs(directorystring)AsyncIterable<string>;
 
readdirsSync(directorystring)Iterable<string>;

filter and filterSync

Filters out the iterated elements of an iterable for which the filter function returns false. This is analogous to the array filter method.

Specifications

type Filter<T> = (element: T) => Promise<boolean>;
type FilterSync<T> = (element: T) => boolean;
 
filter<T>(iterableIterable<T> | AsyncIterable<T>,
          filterFilter<T> | FilterSync<T>)AsyncIterable<T>;
 
filterSync<T>(iterableIterable<T>,
              filterFilterSync<T>)Iterable<T>;

Example

const { filterSync, hasPathSegments } = require("find-files-by-patterns");
 
const paths = [
  "/Documents/project",
  "/Documents/project/data.json",
  "/Documents/project/_directory",
  "/Documents/project/_directory/data.json"
];
[...filterSync(paths, hasPathSegments(segment => !segment.startsWith("_")))];
//=> `[ '/Documents/project', '/Documents/project/data.json' ]`

conjunction and conjunctionSync

Compounds a sequence of filters using logical conjunction.

Specifications

type Filter<T> = (element: T) => Promise<boolean>;
type FilterSync<T> = (element: T) => boolean;
 
conjunction<T>(filtersArray<Filter<T> | FilterSync<T>>)Filter<T>;
 
conjunctionSync<T>(filtersArray<FilterSync<T>>)FilterSync<T>;

Example

const { ofBasename, ofExtname,
  conjunctionSync } = require("find-files-by-patterns");
 
const validDataExtensions = [".json", ".yaml", ".csv"];
const isDataFile = conjunctionSync([
  ofBasename(basename => basename.startsWith("data")),
  ofExtname(...validDataExtensions)
]);
 
isDataFile("/Documents/project/data.json"); //=> `true`
isDataFile("/Documents/project/data.yaml"); //=> `true`
isDataFile("/Documents/project/_data.json"); //=> `false`

disjunction and disjunctionSync

Compounds a sequence of filters using logical disjunction.

Specifications

type Filter<T> = (element: T) => Promise<boolean>;
type FilterSync<T> = (element: T) => boolean;
 
disjunction<T>(filtersArray<Filter<T> | FilterSync<T>>)Filter<T>;
 
disjunctionSync<T>(filtersArray<FilterSync<T>>)FilterSync<T>;

Example

const { ofBasename, disjunctionSync } = require("find-files-by-patterns");
 
const isDataFilename = disjunctionSync([
  ofBasename("data.json"),
  ofBasename("_data.json")
]);
 
isDataFilename("/Documents/project/data.json"); //=> `true`
isDataFilename("/Documents/project/_data.json"); //=> `true`
isDataFilename("/Documents/project/data.yaml"); //=> `false`

allElements and allElementsSync

Converts an iterable to an array.

Specifications

allElements<T>(iterableAsyncIterable<T>)Promise<T[]>;
 
allElementsSync<T>(iterableIterable<T>)T[];

firstElement and firstElementSync

Returns the first element of an iterable.

Specifications

firstElement<T>(iterableAsyncIterable<T>)Promise<T | null>;
 
firstElementSync<T>(iterableIterable<T>)T | null;

onlyElement and onlyElementSync

Returns the only yielded element of an iterable. If there is more than one element yielded by the iterable, then an error is thrown.

Specifications

onlyElement<T>(iterableAsyncIterable<T>)Promise<T | null>;
 
onlyElementSync<T>(iterableIterable<T>)T | null;

About

Building the documentation

npm run doc

Running the tests

npm run test

Building the library

npm run build

Authors

License

This project is licensed under the MIT License. See the LICENSE.md file for details.

Install

npm i find-files-by-patterns

DownloadsWeekly Downloads

4

Version

1.1.1

License

MIT

Unpacked Size

179 kB

Total Files

36

Last publish

Collaborators

  • avatar