• NaN years ago - link
    by @ghost

    Rixfeed Dev Log

    Immutability Helpers

    I wrote some code that allows you to send a message like this to the chat bot:

    And it will apply those updates to the targeted node.

    In this case, we're adding the special-group and public group to the viewers of the node if they are not already added.

    In order to make this work, I wanted to extend a javascript library called immutability-helper

    If you're familiar with React, then you know why an immutability helper would be useful.

    If you aren't, you can read about why immutability-helper is useful using the link I provided above.

    Now, in order to use this libary, you do something like this:

    However, I'm not a big fan of all those curly braces.

    I thought it would be neat to be able to define these change sets using YAML.

    Additionally, I wanted to be able to pass complex functions that take arguments and then apply those changes to the field.

    Immutability helper supports custom functions, but it does not support custom functions which take arguments. Custom functions only support functions which take the current value of the field you are updating.

    In order to enable the behavior I wanted, I had to to write some helper functions that take the arguments defined in your YAML change set and curry them.

    What folows is my solution.

    For the benefit of the reader, I included verbose comments.

    If you don't understand how this code works, that's ok. Neither do I. And I wrote it 3 days ago.

    No coding article would be complete without tests, which you can see here, if you're interested: tests. The tests are worth checking out to at least see how I used the factory functions to create the appropiate mocked functions to enable testing.