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

define own filters #371

Closed
emilos opened this issue Dec 7, 2018 · 1 comment
Closed

define own filters #371

emilos opened this issue Dec 7, 2018 · 1 comment
Labels
enhancement New feature or request

Comments

@emilos
Copy link
Contributor

emilos commented Dec 7, 2018

{"foo" | bar}

where bar would be a function that was passed as an option to the compiler.

await compile('{"foo" | bar}', { filters: { bar (value) { return value + "bar" } })
@emilos emilos added the enhancement New feature or request label Dec 7, 2018
@pkonieczniak
Copy link
Contributor

pkonieczniak commented Apr 4, 2019

There is no problem when custom filter is named:

  let template = await compile('{foo | myFilter}', {
    filters: {
      myFilter: function myFilter (text) {
        return text.toUpperCase()
      } 
    }
  })

In case when someone defined function as anonymous or arrow fn then we cannot create AST.

In first example we received error Function declaration must have a name in this context

filters: {
 myFilter: function () {}
}

For this case we can return a new Function an append the name.

I think that more complex is handling arrow functions.

Babel has some plugins which can be used via node (transform-function-name, plugin-transform-arrow-functions)

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

No branches or pull requests

2 participants