0

I tried to read line by line in file using node js.but I have doubt how to get only condition content (if,for) using node js.

data.js

const fs = require('fs')
const path = require("path");
const file_path = path.resolve('./sample')
const check_console = require('./tests_helper')

check_console.files_paths(file_path).then(result =>console.log(result))
                                    .catch(error =>console.log(error))

file.js

var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(files[i])
});

lineReader.on('line', function (line) {
console.log("Line of Code "+line)
});

I got output
Line of Code console.log('hai')
Line of Code
Line of Code console.log('hai')
Line of Code
Line of Code if(10 == 10)
Line of Code {
Line of Code
Line of Code }

but I want Output
Line of Code if(10 == 10)
Line of Code {
Line of Code
Line of Code }

Neha
  • 2,136
  • 5
  • 21
  • 50
smith hari
  • 437
  • 1
  • 11
  • 22
  • Possible duplicate of [Read a file one line at a time in node.js?](https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js) – Ayush Kumar May 14 '19 at 06:44
  • 2
    That is not so simple. You have to parse every line and determine if it includes `if` and parse it forward until the end of said `if`. BUT if you have string "something if something" - you should ignore that. That might be a job for regex, but anyway it's pretty tedious job and not a one line answer for sure – Mike B May 14 '19 at 06:52
  • @AyushKumar I will check it but.I want pariticular lines of code its any conditions (if,for,function) occur in files.I need that code only.any one give solution – smith hari May 14 '19 at 06:54
  • I agree with @Mikelis .Regex would be helpful here. @SmithHari Does/Will the `If` statement have sub conditions or loops inside it? Want to know for curly braces inside `if`. – Ayush Kumar May 14 '19 at 07:02
  • @Smith Did you solve it? Post the answer if you did. – Ayush Kumar May 14 '19 at 07:23
  • no I working on it .once I got the answer I will post it – smith hari May 14 '19 at 08:19
  • have you tried my solution? – Wellwisher May 14 '19 at 10:50

1 Answers1

0

You can use "string".includes("substring"); method to check if that substring exists or not. I have prepared a quick logic for your problem. line.includes("if") will check if a substring exists and some other condition check I have given to maintain the curly braces.

const fs = require('fs');
const readline = require('readline');

const r1 = readline.createInterface({
    input: require('fs').createReadStream(__dirname + '/test.js')
});
found = false;
curlbrace = 0;
r1.on('line', function (line) {
    if (line.includes("if")) {
        found = true; //setting flag true if found search string
    }
    if (found && line.includes("{")) {
        curlbrace++; //incrementing if breace is started
    }
    if (found && line.includes("}")) {
        curlbrace--; //decrementing if brace is ended
    }
    if (found && curlbrace >= 0) {
        console.log(line); //console the searched result
        if (curlbrace == 0) {
            found = false; //setting false to prevent it to show other contents
        }
    }
});
Wellwisher
  • 472
  • 2
  • 8