-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (36 loc) · 1020 Bytes
/
Copy pathindex.js
File metadata and controls
47 lines (36 loc) · 1020 Bytes
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
var Transform = require('readable-stream/transform')
var stringify = require('json-stringify-safe')
var inherits = require('util').inherits
var crypto = require('crypto')
module.exports = filter
function filter(fn) {
var stream = Unique()
if (typeof fn === 'function') stream._calculate = fn
return stream
}
filter.Unique = Unique
filter.calculate = calculate
inherits(Unique, Transform)
function Unique(options) {
if (!(this instanceof Unique)) return new Unique(options)
options = options || {}
options.objectMode = true
Transform.call(this, options)
this._hashes = Object.create(null)
}
Unique.prototype._calculate = calculate
Unique.prototype._transform = function (doc, NULL, cb) {
var hash = this._calculate(doc)
if (this._hashes[hash]) return cb()
this._hashes[hash] = true
cb(null, doc)
}
Unique.prototype._flush = function (cb) {
delete this._hashes
cb()
}
function calculate(doc) {
return crypto.createHash('sha256')
.update(stringify(doc))
.digest('base64')
}