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

Parse String to Int #145

Closed
ottokruse opened this issue Jan 14, 2022 · 2 comments · Fixed by #146
Closed

Parse String to Int #145

ottokruse opened this issue Jan 14, 2022 · 2 comments · Fixed by #146

Comments

@ottokruse
Copy link

ottokruse commented Jan 14, 2022

First of all, great library, thank you for building it.

This VTL is not supported:

#set($mystring="123")
#set($Integer=1)
#set($myint=$Integer.parseInt($mystring))

(after some googling found this is a way to parse a String to an Int)

However was able to make it work, by doing this, before running the test:

Number.prototype.parseInt = parseInt

Do you maybe have a better recommendation?

@shepherdwind
Copy link
Owner

2.0.6 published. see example

it('support customMethodHandlers config', () => {
var vm = `#set($mystring="123")
#set($Integer=1)
#set($myint=$Integer.parseInt($mystring))
$myint
`;
const customMethodHandlers = [
{
uid: 'parseInt',
match: function({ property, context }) {
return (typeof context === 'number') && property === 'parseInt';
},
resolve({ params }) {
return parseInt(params[0]);
},
}
];
var compile = new Compile(parse(vm), {
customMethodHandlers
});
var ret = compile.render(context)
assert.equal(compile.context.myint, 123)
assert.equal('123', ret.trim())
});

@ottokruse
Copy link
Author

Awesome! Thanks for the quick turn around 🎉

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

Successfully merging a pull request may close this issue.

2 participants