scrapinghub / js2xml

Convert Javascript code to an XML document
MIT License
186 stars 23 forks source link

Extend `make_dict` to handle more generic cases #22

Closed rmax closed 7 years ago

rmax commented 7 years ago

I while ago I made this get_vars(snippet) helper on top of js2xml: https://gist.github.com/rolando/c7ad34cf1f70d114ec64b072581fb1f9

Which is helpful to turn a JS snippet into a python object where you can access JS variables by name and get the parsed values.

Is this a good addition to js2xml?

There are open question, like how to transform anonymous functions var myfunc = function() { var foo = "bar"; } so that you can access to foo value.

rmax commented 7 years ago

Here is an usage example:

>>> import js2xml_utils

>>> code = """
   ...: var FOO['bar'] = [{}, {a: true}];
   ...: var FOO['baz'] = null;
   ...: """

>>> js2xml_utils.get_vars(code)
{'FOO[bar]': [{}, {'a': True}], 'FOO[baz]': None}

>>> js2xml_utils.get_vars(code)['FOO[bar]']
[{}, {'a': True}]

Probably we could start a new project js2dict that depends on js2xml and converts the js code into a lookable dictionary.