Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type checking for Hash values? #158

Closed
mouhannad-sh opened this issue Sep 15, 2019 · 4 comments
Closed

type checking for Hash values? #158

mouhannad-sh opened this issue Sep 15, 2019 · 4 comments
Labels

Comments

@mouhannad-sh
Copy link

I have a custom tag

{% tag name_1 | { default:  true } %}
{% tag name_2 | { default: "true" } %}

the definition I have is like this

parse(tagToken) {
      const { default: _default, ...rest } = Hash.parse(tagToken.args);
      this.defaultValue = _default;
	  console.log(_default) 
}

current output:

'true' 
'"true"'

expected

true // as boolean
"true" // as string

Am I doing something wrong? is there a better way to do this ?

@harttle
Copy link
Owner

harttle commented Sep 16, 2019

Try Hash.create or Hash.createSync, what they do is parse and evaluate each expression. See: https://harttle.land/liquidjs/classes/_template_tag_hash_.hash.html

@mouhannad-sh
Copy link
Author

it seems to be exactly what I needed 🎉

I changed it to the following

async parse(tagToken, context) {
      const { default: _default, ...rest } = await Hash.create(tagToken.args, context);
      this.defaultValue = _default;
      console.log(_default, { type: typeof _default }); 
}

Input

{% tag name_1 | { default: true } %}
{% var name_2 | { default: "true" } %}
{% var name_3 | { default: '"true"' } %}

Output 🔥

true { type: 'boolean' }
true { type: 'string' }
"true" { type: 'string' }

many thanks !

@harttle harttle closed this as completed Oct 7, 2019
@mouhannad-sh
Copy link
Author

@harttle it seems like Hash.create doesn't work anymore on the latest version (9.14.1) was working on 9.0.0

is it possible to achieve the same thing using the new API ?

thanks

harttle pushed a commit that referenced this issue Aug 4, 2020
# [9.15.0](v9.14.1...v9.15.0) (2020-08-04)

### Features

* export toPromise and toValue, see [#158](#158) ([2e5ab98](2e5ab98))
@harttle
Copy link
Owner

harttle commented Aug 4, 2020

It's still there but a bit different, I exported a toPromise (see the above commit) for ease of use.

const { Context, Hash, toPromise } = require('liquidjs')

const hash = new Hash('foo:bar')
const ctx = new Context({bar: 'BAR'})

// Outputs:
// { foo: 'BAR' }
toPromise(hash.render(ctx)).then(console.log)
// or `toValue` to render syncly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants