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'";
3
  • 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. Commented Sep 19, 2019 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 ? Commented Sep 19, 2019 at 15:14
  • @ssc-hrep3 asked another question related to second point stackoverflow.com/questions/58014275/… Commented Sep 19, 2019 at 15:22

3 Answers 3

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));

Sign up to request clarification or add additional context in comments.

4 Comments

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!
@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 ?
added a snippet
@Vandesh asked another question to my second point if you can help stackoverflow.com/questions/58014275/…
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))

Comments

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}' ` 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.