All files / ramekin/lib text-helpers.js

86.67% Statements 13/15
83.33% Branches 10/12
100% Functions 1/1
85.71% Lines 12/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      1x                   83091x         83091x 19253x 19253x 19253x         83091x       83091x   1053x 193x       1053x   82038x      
/**
 * 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
    Iif (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++) {
        Iif (b[j] !== a[start + j]) {
          return false
        }
      }
      return true
    }
    return false
  }
}