examples/webpack/example-builder.js
function createWordIndex(exampleData) {
  const index = {};
  const keys = ['shortdesc', 'title', 'tags'];
  exampleData.forEach((data, i) => {
    keys.forEach(key => {
      let text = data[key];
      if (Array.isArray(text)) {
        text = text.join(' ');
      }
      const words = text ? text.split(/\W+/) : [];
      words.forEach(word => {
        if (word) {
          word = word.toLowerCase();
          let counts = index[word];
          if (counts) {
            if (index in counts) {
              counts[i] += 1;
            } else {
              counts[i] = 1;
            }
          } else {
            counts = {};
            counts[i] = 1;
            index[word] = counts;
          }
        }
      });
    });
  });
  return index;
}
function createWordIndex(exampleData) {
  const index = {};
  const keys = ['shortdesc', 'title', 'tags'];
  exampleData.forEach((data, i) => {
    keys.forEach(key => {
      let text = data[key];
      if (Array.isArray(text)) {
        text = text.join(' ');
      }
      const words = text ? text.split(/\W+/) : [];
      words.forEach(word => {
        if (word) {
          word = word.toLowerCase();
          let counts = index[word];
          if (counts) {
            if (index in counts) {
              counts[i] += 1;
            } else {
              counts[i] = 1;
            }
          } else {
            counts = {};
            counts[i] = 1;
            index[word] = counts;
          }
        }
      });
    });
  });
  return index;
}
function createWordIndex ( exampleData ) { const index = { } ; const keys = [ 'shortdesc' , 'title' , 'tags' ] ; exampleData . forEach ( ( data , i ) => { keys . forEach ( key => { let text = data [ key ] ; if ( Array . isArray ( text ) ) { text = text . join ( ' ' ) ; } const words = text ? text . split ( / \W+ / ) : [ ] ; words . forEach ( word => { if ( word ) { word = word . toLowerCase ( ) ; let counts = index [ word ] ; if ( counts ) { if ( index in counts ) { counts [ i ] += 1 ; } else { counts [ i ] = 1 ; } } else { counts = { } ; counts [ i ] = 1 ; index [ word ] = counts ; } } } ) ; } ) ; } ) ; return index ; }
Create an inverted index of keywords from examples.  Property names are
lowercased words.  Property values are objects mapping example index to word
count.
@param {Array<Object>} exampleData Array of example data objects.
@return {Object} Word index.
Create an inverted index of keywords from examples . Property names are lowercased words . Property values are objects mapping example index to word count .