Skip to content

Add ability to leverage custom operations from JSON Logic #18

@reify-benjamin-solum

Description

@reify-benjamin-solum

Taking a shot in the dark as it looks like this library may be unsupported.

The jsonLogic library has the ability to add custom operations: https://jsonlogic.com/add_operation.html

This is needed for things like String.includes. Unfortunately, This library has no way to tap into those as it currently stands. So doing the below fails:

import jsonLogic from 'json-logic-js';
import jsToJsonLogic from 'js-to-json-logic';

jsonLogic.add_operation('includes', (a, b) => a.includes(b));

const logicStr = jsToJsonLogic(`pie.filling includes 'sug'`); // this blows up

whereas this works as expected:

import jsonLogic from 'json-logic-js';
import jsToJsonLogic from 'js-to-json-logic';

jsonLogic.add_operation('includes', (a, b) => a.includes(b));

const data = { temp: 100, pie: { filling: 'apples and sugar' } };

console.log(
  jsonLogic.apply({ includes: [{ var: 'pie.filling' }, 'sug'] }, data) // results in boolean true
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions