Commit 270506f3aabb41d4a2d4ae309eef3029ee733587
unique
Dominic Tarr committed on 3/27/2013, 11:56:04 AMParent: bd9814c315e2dae06c3ea1e7b711afe595cee63b
Files changed
throughs.js | changed |
throughs.js | ||
---|---|---|
@@ -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 | + | |
1 | 13 | var k = 0 |
2 | 14 | var map = exports.map = |
3 | 15 | function (read, map) { |
4 | 16 | 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 | |
10 | 18 | return function (end, cb) { |
11 | 19 | read(end, function (end, data) { |
12 | 20 | var data = !end ? map(data) : null |
13 | 21 | cb(end, data) |
@@ -20,9 +28,9 @@ | ||
20 | 28 | //regexp |
21 | 29 | if('object' === typeof test |
22 | 30 | && 'function' === typeof test.test) |
23 | 31 | test = test.test.bind(test) |
24 | - test = test || function (data) {return !!data} | |
32 | + test = prop(test) || id | |
25 | 33 | return function next (end, cb) { |
26 | 34 | read(end, function (end, data) { |
27 | 35 | if(!end && !test(data)) |
28 | 36 | return next(end, cb) |
@@ -69,8 +77,23 @@ | ||
69 | 77 | }) |
70 | 78 | } |
71 | 79 | } |
72 | 80 | |
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 | + | |
73 | 96 | var nextTick = process.nextTick |
74 | 97 | |
75 | 98 | var highWaterMark = exports.highWaterMark = |
76 | 99 | function (read, highWaterMark) { |
Built with git-ssb-web