mena-devs / objectron

Compare an object to a generic model to test equality and extract matches
Apache License 2.0
17 stars 2 forks source link

Ability to match multiple patterns in 1 call #23

Open Link- opened 3 years ago

Link- commented 3 years ago

Problem:

There are scenarios that require matching an item in an array but also the following item (or items at any other index). Example:

[
  {
    type: 'code',
    raw: '```\n{ issues: [1,2] }\n```\n',
    lang: '',
    text: '{ issues: [1,2] }'
  },
  { type: 'hr', raw: '---\n\n' },
  {
    type: 'heading',
    raw: '## Action Items\n',
    depth: 2,
    text: 'Action Items',
    tokens: [ [Object] ]
  },
  {
    type: 'heading',
    raw: '#### Test Issue #1 - #1 - 03 October 2020\n',
    depth: 4,
    text: 'Test Issue #1 - #1 - 03 October 2020',
    tokens: [ [Object] ]
  },
  {
    type: 'list',
    raw: '- [ ] Action item #1 for @Link- \n' +
      '- [ ] @Link- has to do action items #2\n' +
      '\n',
    ordered: false,
    start: '',
    loose: false,
    items: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  }
]

For the payload I would like to match the following objects:

  {
    type: 'heading',
    raw: '#### Test Issue #1 - #1 - 03 October 2020\n',
    depth: 4,
    text: 'Test Issue #1 - #1 - 03 October 2020',
    tokens: [ [Object] ]
  },
  {
    type: 'list',
    raw: '- [ ] Action item #1 for @Link- \n' +
      '- [ ] @Link- has to do action items #2\n' +
      '\n',
    ordered: false,
    start: '',
    loose: false,
    items: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  }

However, the match() function accepts only 1 pattern. If arguments are passed as follows:

match(
      block,
      {
        type: 'heading',
        depth: 4,
        text: /.*(?<issueNumber>#[0-9]*) - (?<date>.*)/
      },
      {
        type: 'list',
        ordered: false,
        loose: false
      }
    )

The method will assume the second pattern object as the callback.

Proposed Solution

Allow match() to accept an array of patterns while providing a new optional flag that will inform the method that it should handle an array of patterns not lookup the array structure in the payload.