JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 831
  • Score
    100M100P100Q98211F
  • License MIT

A react renderer for ASTs generated by equation-parser/equation-resolver

Package Exports

  • react-equation

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-equation) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-equation – Parse, evaluate and render equation from plain text

A react render-layer for equation-parser and equation-resolver.

Quick-start

Install the package. Optionally add equation-resolver as a direct dependency, if you want functions and variables.

npm install -S react-equation equation-resolver

or

yarn add react-equation equation-resolver

Start rendering equations

import React from 'react'
import ReactDOM from 'react-dom'
import { Equation, EquationEvaluate, EquationOptions } from 'react-equation'
import { defaultVariables, defaultFunctions } from 'equation-resolver'

ReactDOM.render((
    <>
        <div>
            <Equation
                value='5m + 1/2m * sin(π) + (22 m^2) / (2m)'
            />
        </div>
        <div>
            <EquationEvaluate
                value='5m + 1/2m * sin(π) + (22 m^2) / (2m)'
            />
        </div>
        <div>
            <EquationEvaluate
                value='5m + 1/2m * sin(π) + (22 m^2) / (2m)'
                variables={defaultVariables}
                functions={defaultFunctions}
            />
        </div>
        <EquationOptions
            variables={defaultVariables}
            functions={defaultFunctions}
        >
            <div>
                <EquationEvaluate
                    value='5m + 1/2m * sin(π) + (22 m^2) / (2m)'
                />
            </div>
        </EquationOptions>
    </>
), const rootElement = document.getElementById("root"));

Components

All the included components are split up in the interest of allowing tree-shaking to reduce the bundle-size by avoiding either the parser, the resolver or both, depending on needs.

All the components can have variables, functions, simplifyableUnits, errorHandler, className and style. These props can also be passed along through the EquationOptions context-provider.

Equation

Parse the string provided as value and render it. No evaluation is done.

Example:

<Equation
    value='2 + a'
/>
// Renders a prettified 2 + a

EquationEvaluate

Parse the string provided as value, evaluate it and render the formatted equation.

Example:

<EquationEvaluate
    value='2 + a'
    variables={{ a: { type: 'number', value: 5 }}}
/>
// Renders a prettified 2 + a = 7

EquationPreparsed

Render a pre-parsed equation provided as value. No evaluation is done.

Example:

<EquationPreparsed
    value={{ type: 'plus', a: { type: 'number', value: '2' }, b: { type: 'variable', name: 'a' } }}
/>
// Renders a prettified 2 + a

EquationEvaluatePreparsed

Evaluate a pre-parsed equation provided as value and render the formatted equation.

Example:

<EquationEvaluatePreparsed
    value={{ type: 'plus', a: { type: 'number', value: '2' }, b: { type: 'variable', name: 'a' } }}
    variables={{ a: { type: 'number', value: 5 }}}
/>
// Renders a prettified 2 + a = 7

Error handling

An error handler should be added either to the Equation-components or to EquationOptions, otherwise the raw errorType of the error is shown. If english errors suffice, simply import defaultErrorHandler. If custom errors (for instance for localisation), see ./src/errorHandler.ts for easy implementation.