File: index.js

package info (click to toggle)
node-tap 12.0.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,996 kB
  • sloc: javascript: 13,674; sh: 75; makefile: 73
file content (245 lines) | stat: -rw-r--r-- 5,574 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
'use strict'

module.exports = shallow
shallow.strict = strict

// TODO a future version of this that drops node 0.x support will
// check for Symbol.iterator, and handle anything that can be passed
// to Array.from()

var hasSet = typeof Set === 'function'

function isSet (object) {
  return hasSet && (object instanceof Set)
}

function isMap (object) {
  return hasSet && (object instanceof Map)
}

function setSame (a, b, ca, cb, fn) {
  if (a.size !== b.size)
    return false
  else if (a.size === 0)
    return true
  else {
    var ret = true
    var seen = new Set()
    a.forEach(function (entry) {
      if (!ret)
        return

      if (b.has(entry) && !seen.has(entry)) {
        seen.add(entry)
        return
      }

      // see if b has anything that matches
      // but don't allow anything to match more than once.
      var done = false
      b.forEach(function (bentry) {
        if (done || seen.has(bentry))
          return

        if (fn(bentry, entry, ca, cb)) {
          seen.add(bentry)
          ret = true
          done = true
        }
      })

      // didn't find the match by walking the object
      // not a match!
      if (!done)
        ret = false
    })
    return ret
  }
}

function mapSame (a, b, ca, cb, fn) {
  var ret = a.size === b.size
  if (a.size && ret) {
    a.forEach(function (value, key) {
      if (ret)
        ret = b.has(key)
      if (ret)
        ret = fn(value, b.get(key), ca, cb)
    })
  }
  return ret
}

function isArguments (object) {
  return Object.prototype.toString.call(object) === '[object Arguments]'
}

function arrayFrom (obj) {
  return Array.isArray(obj) ? obj
    : Array.from ? Array.from(obj)
    : Array.prototype.slice.call(obj)
}

function strict (a, b) {
  return deeper(a, b, [], [])
}

function deeper (a, b, ca, cb) {
  return a === b ? true
    : a !== a ? b !== b
    : typeof a !== 'object' || typeof b !== 'object' ? false
    : a === null || b === null ? false
    : Buffer.isBuffer(a) && Buffer.isBuffer(b) ? bufferSame(a, b)
    : a instanceof Date && b instanceof Date ? a.getTime() === b.getTime()
    : a instanceof RegExp && b instanceof RegExp ? regexpSame(a, b)
    : isArguments(a) ?
      isArguments(b) && deeper(arrayFrom(a), arrayFrom(b), ca, cb)
    : isArguments(b) ? false
    : a.constructor !== b.constructor ? false
    : isSet(a) && isSet(b) ? setSame(a, b, ca, cb, deeper)
    : isMap(a) && isMap(b) ? mapSame(a, b, ca, cb, deeper)
    : deeperObj(a, b, Object.keys(a), Object.keys(b), ca, cb)
}

function deeperObj (a, b, ka, kb, ca, cb) {
  // don't bother with stack acrobatics if there's nothing there
  return ka.length === 0 && kb.length === 0 ? true
    : ka.length !== kb.length ? false
    : deeperObj_(a, b, ka, kb, ca, cb)
}

function deeperObj_ (a, b, ka, kb, ca, cb) {
  var ret = true

  var cal = ca.length

  var go = true
  while (cal-- && go) {
    if (ca[cal] === a) {
      ret = cb[cal] === b
      go = false
    }
  }

  if (go) {
    ca.push(a)
    cb.push(b)

    ka.sort()
    kb.sort()

    for (var j = ka.length - 1; j >= 0 && ret; j--) {
      if (ka[j] !== kb[j])
        ret = false
    }

    if (ret) {
      var key
      for (var k = ka.length - 1; k >= 0 && ret; k--) {
        key = ka[k]
        if (!deeper(a[key], b[key], ca, cb))
          ret = false
      }
    }

    if (ret) {
      ca.pop()
      cb.pop()
    }
  }

  return ret
}

function shallow (a, b) {
  return shallower(a, b, [], [])
}

function regexpSame (a, b) {
  return a.source === b.source &&
    a.global === b.global &&
    a.multiline === b.multiline &&
    a.lastIndex === b.lastIndex &&
    a.ignoreCase === b.ignoreCase
}

function bufferSame (a, b) {
  var ret
  if (a.equals) {
    ret = a.equals(b)
  } else if (a.length !== b.length) {
    ret = false
  } else {
    ret = true
    for (var j = 0; j < a.length && ret; j++) {
      if (a[j] != b[j])
        ret = false
    }
  }
  return ret
}

function shallower (a, b, ca, cb) {
  return typeof a !== 'object' && typeof b !== 'object' && a == b ? true
    : a === null || b === null ? a == b
    : a !== a ? b !== b
    : typeof a !== 'object' || typeof b !== 'object' ? false
    : Buffer.isBuffer(a) && Buffer.isBuffer(b) ? bufferSame(a, b)
    : a instanceof Date && b instanceof Date ? a.getTime() === b.getTime()
    : a instanceof RegExp && b instanceof RegExp ? regexpSame(a, b)
    : isArguments(a) || isArguments(b) ?
      shallower(arrayFrom(a), arrayFrom(b), ca, cb)
    : isSet(a) && isSet(b) ? setSame(a, b, ca, cb, shallower)
    : isMap(a) && isMap(b) ? mapSame(a, b, ca, cb, shallower)
    : shallowerObj(a, b, Object.keys(a), Object.keys(b), ca, cb)
}

function shallowerObj (a, b, ka, kb, ca, cb) {
  // don't bother with stack acrobatics if there's nothing there
  return ka.length === 0 && kb.length === 0 ? true
    : ka.length !== kb.length ? false
    : shallowerObj_(a, b, ka, kb, ca, cb)
}

function shallowerObj_ (a, b, ka, kb, ca, cb) {
  var ret

  var cal = ca.length
  var go = true
  while (cal-- && go) {
    if (ca[cal] === a) {
      ret = cb[cal] === b
      go = false
    }
  }

  if (go) {
    ca.push(a)
    cb.push(b)

    ka.sort()
    kb.sort()

    ret = true
    for (var k = ka.length - 1; k >= 0 && ret; k--) {
      if (ka[k] !== kb[k])
        ret = false
    }

    if (ret) {
      var key
      for (var l = ka.length - 1; l >= 0 && ret; l--) {
        key = ka[l]
        if (!shallower(a[key], b[key], ca, cb))
          ret = false
      }
    }

    if (ret) {
      ca.pop()
      cb.pop()
    }
  }

  return ret
}