1

As beginner, I don't know what is I can start from. I need to implement function which will transform string to object with particular structure like I wrote below (original string may be with other number of levels, conditionals etc.):

const str = `time !== 10 && (state === 'Ready' || state === 'Pending')`;

const result = {
  and: [
    {
      field: 'time',
      expression: '!==',
      value: 10
    },
    {
      or: [
        {
          field: 'state',
          expression: '===',
          value: 'Ready'
        },
        {
          field: 'state',
          expression: '===',
          value: 'Pending'
        }
      ]
    }
  ]
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
mark
  • 275
  • 4
  • 13

1 Answers1

1

The structure you mention is called an Abstract Syntax Tree (AST). This is a commom data structure that can represent the abstract structure of a word belonging to a language.

Your strings seem to be written in the infix order: you can try to implement the shunting yard algorithm to build the AST from the string.

As said in a comment, this is not a beginner problem. It would be better if you had some prior experience in data structures, algorithms, theory of languages/automata, and computer programming before implementing the shunting yard algorithm.

m.raynal
  • 2,983
  • 2
  • 21
  • 34