Commit bffa53fb184e86b8070bd6d059813eb100b8ebb4
refactor out tests to reuse
Dominic Tarr committed on 4/15/2013, 10:51:06 PMParent: e009a08c7f44e7c7f9c2890d8f00780b1819ebe2
Files changed
sinks.js | changed |
throughs.js | changed |
util.js | added |
sinks.js | ||
---|---|---|
@@ -1,4 +1,8 @@ | ||
1 | +var u = require('./util') | |
2 | +var prop = u.prop | |
3 | +var id = u.id | |
4 | + | |
1 | 5 | var drain = exports.drain = function (read, op, done) { |
2 | 6 | ;(function next() { |
3 | 7 | var sync = true, returned = false, loop = true |
4 | 8 | do { |
throughs.js | ||
---|---|---|
@@ -1,16 +1,9 @@ | ||
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 | -} | |
1 | +var u = require('./util') | |
2 | +var prop = u.prop | |
3 | +var id = u.id | |
4 | +var tester = u.tester | |
8 | 5 | |
9 | -function id (item) { | |
10 | - return item | |
11 | -} | |
12 | - | |
13 | 6 | var map = exports.map = |
14 | 7 | function (read, map) { |
15 | 8 | map = prop(map) || id |
16 | 9 | return function (end, cb) { |
@@ -35,12 +28,9 @@ | ||
35 | 28 | |
36 | 29 | var filter = exports.filter = |
37 | 30 | function (read, test) { |
38 | 31 | //regexp |
39 | - if('object' === typeof test | |
40 | - && 'function' === typeof test.test) | |
41 | - test = test.test.bind(test) | |
42 | - test = prop(test) || id | |
32 | + test = tester(test) | |
43 | 33 | return function next (end, cb) { |
44 | 34 | read(end, function (end, data) { |
45 | 35 | if(!end && !test(data)) |
46 | 36 | return next(end, cb) |
@@ -75,8 +65,11 @@ | ||
75 | 65 | var n = test; test = function () { |
76 | 66 | return n -- |
77 | 67 | } |
78 | 68 | } |
69 | + // else | |
70 | +// test = tester(test) | |
71 | + | |
79 | 72 | return function (end, cb) { |
80 | 73 | if(ended) return cb(ended) |
81 | 74 | if(1 === more) end = true |
82 | 75 | if(ended = end) return read(ended, cb) |
util.js | ||
---|---|---|
@@ -1,0 +1,23 @@ | ||
1 | +exports.id = | |
2 | +function (item) { | |
3 | + return item | |
4 | +} | |
5 | + | |
6 | +exports.prop = | |
7 | +function (map) { | |
8 | + if('string' == typeof map) { | |
9 | + var key = map | |
10 | + return function (data) { return data[key] } | |
11 | + } | |
12 | + return map | |
13 | +} | |
14 | + | |
15 | +exports.tester = function (test) { | |
16 | + if(!test) return exports.id | |
17 | + if('object' === typeof test | |
18 | + && 'function' === typeof test.test) | |
19 | + return test.test.bind(test) | |
20 | + return exports.prop(test) || exports.id | |
21 | +} | |
22 | + | |
23 | + |
Built with git-ssb-web