# onml

> JSONML Library

Latest version **2.1.0** (published 2021-01-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install onml
pnpm add onml
yarn add onml
bun add onml
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2021-01-20 |
| First published | 2015-10-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 231.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 24 |
| Author | Aliaksei Chapyzhenka |
| Maintainers | drom |
| Keywords | jsonml, xml, html, svg |

## Links

- npm: https://www.npmjs.com/package/onml
- Repository: https://github.com/drom/onml
- Homepage: https://github.com/drom/onml#readme
- Issues: https://github.com/drom/onml/issues
- npm.io page: https://npm.io/package/onml

## Dependencies (1)

- [sax](https://npm.io/package/sax.md) ^1.2.1

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2021-01-20
- 2.0.0 — 2021-01-20
- 1.3.0 — 2021-01-19
- 1.2.0 — 2020-01-13
- 1.1.0 — 2019-09-03
- 1.0.0 — 2019-04-18
- 0.5.0 — 2019-04-18
- 0.4.5 — 2018-07-11
- 0.4.4 — 2018-06-08
- 0.4.3 — 2018-06-06
- 0.4.2 — 2018-06-06
- 0.4.1 — 2017-07-17
- 0.4.0 — 2016-12-08
- 0.3.1 — 2016-01-13
- 0.3.0 — 2016-01-13
- … 3 more at https://npm.io/package/onml/versions

## README

# ONML

[![NPM version](https://img.shields.io/npm/v/onml.svg)](https://www.npmjs.org/package/onml)
[![Actions Status](https://github.com/drom/onml/workflows/Tests/badge.svg)](https://github.com/drom/onml/actions)

[jsonml.org](http://www.jsonml.org/) compatible tool set.

## Use
### Node.js

```
npm i onml --save
```

```js
var onml = require('onml');
```

## API
### onml.parse() --- onml.p()
The `onml.parse()` method parses a XML/HTML/SVG string and returns a JavaScript value.

```js
var obj = onml.parse('<text a="5">so me</text>');
console.log(obj);
-->
["text", {a: "5"}, "so me"]
```

### onml.stringify() --- onml.s()
The `onml.stringify(array, [indentation])` method converts a JavaScript value to a XML/HTML/SVG string.

```js
var str = onml.stringify(['text', {a: 55}, 'so me'], 2);
console.log(str);
-->
<text a="55">
  so me
</text>
```

### onml.traverse() --- onml.t()
JSONML object traversal tool. See [test/traverse.js](test/traverse.js) for more details.

```js
onml.traverse(obj, {
    enter: function (node, parent) {
        ...
    },
    leave: function (node, parent) {
        ...
    }
});
```
Inside `enter` and `leave` functions:

`node` and `parent` objects have the following attributes:
  * `.name` -- tag name
  * `.attr` -- attributes object
  * `.full` -- full node array

`this` will hold additional methods:
  * `this.name(string)` -- to change the node tag
  * `this.skip()` -- to skip subtree based on the current node
  * `this.remove()` -- to remove current node
  * `this.replace(array)` -- to replace current node

```js
// count divs on enter
var count = 0;
onml.traverse(
    ['b',
        ['div', {a: true},
            ['span',
                'div',
                ['div',
                    ['div', {},
                        ['div', {a: true}]
                    ]
                ],
                ['div', {},
                    ['div']
                ]
            ]
        ]
    ],
    {
        enter: function (node) {
            if (node.name === 'div') {
                count++;
            }
        }
    }
);
console.log(count);
-->
6
```

## Testing
`npm test`

## License
MIT [LICENSE](https://github.com/drom/onml/blob/master/LICENSE).

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