Skip to main content

parse-repr

A lightweight Python __repr__ parser for JavaScript.

Download#

npm Package parse-repr

To install the library, run:

npm install parse-repr

Usage#

To use the library, you will first need to put this at the top of any Python files that you want to use the lowercase values in:

// with es6+ importimport ParseRepr from "parse-repr"
// or, with requirevar ParseRepr = require("parse-repr")

and that is basically it, you can now use it.

API#

constructor(repr: string)#

Basic constructor.

Argument repr (string): The unparsed repr.

Throws Error: If you don't pass a repr or pass undefined.

parse()#

Parses the repr. This is automatically called by the constructor and should only be called if you change the repr.

getObjectTypeName(): string#

Get the name of the object type (e.g. in "<User me>", the object type would be "User").

Returns: The name of the object type

getUnparsedRepr(): string#

Returns: The unparsed repr.

getParts(): string[]#

Get the repr parts. In the case your repr looks like "<User 123456 Billy>", ["123456", "Billy"] would be returned.

In the case your repr looks like "<User id=123456 firstName=Billy>", ["id=123456", "firstName=Billy"] would be returned.

If you want actual key-value pairs for this, see getKeyValuePairs().

Returns: The list of elements in the repr.

getKeyValuePairs(): KeyValueObjects#

Gives you an object with the key-value pairs named in the repr.

Returns: An object as described above.

Throws Error: If the repr doesn't use key-value pairs.

interface KeyValueObjects {    [key: string]: string}

Source#

The source can be found on GitHub.