ForestAdmin/forest-express-mongoose

View on GitHub
src/utils/records-decorator.js

Summary

Maintainability
A
0 mins
Test Coverage
F
13%
const _ = require('lodash');

function decorateForSearch(records, fields, searchValue) {
  const matchFields = {};
  records.forEach((record, index) => {
    fields.forEach((fieldName) => {
      const value = record[fieldName];
      if (value) {
        const searchEscaped = searchValue.replace(/[-[\]{}()*+!<=:?./\\^$|#\s,]/g, '\\$&');
        const searchHighlight = new RegExp(searchEscaped, 'i');
        const match = value.toString().match(searchHighlight);
        if (match) {
          if (!matchFields[index]) {
            matchFields[index] = {
              id: record._id,
              search: [],
            };
          }
          matchFields[index].search.push(fieldName);
        }
      }
    });
  });

  return _.isEmpty(matchFields) ? null : matchFields;
}

exports.decorateForSearch = decorateForSearch;