0

Trying to create text string from interface to display into UI , Below code is creating separate file for each interface , How do i creat single file as i have given in the expected.

Also is there way to create UI to display typescript code as a sample instead of using text string ?

main.js

var path = "/filepath"
var data = [
  {
        name: "IParam"
    },
    {
        name: "IError"
    }
]
  function createInterfaces(path, data) {
        var _text = 'import { ';
        $.each(data,function(id,val){
            _text +=  val.name + '}' + 'from ' + path + ';\n\n';

        });
        return _text;
    }

Expected Result should be

"import { IParam, IError} from '/filePath'";
hussain
  • 6,587
  • 18
  • 79
  • 152
  • You need to clarify. And it seems very wrong to build a JavaScript import statement as a String *with* JavaScript. But maybe you can clarify. – ssc-hrep3 Sep 19 '19 at 14:51
  • thats why i asked in second point , is there way to build TS using JS code , goal is to have this code on UI so user can compile and use it online playground , any better approach ? – hussain Sep 19 '19 at 15:14
  • @ssc-hrep3 asked another question related to second point https://stackoverflow.com/questions/58014275/how-to-build-typescript-code-in-js-using-a-function – hussain Sep 19 '19 at 15:22

3 Answers3

3

You should move the last bit out of your for loop

function createInterfaces(path, data) {
        var _text = 'import { ';
        $.each(data,function(id,val){
            _text +=  val.name;

        });
        _text += '}' + 'from ' + path + ';\n\n'
        return _text;
    }

Another way to do it would be -

function createInterfaces(path, data){
 const imports  = data.map(d => d.name).join(', ');
 return `import { ${imports} } from '${path}';\n\n`;
}

Here's the snippet to check -

var path = "/filepath"
var data = [
  {
        name: "IParam"
    },
    {
        name: "IError"
    }
]

function createInterfaces(path, data){
 const imports  = data.map(d => d.name).join(', ');
 return `import { ${imports} } from '${path}';\n\n`;
}

console.log(createInterfaces(path, data));
Vandesh
  • 6,368
  • 1
  • 26
  • 38
  • Nice. Shows the improvement OP needs to make to existing code, AND shows an alternate method using map. The only thing that would make this better is to use runnable snippets! – random_user_name Sep 19 '19 at 15:00
  • @Vandesh Thank you thats what i was exactly looking , Any suggestion to make this code compile able once we have all required typescript data , putting all sample code together so user can test like online playground ? – hussain Sep 19 '19 at 15:12
  • added a snippet – Vandesh Sep 19 '19 at 15:22
  • @Vandesh asked another question to my second point if you can help https://stackoverflow.com/questions/58014275/how-to-build-typescript-code-in-js-using-a-function – hussain Sep 19 '19 at 15:22
0

You should call Array.prototype.map on the data array and return each name, then join the results.

var path = "/filepath"
var data = [ { name: "IParam" }, { name: "IError" } ]

function createInterfaces(path, data) {
  return "import { " + data.map(d => d.name).join(', ') + " } from '" + path + "';\n\n"
}

console.log(createInterfaces(path, data))
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

You can use the array.map to create the string and assign teh result to text variable.

text += `import { ${data.map(a => a.name).toString()} } from '${path}' ` 
nircraft
  • 8,242
  • 5
  • 30
  • 46