selfrefactor / rambdax

Extended version of Rambda
https://selfrefactor.github.io/rambdax
MIT License
221 stars 26 forks source link

Add arbitrary expression parsing in curly braces … #46

Closed brandonpittman closed 4 years ago

brandonpittman commented 4 years ago

I found myself wanting to push the limits of what template can do, but I didn't want to pull in a larger templating library, so I figured we could just allow the template function to handle any expression inside it.

selfrefactor commented 4 years ago

Hi @brandonpittman
That sound like a smart idea.

I appreciate your PR, but I will have to ask you to add some tests, because as much as I see, there should be a change in R.template specs.

selfrefactor commented 4 years ago

Few more things:

FAIL src/template.spec.js

✓ within bracets (3ms)

✓ ok

✓ no interpolation + curry (1ms)

✕ with missing template input (4ms)

● with missing template input

ReferenceError: bar is not defined

at eval (eval at template (:22), <anonymous>:1:23)
brandonpittman commented 4 years ago

I’ll work on that today. Thanks.

brandonpittman commented 4 years ago

@selfrefactor I changed the escape name and exported the function.

I also changed the test because it shouldn't spit out the template string anymore. It's going to evaluate everything in the brackets, which should actually throw a reference error.

selfrefactor commented 4 years ago

I will check it later today. Thank you again.

selfrefactor commented 4 years ago

I copied your version of R.template and I ran it against the current Rambdax spec. This test:

test('with missing template input', () => {
  const input = 'foo is {{bar}} even {{a}} more'
  const templateInput = {
    baz : 'BAR',
    a   : 1,
  }

  const result = template(input, templateInput)
  const expectedResult = 'foo is {{bar}} even 1 more'

  expect(result).toEqual(expectedResult)
})

you know that fails. because in your tests you expect to throw. We would introduce a breaking change, this even forcing to major bump. Tests are contracts, and I think this contract of R.template not to throw on wrong input seems important.

Maybe you should change the method so it passes the failing test.

brandonpittman commented 4 years ago

@selfrefactor Okay, we now just pass back the unprocessed input in the event of a ReferenceError. Existing specs pass and I added a new one for arbitrary JS expressions.

selfrefactor commented 4 years ago

@brandonpittman - that sounds good to me. As you may have understand, my main interest is not the code itself rather than what happens with the tests. I will wait for you to notify me once you are ready with the tests.

brandonpittman commented 4 years ago

Not sure what you mean.

The latest commit makes all your tests pass and passes a new test I wrote to cover bare expressions.

selfrefactor commented 4 years ago

I will release a version this Sunday. Thank you for your collaboration.

brandonpittman commented 4 years ago

Thank you!

selfrefactor commented 4 years ago

Just released 3.6.0 including this MR.

brandonpittman commented 4 years ago

@selfrefactor Still showing up at 3.5 on NPM.

selfrefactor commented 4 years ago

I realized that just now, and finally the release is made.