git ssb

2+

Dominic / pull-stream



Commit 270506f3aabb41d4a2d4ae309eef3029ee733587

unique

Dominic Tarr committed on 3/27/2013, 11:56:04 AM
Parent: bd9814c315e2dae06c3ea1e7b711afe595cee63b

Files changed

throughs.jschanged
throughs.jsView
@@ -1,13 +1,21 @@
1+function prop (map) {
2+ if('string' == typeof map) {
3+ var key = map
4+ return function (data) { return data[key] }
5+ }
6+ return map
7+}
8+
9+function id (item) {
10+ return item
11+}
12+
113 var k = 0
214 var map = exports.map =
315 function (read, map) {
416 var _k = k++
5- if('string' == typeof map) {
6- var key = map
7- map = function (data) { return data[key] }
8- }
9- map = map || function (e) {return e}
17+ map = prop(map) || id
1018 return function (end, cb) {
1119 read(end, function (end, data) {
1220 var data = !end ? map(data) : null
1321 cb(end, data)
@@ -20,9 +28,9 @@
2028 //regexp
2129 if('object' === typeof test
2230 && 'function' === typeof test.test)
2331 test = test.test.bind(test)
24- test = test || function (data) {return !!data}
32+ test = prop(test) || id
2533 return function next (end, cb) {
2634 read(end, function (end, data) {
2735 if(!end && !test(data))
2836 return next(end, cb)
@@ -69,8 +77,23 @@
6977 })
7078 }
7179 }
7280
81+var unique = exports.unique = function (read, field, invert) {
82+ field = prop(field) || id
83+ var seen = {}
84+ return filter(read, function (data) {
85+ var key = field(data)
86+ if(seen[key]) return !!invert //false, by default
87+ else seen[key] = true
88+ return !invert //true by default
89+ })
90+}
91+
92+var nonUnique = exports.nonUnique = function (read, field) {
93+ return unique(read, field, true)
94+}
95+
7396 var nextTick = process.nextTick
7497
7598 var highWaterMark = exports.highWaterMark =
7699 function (read, highWaterMark) {

Built with git-ssb-web