question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow searching in arrays

See original GitHub issue

Discussion started in https://gitter.im/reactabular/reactabular?at=57ee9a024380866f04cb1cbf

To summarize, currently reactabular-search only expects to deal with strings, but in an effort to accommodate as many use cases as possible, it will try to cast a column’s value to string. This works well most of the time: a number such as 1337 would become the searchable string "1337" following that logic. However, when dealing with an array, such as ["Black", "White", "Blue"], it gets casted to "Black,White,Blue". Again, that is “fine” if you just wanted to give search a hand, but it starts to introduce issues. Notably, the query , now returns every row that has at least an item in that array as a result. It also becomes much more difficult to handle the search highlights since they are computed against once string, but the original data is still an array that could be mapped to an unordered list, for example.

So currently search.multipleColumns populates _highlights as such for the Bl query:

{
  _highlights: {
    colors: [
      {
        startIndex: 0,
        length: 2,
      },
      {
        startIndex: 12,
        length: 2,
      },
    ],
  },
  name: "colors",
},

The idea would be for it to traverse arrays and run the search against sub-items:

{
  _highlights: {
    colors: [
      [{
        startIndex: 0,
        length: 2,
      }],
      ,
      [{
        startIndex: 0,
        length: 2,
      }],
    ],
  },
  name: "colors",
},

As you can realize, it keeps the indexes aligned in the results by using sparse arrays. Then, the formatting code could look something like that for the colors column:

cell: {
  format: (colorList, {rowData: {_highlights: {colors}}}) => (
    <ul>
      {colorList.map((colorName, index) => (
        <li key={index}>
          {highlight.cell(colorName, colors[index])}
        </li>
      ))}
    </ul>
  ),
},

I’m presuming highlight.cell(colorName, undefined) just maps to colorName but I didn’t double check. It isn’t the case here, but the approach would be the same for any level of nesting with arrays. There is also the case of objects I didn’t explore, but it could potentially be considered as well.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bebrawcommented, Oct 3, 2016

Yeah, looks good to me.

0reactions
mathieumgcommented, Oct 3, 2016

So this modification would become:

const _columnMatches = ({
  // ...
  castingStrategy = v => Array.isArray(v) ? v : String(v),
}) => {
  // ...
  const resolvedValue = castingStrategy(row[`_${property}`] || row[property]);

Plus of course the appropriate logic to handle this array in the search strategies.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Searching in Arrays - Data Structures Handbook
The simplest search to be done on an array is the linear search. This search starts from one end of the array and...
Read more >
Searching Arrays – Programming Fundamentals - Rebus Press
Finding a specific member of an array means searching the array until the member is found. It's possible that the member does not...
Read more >
Four Different Ways to Search an Array in JavaScript
In this article, we will discuss four methods we can use to search for an item in an array. These methods are:.
Read more >
How do I Search an Array? - LiveCode Lessons
There are a number of ways to search arrays and the choice of search depends largely on the type and size of the...
Read more >
Four Methods to Search Through Arrays in JavaScript
Learn about four approaches to searching for values in arrays: includes, indexOf, find, and filter methods.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found