Blame view

node_modules/uniq/uniq.js 910 Bytes
aaac7fed   liuqimichale   add
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
50
51
52
53
54
55
56
57
  "use strict"
  
  function unique_pred(list, compare) {
    var ptr = 1
      , len = list.length
      , a=list[0], b=list[0]
    for(var i=1; i<len; ++i) {
      b = a
      a = list[i]
      if(compare(a, b)) {
        if(i === ptr) {
          ptr++
          continue
        }
        list[ptr++] = a
      }
    }
    list.length = ptr
    return list
  }
  
  function unique_eq(list) {
    var ptr = 1
      , len = list.length
      , a=list[0], b = list[0]
    for(var i=1; i<len; ++i, b=a) {
      b = a
      a = list[i]
      if(a !== b) {
        if(i === ptr) {
          ptr++
          continue
        }
        list[ptr++] = a
      }
    }
    list.length = ptr
    return list
  }
  
  function unique(list, compare, sorted) {
    if(list.length === 0) {
      return list
    }
    if(compare) {
      if(!sorted) {
        list.sort(compare)
      }
      return unique_pred(list, compare)
    }
    if(!sorted) {
      list.sort()
    }
    return unique_eq(list)
  }
  
  module.exports = unique