The Crystal Programming Language Forum

Haye: Expressive string syntax to Array/Hash and Vice-Versa parser for Crystal. Based on the javascript version

Expressive string syntax to Array/Hash and Vice-Versa parser for Crystal. Based on the javascript version.

Usage

require "haye.cr"

Pipe -> Array

expression = 'required|email:unique,users'
parsed = Haye.fromPipe(expression) # Haye.fromPipe(expression, Haye::PresenterTypes::Array)

Output (Haye::Presenters::Node[])

[
  #<Haye::Presenters::Node:0x7f11fd891e40 
    @name="required", 
    @args=[]
  >, 
  #<Haye::Presenters::Node:0x7f11fd891de0 
    @name="email", 
    @args=["unique", "users"]
  >
]

Pipe -> Hash

expression = 'required|email:unique,users'
parsed = Haye.fromPipe(expression, Haye::PresenterTypes::Hash)

Output (Hash)

{
  "required" => [], 
  "email" => ["unique", "users"]
}
3 Likes

An example for the forum would be nice too :)

1 Like

Done ;)