haye
v3.0.0
Published
Haye is a super fast string based expression parser for Node.js
Downloads
123,277
Maintainers
Readme
Haye
Haye is a simple super fast string expression parser. In support pipe
and qs
string expressions ( explained below ).
Limitations The keys/values inside the expression cannot have any of the reserved keywords, otherwise parser will mis-behave.
Pipe expression
The pipe based expression is very popular in Laravel community, due to their Validation engine, and same is adopted by Indicative.
Syntax example:
required|email|max:4|range:10,30
- Each item is separated by
|
- The values are defined after
:
- Multiple values are separated by
,
. - White spaces in keys are trimmed.
Qs expression
The query string expression is almost similar to the URL query string, with couple of small modifications to make the expression readable.
Syntax example:
required,email,max=4,range=[1, 10]
- Each item is separated by
,
- The values are defined after
=
- Multiple values are separated by
,
inside[]
. - White spaces in keys are trimmed.
Installation
The module is available on npm
npm i haye
# yarn
yarn add haye
Usage
Below is the bunch of usage examples
Pipe -> Array
const haye = require('haye')
const expression = 'required|email:unique,users'
const parsed = haye.fromPipe(expression).toArray()
Output
[
{ name: 'required', args: [] },
{ name: 'email', args: ['unique', 'users'] }
]
Pipe -> JSON
const haye = require('haye')
const expression = 'required|email:unique,users'
const parsed = haye.fromPipe(expression).toJSON()
Output
{
required: [],
email: [ 'unique', 'users' ]
}
Qs -> Array
const haye = require('haye')
const expression = 'required,email=[unique,users]'
const parsed = haye.fromQS(expression).toArray()
Output
[
{ name: 'required', args: [] },
{ name: 'email', args: ['unique', 'users'] }
]
Qs -> JSON
const haye = require('haye')
const expression = 'required,email=[unique,users]'
const parsed = haye.fromQS(expression).toJSON()
Output
{
required: [],
email: [ 'unique', 'users' ]
}