Nodejs:如何在从API请求获得的原始文件数据中重复检查特定字符串?

发布于 2025-01-09 19:39:47 字数 2211 浏览 0 评论 0原文

我有一个使用 Nodejs 开发的应用程序。该应用程序正在向 GitLab API 发出请求并从中获取原始文件数据。

我想读取另一个字符串之后出现的特定字符串,并从中获取所有类似的数据。我只是对这部分有点困惑,无法进一步进行,有人可以向我解释如何实现这一目标吗?

以下是示例文件数据: 我想读取关键字 Scenario: 之后出现的所有数字,即在这种情况下我想获取 A001-D002 & K002-M002。这些数字可以是任意数字,并且可以出现在文件内容中的任何位置。我想读取它们并将它们存储在该特定文件的数组中。

FileName: File Data
  Background:
    This is some random background

  Scenario: A001-D002 My first scenario
    Given I am sitting on a plane
    When I am offered drinks

  Scenario: K002-M002 My second scenario
    Given when I book the uber taxi
    When I get the notifications

我不明白如何迭代文件内容并读取每个单词并匹配并相应地获取 id。

以下是我向 GitLab 发出请求并获取原始文件内容的代码: ./index.js

const   express     =   require('express');
const   http        =   require("http");
const   bodyParser  =   require('body-parser');
const   app         =   express();
const   port        =   process.env.PORT || 9000;
const   gitlabDump  =   require("./controller/GitLabDump");

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true }));

//Make NodeJS to Listen to a particular Port in Localhost
app.listen(port, function(){

        gitlabDump.gitlabDump(type, function(data){
            console.log("Completed Execution for GitLab")
            process.exit();
        })
    
}

我的./controller/GitLabDump.js

const request = require('request');
const https = require('https');
const axios = require('axios');

exports.gitlabDump = function(callback){
    var gitlabAPI = "https://gitlab.com/api/v4/projects/<project_id>/repository/files/tree/<subfolders>/<fileName>/raw?ref=master&private_token=<privateToken>";

    //Make the request to the each file and read its raw contents
    request(gitlabAPI, function(error, response, body) {
        const featureFileData = JSON.parse(JSON.stringify(body)).toString();

        console.log(featureFileData)
        
        for(const match of featureFileData.matchAll("Scenario:")){
            console.log(match);
        }

        callback("Completed");
    })

    
}

我能够打印文件内容。有人可以解释一下如何迭代原始文件内容并获取所有必需的 id 吗?

I have an application developing using Nodejs. This application is making a request to GitLab API and obtaining the raw file data from it.

I would like to read the particular string which is present after another string and get all similar data from it. I am just a bit confused on this part and unable to proceed further can someone please explain to me how to achieve this?

Following is the sample file data:
I would like to read all the numbers if present after the keyword Scenario: i.e in this case I would like to get A001-D002 & K002-M002. These numbers can be anything random and can appear anywhere within the file content. I would like to read them and store them within an array for that particular file.

FileName: File Data
  Background:
    This is some random background

  Scenario: A001-D002 My first scenario
    Given I am sitting on a plane
    When I am offered drinks

  Scenario: K002-M002 My second scenario
    Given when I book the uber taxi
    When I get the notifications

I am not understanding how to iterate over the file content and read every word and match and accordingly obtain the ids.

Following is the code that I have which makes the request to GitLab and obtains the raw file content:
./index.js:

const   express     =   require('express');
const   http        =   require("http");
const   bodyParser  =   require('body-parser');
const   app         =   express();
const   port        =   process.env.PORT || 9000;
const   gitlabDump  =   require("./controller/GitLabDump");

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true }));

//Make NodeJS to Listen to a particular Port in Localhost
app.listen(port, function(){

        gitlabDump.gitlabDump(type, function(data){
            console.log("Completed Execution for GitLab")
            process.exit();
        })
    
}

My ./controller/GitLabDump.js:

const request = require('request');
const https = require('https');
const axios = require('axios');

exports.gitlabDump = function(callback){
    var gitlabAPI = "https://gitlab.com/api/v4/projects/<project_id>/repository/files/tree/<subfolders>/<fileName>/raw?ref=master&private_token=<privateToken>";

    //Make the request to the each file and read its raw contents
    request(gitlabAPI, function(error, response, body) {
        const featureFileData = JSON.parse(JSON.stringify(body)).toString();

        console.log(featureFileData)
        
        for(const match of featureFileData.matchAll("Scenario:")){
            console.log(match);
        }

        callback("Completed");
    })

    
}

I am able to print the file contents. Can someone please explain me how can I iterate over the raw file contents and get all the required ids?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别理我 2025-01-16 19:39:47

我建议您使用一种方法,通过迭代每一行来分析字符串的每个部分(我假设您的字符串是像您的示例中那样组成的)。与使用正则表达式相比,它更容易理解和编码。

下面的示例代表您的请求回调函数。
我将代码分为 3 个逻辑:

  • 搜索文件名
  • 搜索我们感兴趣的行(“场景”单词)
  • 通过过滤器函数提取 ID

之后,您可以轻松更改 ID 过滤器(txt.substr(0, txt.indexOf(' ')) 使用更正确的表达式来提取句子,

结果将发送到回调函数,第一个参数是文件名,第二个参数是所有 id,就像您在示例中所做的那样。 。

((callback) => {

    const featureFileData = `FileName: File Data
      Background:
        This is some random background

      Scenario: A001-D002 My first scenario
        Given I am sitting on a plane
        When I am offered drinks

      Scenario: K002-M002 My second scenario
        Given when I book the uber taxi
        When I get the notifications`;

    // find "filename"
    const filenames = featureFileData.split('\n')
        .filter(line => line.trim().substr(0,8) === 'FileName')
        .map((raw) => {
            if(!raw) return 'unknown';
            const sentences = raw.trim().split(':');
            if(sentences[1] && sentences[1].length) {
                return sentences[1].trim();
            }
        });

    // filter the "Scenario" lines
    const scenarioLines = featureFileData.split('\n')
        .map((line) => {
            if(line.trim().substr(0,8) === 'Scenario') {
                const sentences = line.trim().split(':');
                if(sentences[1] && sentences[1].length) {
                    return sentences[1].trim();
                }
            }
            return false;
        })
        .filter(r => r !== false);

    // search ids
    const ids = scenarioLines.map(txt => txt.substr(0, txt.indexOf(' ')));

    callback(filenames[0], ids);

})(console.log)

I suggest you to use a method by analyzing each part of your string by iterating over each lines (i assume that your string is compose like in your exemple). It is easier to understand and coding it than using a regex.

The exemple below represent your request callback function.
I split the code in 3 logics :

  • search the filename
  • search the line we are interesting with ("Scenario" word)
  • extract the ID by filter function

You can after that, easily change you ID filter (txt.substr(0, txt.indexOf(' ')) to use a more proper expression to extract your sentence.

The result is sent to a callback function with as first argument the filename, and as second all ids. Like you did in your exemple.

((callback) => {

    const featureFileData = `FileName: File Data
      Background:
        This is some random background

      Scenario: A001-D002 My first scenario
        Given I am sitting on a plane
        When I am offered drinks

      Scenario: K002-M002 My second scenario
        Given when I book the uber taxi
        When I get the notifications`;

    // find "filename"
    const filenames = featureFileData.split('\n')
        .filter(line => line.trim().substr(0,8) === 'FileName')
        .map((raw) => {
            if(!raw) return 'unknown';
            const sentences = raw.trim().split(':');
            if(sentences[1] && sentences[1].length) {
                return sentences[1].trim();
            }
        });

    // filter the "Scenario" lines
    const scenarioLines = featureFileData.split('\n')
        .map((line) => {
            if(line.trim().substr(0,8) === 'Scenario') {
                const sentences = line.trim().split(':');
                if(sentences[1] && sentences[1].length) {
                    return sentences[1].trim();
                }
            }
            return false;
        })
        .filter(r => r !== false);

    // search ids
    const ids = scenarioLines.map(txt => txt.substr(0, txt.indexOf(' ')));

    callback(filenames[0], ids);

})(console.log)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文