Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dawaltconley/liquid-args
An arguments parser for custom liquidjs tags.
https://github.com/dawaltconley/liquid-args
javascript liquid nunjucks
Last synced: about 9 hours ago
JSON representation
An arguments parser for custom liquidjs tags.
- Host: GitHub
- URL: https://github.com/dawaltconley/liquid-args
- Owner: dawaltconley
- Created: 2020-06-15T17:22:50.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-04-11T00:54:57.000Z (almost 2 years ago)
- Last Synced: 2024-12-15T03:20:19.598Z (27 days ago)
- Topics: javascript, liquid, nunjucks
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/liquid-args
- Size: 79.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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
);
```