All files / ramekin/lib util.js

0% Statements 0/20
0% Branches 0/12
0% Functions 0/3
0% Lines 0/18

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 44 45 46 47 48 49                                                                                                 
const intersection = (a, b) => {
  let setB = new Set(b);
  return Array.from(new Set(a.filter(x => setB.has(x))))
}
 
 
  /**
   * 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.
   */
  const 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
  }
 
module.exports = {
  intersection,
  isSubPhrase
}