-3

Given a string I need to create an object and convert that string to properties.

So given this:

"anchor: true,
text: 'primary button',
style: 'primary'
"

I need to generate a real object:

{
     anchor: true,
     text: 'primary button',
     style: 'primary'
}

Any help on this of course greatly appreciated.

busyPixels
  • 366
  • 1
  • 5
  • 18

2 Answers2

0

This depends on the markdown format. If it is Yaml (which it looks like), you will need to use a yaml parser such as js-yaml (https://github.com/nodeca/js-yaml).

John
  • 31
  • 2
0

Thanks to @dandavis for posting this answer in a comment:

anObject=eval("0||{"+yourString+"}")
busyPixels
  • 366
  • 1
  • 5
  • 18
  • 1
    I couldn't bring myself to put eval() in an answer, and i would probably get beat up if i did. A lot of people don't like eval for a lot of reasons, most of them trivial, but some worth considering. I would say that as long as you don't eval() user input, and you don't need to run with "es3 compact profile"-level support (ala chrome extensions), eval() will be the simplest way to do what you want with controlled input. – dandavis Jan 13 '15 at 18:52