find-in是Node的文件文本搜索

find-in是Node的文件文本搜索

Node.js 文本处理

详细介绍

NPM

Travis Codecov license

Overview

find-in is file text-searching for node.

How it works?

Simply, find-in creates read stream to read from the target file in chunks, matches the chunks using match method returns an array of objects contains the final results in callback function.

Getting Started

clone the repo:

git clone git@github.com:jimmy02020/find-in.git
cd find-in

Using npm:

$ npm install find-in

Syntax

find(options, callback)

options

  • path file path,
  • request array of regex that will be matched in file
  • encoding read stream encoding (default: utf8)
  • join number of chunk combined (default: 2), increasing the number will widen the matching chunk boundaries

The callback gets two arguments (err, report).

report An array of objects. Each element contains three objects:

  • isFound searching result
  • reg regex sent
  • match matching result. An array if there are results otherwise returns null. for more see String.prototype.match()

Using find-in

const find = require('find-in')

// let's create some request to search for it in our file.
const req = [
  /old/g,
  /new/g,
]

find({ path: '/path1/path2/fileName', request: req }, (err, report) => {
  //
  [
    {
      isFound: true,
      reg:/old/g,
      match: ['old'] // the result of matching
    },
    { isFound: false, // not found so it wasn't changed
      reg: /new/g,
      match: null
    },
   ]
  //
});

Or you can check specific result as following.

find({ path: '/path1/path2/fileName', request: [ph0, ph1, p2, ph3] }, (err, report) => {
  if(report[2].isFound){
    console.log('p2 was found');
    // do something
  } else {
    console.log('not found');
    // do something else
  }
});

Tests

$ npm test

License

This project is licensed under the MIT License

推荐源码