search-in-file
Advanced tools
Comparing version 1.2.0 to 1.2.1
const { createReadStream } = require("fs"); | ||
const { createInterface } = require('readline'); | ||
/** | ||
* Method to search the given text in the given data. | ||
* | ||
* @param {string} data Data on which search will be performed. | ||
* @param {string} textToSearch Text to search in file. | ||
* @param {object} options Various options for file search. | ||
* @returns {boolean} Returns true is text is present in given data. Else returns false | ||
*/ | ||
const search = (data, textToSearch, options) => { | ||
@@ -27,2 +35,10 @@ if (!options || !(options instanceof Object)) { | ||
/** | ||
* Method to read the given file and search for the given text line by line. | ||
* | ||
* @param {string} filePath Path of the file to search. | ||
* @param {string} textToSearch Text to search in file. | ||
* @param {object} options Various options for file search. | ||
* @returns {promise} Returns a promise that resolves with path of file, line and line number in which the given text is present. | ||
*/ | ||
const searchLineByLine = (filePath, textToSearch, options) => { | ||
@@ -56,2 +72,10 @@ return new Promise((resolve, reject) => { | ||
/** | ||
* Method to read the given file and search for the given text. | ||
* | ||
* @param {string} filePath Path of the file to search. | ||
* @param {string} textToSearch Text to search in file. | ||
* @param {object} options Various options for file search. | ||
* @returns {promise} Returns a promise that resolves with path of file in which the given text is present. | ||
*/ | ||
const readFileAndSearch = (filePath, textToSearch, options) => { | ||
@@ -58,0 +82,0 @@ return new Promise((resolve, reject) => { |
{ | ||
"name": "search-in-file", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Includes utility methods and command to search text within file(s).", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,4 +10,43 @@ # Search In File | ||
## Usage | ||
## As command line tool | ||
## Available Utility Methods | ||
### `1. fileSearch(paths, textToSearch, options)` | ||
> Search a text within files | ||
> | ||
> Parameters: | ||
> * paths: Absolute path files to search. | ||
> * textToSearch: Text to search in files. | ||
> * options: Various options for file search. | ||
> | ||
> Return value: A promise with a list of files in which text is present. | ||
### `2. getFilesFromDir(dirPath, recursive, omitEmpty)` | ||
> Get a list of files from a directory | ||
> | ||
> Parameters: | ||
> * dirPath: Path of the directory to get files from. | ||
> * recursive: Get files recursively from directory. | ||
> * omitEmpty: A flag to omit empty files from result. | ||
> | ||
> Return value: A list of files present in the given directory. | ||
### `3. search(data, textToSearch, options)` | ||
> Check if the text is present in given data | ||
> | ||
> Parameters: | ||
> * data: Data to search from. | ||
> * textToSearch: Text to search. | ||
> * options: Various options for search. | ||
> | ||
> Return value: true, if text is present in given data. Else false | ||
## Available Options | ||
* `recursive`: Searches recursively in all sub-directories and files. Default `false` | ||
* `words`: Searches for the exact word. Default `false` | ||
* `ignoreCase`: Case in-sensitive search. Default `false` | ||
* `isRegex`: Searches for the regular expression. Default `false` | ||
* `ignoreDir`: Ignore the specified directories/path while searching. Default `[]` | ||
* `fileMask`: Only search in files with given extension. Default `null` | ||
* `searchResults`: Type of search results to get. `lineNo` to get line with line number that contains given search text. Default `filePaths` | ||
## Usage As command line tool | ||
To use this as a command line tool install this package globally using `-g` command. | ||
@@ -86,42 +125,2 @@ | ||
``` | ||
will search for the exact word `hello` and output the files along with the line number where the text is found. | ||
## Utility Methods | ||
### `1. fileSearch(paths, textToSearch, options)` | ||
> Search a text within files | ||
> | ||
> Parameters: | ||
> * paths: Absolute path files to search. | ||
> * textToSearch: Text to search in files. | ||
> * options: Various options for file search. | ||
> | ||
> Return value: A promise with a list of files in which text is present. | ||
### `2. getFilesFromDir(dirPath, recursive, omitEmpty)` | ||
> Get a list of files from a directory | ||
> | ||
> Parameters: | ||
> * dirPath: Path of the directory to get files from. | ||
> * recursive: Get files recursively from directory. | ||
> * omitEmpty: A flag to omit empty files from result. | ||
> | ||
> Return value: A list of files present in the given directory. | ||
### `3. search(data, textToSearch, options)` | ||
> Check if the text is present in given data | ||
> | ||
> Parameters: | ||
> * data: Data to search from. | ||
> * textToSearch: Text to search. | ||
> * options: Various options for search. | ||
> | ||
> Return value: true, if text is present in given data. Else false | ||
## Available Options | ||
* `recursive`: Searches recursively in all sub-directories and files. Default `false` | ||
* `words`: Searches for the exact word. Default `false` | ||
* `ignoreCase`: Case in-sensitive search. Default `false` | ||
* `isRegex`: Searches for the regular expression. Default `false` | ||
* `ignoreDir`: Ignore the specified directories/path while searching. Default `[]` | ||
* `fileMask`: Only search in files with given extension. Default `null` | ||
* `searchResults`: Type of search results to get. `lineNo` to get line with line number that contains given search text. Default `filePaths` | ||
will search for the exact word `hello` and output the files along with the line number where the text is found. |
15927
259
124