All files / lib text-helpers.js

0% Statements 0/15
0% Branches 0/12
0% Functions 0/1
0% Lines 0/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                                                                                     
/**
 * Set of helper functions for processing text and trending.
 */
module.exports = class TextHelpers {
  /**
   * Returns true if one phrase is a sub phrase of the other.
   *
   * @params a (Array) an array of words
   * @params b (Array) another array of words
   * @return boolean - whether a or b is a sub-phrase of the other.
   */
  static isSubPhrase (a, b) {
    // if either are empty, return false
    if (a.length === 0 || b.length === 0) {
      return false
    }
 
    // swap phrases if a is less than b
    if (b.length > a.length) {
      let swap = a
      a = b
      b = swap
    }
 
    // Given that b is either the same or shorter than a, b will be a sub set
    // a, so start matching  similar shorter  find where the first match.
    let start = a.indexOf(b[0])
 
    // it was found, and check there is space
    // Rewrite just subtract a from start .. (start + )
    if ((start >= 0) && ((start + b.length) <= a.length)) {
      // check the rest matches
      for (let j = 1; j < b.length; j++) {
        if (b[j] !== a[start + j]) {
          return false
        }
      }
      return true
    }
    return false
  }
}