# liquid-args

> A parser for custom tag arguments in liquidjs. Meant to mirror Nunjucks.

Latest version **1.1.0** (published 2023-04-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install liquid-args
pnpm add liquid-args
yarn add liquid-args
bun add liquid-args
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-04-11 |
| First published | 2020-06-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 27.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Dylan Awalt-Conley |
| Maintainers | dawaltconley |
| Keywords | liquid, pegjs, parser, tags |

## Links

- npm: https://www.npmjs.com/package/liquid-args
- Repository: https://github.com/dawaltconley/liquid-args
- Homepage: https://github.com/dawaltconley/liquid-args#readme
- Issues: https://github.com/dawaltconley/liquid-args/issues
- npm.io page: https://npm.io/package/liquid-args

## 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

- 1.1.0 (latest) — 2023-04-11
- 1.0.0 — 2021-04-10
- 0.3.2 — 2020-06-24
- 0.3.1 — 2020-06-16
- 0.3.0 — 2020-06-16
- 0.2.0 — 2020-06-16
- 0.1.1 — 2020-06-16
- 0.1.0 — 2020-06-15

## README

# Liquid Arguments Parser

This module exists to parse the arguments string of a [custom Liquid JS tag](https://liquidjs.com/tutorials/register-filters-tags.html), though it does not depend on `liquidjs` and could also be used in other contexts.

## Basic Usage

```javascript
const parser = require(liquid-args);

parser(`foo "bar" 42`);

// [ 'foo', '"bar"', '42' ]
```

It supports key/value arguments, which it returns in an object as a last argument. The output mimics the standard arguments for custom Nunjucks tags.

```javascript
parser(`first_name last_name age=68 height="5' 7\\""`);

// [ 'first_name',
//   'last_name',
//   { __keywords: true, age: '68', height: '"5\' 7\\""' } ]
```

The parser also takes a function as an optional second argument, which will evaluate each value before returning the arguments.

```javascript
const { Liquid } = require('liquidjs');
const engine = new Liquid();

const evalFunc = arg => engine.evalValueSync(arg, /* some context */)

parser(`foo "bar" 42`, evalFunc);

// [ fooValue, 'bar', 42 ]
```

If the function is async or returns a Promise, then the parser will return an array of Promises, which each resolve to the corresponding evaluated argument (or keywords arg object).

## Example

Here's an example of the parser being used to create a custom `liquidjs` tag:

```javascript
const parser = require('liquid-args');
const { Liquid } = require('liquidjs');
const engine = new Liquid();

engine.registerTag('jsonify', {
    parse: function (tagToken) {
        this.args = tagToken.args;
    },
    render: async function (ctx, emitter, hash) {
        const evalValue = arg => this.liquid.evalValue(arg, ctx);
        this.args = parser(this.args, evalValue);
        return JSON.stringify(await Promise.all(this.args));
    }
});
```

### TypeScript 

When using TypeScript, the parser optionally accepts two generics, one defining positional arguments and another defining an object of keyword arguments.

```typescript
import parser from 'liquid-args';

let args: string[] = parser(`foo "bar" 42`);

let kwargs = parser<[string, string], { age: string, height: string }>(
  `first_name last_name age=68 height="5' 7\\""`
);

// pass a function if you want to specify non-string return types
let evaluated = parser<[], { age: number, height: string }>(
  `age=68 height="5' 7\\""`,
  (arg: string) => Number(arg) || arg
);
```

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