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

Support custom functions #30

Open
the-g-man opened this issue May 4, 2017 · 3 comments
Open

Support custom functions #30

the-g-man opened this issue May 4, 2017 · 3 comments

Comments

@the-g-man
Copy link

Is there a timeframe for support for adding our own custom functions - as is already available in jmespath.py?

If this isn't in the pipeline, would you be willing to take a PR for adding this? I'd propose an interface as close as possible to the one already available in Python. So...

> jmespath.search(query, data, { customFunctions : new MyCustomFunctions() })
@bejohu
Copy link

bejohu commented Apr 28, 2019

Looks like @burningtree created PR for this a while ago: #2

@peterfotinis
Copy link

+1

@glenveegee
Copy link

We've implemented this in a slightly different way while still maintaining validation and interpreter scoping within the function. It also helps to add any number as a series of plugins to jmespath. See https://github.com/nanoporetech/jmespath-ts

Here's a snippet from the unit test:

  it('register a customFunction', () =>  {
      expect(() => search({
        foo: 60,
        bar: 10
      }, 'divide(foo, bar)')
      ).toThrow('Unknown function: divide()');


      jmespath.registerFunction('divide', function(resolvedArgs) {
          const [dividend, divisor] = resolvedArgs;
          return dividend / divisor;
        },
        [{ types: [jmespath.TYPE_NUMBER] }, { types: [jmespath.TYPE_NUMBER] }]
      )

      expect(search({
        foo: 60,
        bar: 10
      }, 'divide(foo, bar)')
      ).toEqual(6)
  });

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

No branches or pull requests

4 participants