# an-expression

> Represent simple javascript programs in JSON

Latest version **0.57.0** (published 2018-04-28) · 0 weekly downloads

## Install

```sh
npm install an-expression
pnpm add an-expression
yarn add an-expression
bun add an-expression
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.57.0 |
| Published | 2018-04-28 |
| First published | 2017-03-06 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 168.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | erikpukinskis |

## Links

- npm: https://www.npmjs.com/package/an-expression
- Repository: https://github.com/erikpukinskis/an-expression
- Homepage: https://github.com/erikpukinskis/an-expression#readme
- Issues: https://github.com/erikpukinskis/an-expression/issues
- npm.io page: https://npm.io/package/an-expression

## Dependencies (3)

- [forkable-list](https://npm.io/package/forkable-list.md) *
- [function-call](https://npm.io/package/function-call.md) *
- [module-library](https://npm.io/package/module-library.md) *

## Recent versions

- 0.57.0 (latest) — 2018-04-28
- 0.56.0 — 2018-04-22
- 0.55.0 — 2018-04-22
- 0.54.0 — 2018-04-22
- 0.53.0 — 2018-03-12
- 0.52.0 — 2018-03-11
- 0.51.0 — 2018-02-19
- 0.50.0 — 2018-02-18
- 0.49.0 — 2018-02-18
- 0.48.0 — 2018-02-18
- 0.47.0 — 2017-12-17
- 0.46.0 — 2017-11-07
- 0.45.0 — 2017-10-25
- 0.44.0 — 2017-09-30
- 0.43.0 — 2017-06-07
- … 42 more at https://npm.io/package/an-expression/versions

## README

**anExpression** lets you programmatically build JavaScript expressions, modify them, and then turn them into source code.

```javascript
var anExpression = require("an-expression")
var javascriptToEzjs = require("javascript-to-ezjs")

var tree = anExpression.tree()

javascriptToEzjs("blah({\n  a: 1,})", tree)
```

Once we have built a tree, we can query the data. Here we'll iterate through the key value pairs on that object expression, and expect to see the number 1:

```javascript
var objectExpressionId = tree.getListItem("arguments", tree.rootId(), 0)

tree.eachListItem(
  "pairIds",
  objectExpressionId,
  function(pairId) {
    var key = tree.getAttribute(tree, "key", pairId)
    var parentId = tree.getAttribute("key", pairId)
    var valueId = tree.getAttribute("valueId", pairId)
    var number = tree.getAttribute("number", valueId)

    if (number != 1) {
      throw new Error("Bad number") }

    if (parentId != tree.root().id) {
      throw new Error("Bad root")}})
```

We can also modify the tree:

```javascript
tree.addKeyValuePair(
  objectExpressionId,
  "b",
  tree.numberLiteral(2))
```

And then dump the modified code:

```javascript
// tree.toJavaScript() returns:
blah({
  a: 1,
  b: 2})
```

## Methods

```javascript
// Adds an empty function literal at the next available position
tree.addExpressionAt(
  tree.reservePosition(),
  anExpression.functionLiteral())

// Add a string literal inside that function literal
var functionId = tree.rootId()
var stringLiteral = anExpression.stringLiteral("alert")
tree.addToParent(functionId, stringLiteral)
```

## Expression generators

```javascript

var hello = anExpression.stringLiteral("hello, world")

// "hello, world"

anExpression.functionLiteral({
  functionName: "add",
  argumentNames: ["a", "b"],
  body: hello})

// function add(a, b) {
//   "hello, world"
// }

var a = anExpression.numberLiteral(42)

// 42

var b = anExpression.numberLiteral(1)

// 1

anExpression.functionCall({
  functionName: "add",
  arguments: [a, b]})

// add(42, 1)

anExpression.variableReference("a")

// a

anExpression.variableAssignment({
  variableName: "a",
  expression: "b"})

// a = b

anExpression.variableAssignment({
  variableName: "c",
  expression: "b",
  isDeclaration: true})

// var c = b

anExpression.objectLiteral({
  name: a.id
})

// {name: a}

anExpression.arrayLiteral([a.id, b.id])

// [a, b]

anExpression.boolean(true)
anExpression.true()

// true

anExpression.false()

// false

anExpression.returnStatement({
  expression: a})

// return a

```

---
_Source: https://npm.io/package/an-expression · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
