3.0.0 • Published 3 years ago

haye v3.0.0

Weekly downloads
14,603
License
MIT
Repository
github
Last release
3 years ago

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
  1. Each item is separated by |
  2. The values are defined after :
  3. Multiple values are separated by ,.
  4. 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]
  1. Each item is separated by ,
  2. The values are defined after =
  3. Multiple values are separated by , inside [].
  4. 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' ]
}