# jsaml

> An object literal parser with yaml-like syntax

Latest version **1.0.6** (published 2017-04-16) · GPL license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2017-04-16 |
| First published | 2016-08-03 |
| Weekly downloads | 0 |
| License | GPL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | pistacchio@gmail.com |
| Maintainers | pistacchio |
| Keywords | yaml, parser, serializer, object literal, es6 |

## Links

- npm: https://www.npmjs.com/package/jsaml
- Homepage: https://github.com/pistacchio/jsaml
- npm.io page: https://npm.io/package/jsaml

## Dependencies (3)

- [chai](https://npm.io/package/chai.md) ^3.5.0
- [mocha](https://npm.io/package/mocha.md) ^3.2.0
- [js-yaml](https://npm.io/package/js-yaml.md) ^3.6.1

## Alternatives

- [monaco-yaml](https://npm.io/package/monaco-yaml.md) — 420.1K weekly downloads
- [@crewx/workflow](https://npm.io/package/@crewx/workflow.md) — 3.1K weekly downloads
- [yaml-cat](https://npm.io/package/yaml-cat.md) — 38 weekly downloads
- [nunjucks-in-yaml](https://npm.io/package/nunjucks-in-yaml.md) — 9 weekly downloads
- [shopify-symlinks](https://npm.io/package/shopify-symlinks.md) — 3 weekly downloads

## Recent versions

- 1.0.6 (latest) — 2017-04-16
- 1.0.5 — 2017-04-16
- 1.0.4 — 2017-04-16
- 1.0.3 — 2016-08-03
- 1.0.0 — 2016-08-03

## README

# Jsaml

Jsaml is [YAML](http://yaml.org/) extension for javascript that allows to define functions, getters, setters and include other Jsaml files converting the Jsaml source file into a ES6 object literal.

[Jsaml Homepage](https://github.com/pistacchio/jsaml)

## Usage

```javascript
const jsaml = require('jsaml');

const jsamlString = `
object:
    inlineFunction: (arg) return arg;`;

const myObj = jsaml.load(jsamlString);

const myObjFromFile = jsaml.load('/path/to/my/jsamlFile.jsaml');
```

## Functions

To declare an inline function:

```javascript
object:
    inlineFunction: (arg) return arg;
```

This will be converted to:

```javascript
{
    object: {
        inlineFunction: function (arg) {
            return arg;
        }
    }
}
```

Functions can also be multiline:

```javascript
object:
    multilineFunction: (arg)
        const argTimesTwo = arg * 2;
        return arg;
```

This will be converted to:

```javascript
{
    object: {
        multilineFunction: function (arg) {
            let argTimesTwo = arg * 2;
            return arg;
        }
    }
}
```

This works for arrays as well:

```javascript
object:
    someArray:
        - (arg)
            const argTimesTwo = arg * 2;
            return arg;
```

### Redefine function syntax

If you don't want to use "()" to signal a function, you can pass different opening / closing signals via the option dictionary.


```javascript
const jsamlString = `
object:
    inlineFunction: {{arg]] return arg;`;

const myObj = jsaml.load(jsamlString, options: {
    open_funcion_bracket:  '{{',
    close_funcion_bracket: ']]'    
});
```
## Getters and setters

If an object has only a "get" function, or a "set" function or both, they'll be converted to setters and getters

```javascript
const jsamlString = `
object:
    property:
        get: () return this.prop
        set: (value) this.prop = value * 2`;

const myObj = jsaml.load(jsamlString);

myObj.property = 21;
console.log(myObj.property); // 42
```

### Redefine getters and setters syntax

If you don't want to use "get" and "set", you can pass different opening / closing signals via the option dictionary.

```javascript
const jsamlString = `
object:
    property:
        myGet: () return this.prop
        mySet: (value) this.prop = value * 2`;

const myObj = jsaml.load(jsamlString, options: {
    get_property: 'myGet',
    set_property: 'mySet'
});
```

## Include files

You can also include external Jsaml files:

```javascript
/* myOtherFile.jsaml

externalFile:
    a: 1
    b:
        - 2
        - 3
*/

object:
    includedProperty: @include myOtherFile.jsaml

```

This will be loaded as:

```javascript
{
    object: {
        includedProperty: {
            externalFile: {
                a: 1,
                b: [2, 3]
            }
        }
    }
}
```

### Redefine include syntax

If you don't want to use "@include", you can pass different opening / closing signals via the option dictionary.

```javascript
const jsamlString = `
object:
    property: @myInclude /path/to/file.jsaml

const myObj = jsaml.load(jsamlString, options: {
    include_signal: '@myInclude'
});
```

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