liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
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
/**
 * Returns an array containing the initial elements which both input arrays have in common.
 *
 * A common use-case for this is discovering common ancestors between two file paths.
 *
 * ```js
 * > commonSequence = require('common-sequence');
 *
 * > pathA = '/Users/lloyd/Documents/75lb/dmd'.split('/');
 * > pathB = '/Users/lloyd/Documents/75lb/array-tools'.split('/');
 *
 * > commonSequence(pathA, pathB).join('/');
 * '/Users/lloyd/Documents/75lb'
 * ```
 *
 * or a more trivial example:
 * ```js
 * > a.commonSequence([ 1, 2, 3 ], [ 1, 2, 4 ])
 * [ 1, 2 ]
 * ```
 * @module common-sequence
 */
module.exports = commonSequence
 
/**
 * Returns the initial elements which both input arrays have in common
 * @param {Array} - first array to compare
 * @param {Array} - second array to compare
 * @returns {Array}
 * @alias module:common-sequence
 */
function commonSequence (a, b) {
  var result = []
  for (var i = 0; i < Math.min(a.length, b.length); i++) {
    if (a[i] === b[i]) {
      result.push(a[i])
    } else {
      break
    }
  }
  return result
}