aboutsummaryrefslogtreecommitdiff
path: root/lib/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util.js')
-rw-r--r--lib/util.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/util.js b/lib/util.js
index e8fc435..d25ecaf 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -141,3 +141,31 @@ u.escapeHTML = function (html) {
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
}
+
+u.pullSlice = function (start, end) {
+ if (end == null) end = Infinity
+ var offset = 0
+ return function (read) {
+ return function (abort, cb) {
+ if (abort) read(abort, cb)
+ else if (offset >= end) read(true, function (err) {
+ cb(err || true)
+ })
+ else if (offset < start) read(null, function next(err, data) {
+ if (err) return cb(err)
+ offset += data.length
+ if (offset <= start) read(null, next)
+ else if (offset < end) cb(null,
+ data.slice(data.length - (offset - start)))
+ else cb(null, data.slice(data.length - (offset - start),
+ data.length - (offset - end)))
+ })
+ else read(null, function (err, data) {
+ if (err) return cb(err)
+ offset += data.length
+ if (offset <= end) cb(null, data)
+ else cb(null, data.slice(0, data.length - (offset - end)))
+ })
+ }
+ }
+}