'How to extract from export to ; with JavaScript

I have following codes in a file. Please note that VS code inserts a new line after export let theadClass = when I save the file.

<script lang="ts">
    export let header: Array<string>;
    export let divClass: string = 'relative overflow-x-auto shadow-md sm:rounded-lg';
    export let tableClass: string = 'w-full text-sm text-left text-gray-500 dark:text-gray-400';
    export let theadClass: string =
        'text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400';
</script>

<div class={divClass}>
// more after this line ...
</div>

Ideally I'd like to extract as following to create a prop table. But it is ok if I extract strings between export to ; as well.

header: Array<string>;
divClass:string = 'relative overflow-x-auto shadow-md sm:rounded-lg';
tableClass:string = 'w-full text-sm text-left text-gray-500 dark:text-gray-400';
theadClass:string = 'text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400';

When I use the following funciton:

import * as fs from 'fs';

export function getLines(fileName, keyword) { 
  let outputs =[];
  const file = fs.readFileSync(fileName, {encoding: 'utf-8'});
  let arr = file.split(/\r?\n/);
  arr.forEach((line, idx) => {
    if(line.includes(keyword)){
      outputs.push(line);
    }
  });
  return outputs
}

I get the following:

 [
  '\texport let header: Array<string>;',
  "\texport let divClass: string = 'relative overflow-x-auto shadow-md sm:rounded-lg';",
  "\texport let tableClass: string = 'w-full text-sm text-left text-gray-500 dark:text-gray-400';",
  '\texport let theadClass: string ='
]

As you can see it, the last line is not complete.

How can I extract lines that have export to ;?



Solution 1:[1]

I'm guessing the problem is the newline that VSCode inserts newline automatically. Would it be viable for you to have a .prettierrc with a larger printWidth? If not, add //prettier-ignore before the last line?

Or alternatively, split the file using ; instead of \n in let arr = file.split(/\r?\n/);.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cSharp