stream-match
Advanced tools
+7
-5
@@ -5,11 +5,13 @@ 'use strict' | ||
| new Promise(resolve => { | ||
| const test = typeof pattern === 'string' | ||
| ? buf => buf.includes(pattern) | ||
| : buf => pattern.test(buf) | ||
| const match = | ||
| typeof pattern === 'string' | ||
| ? buf => buf.includes(pattern) | ||
| : buf => pattern.exec(buf) && pattern.exec(buf)[1] | ||
| let buf = '' | ||
| const onData = data => { | ||
| buf += data | ||
| if (test(buf)) { | ||
| const res = match(buf) | ||
| if (res) { | ||
| stream.removeListener('data', onData) | ||
| resolve() | ||
| resolve(res) | ||
| } | ||
@@ -16,0 +18,0 @@ } |
+4
-2
| { | ||
| "name": "stream-match", | ||
| "version": "1.1.1", | ||
| "version": "1.2.0", | ||
| "license": "MIT", | ||
| "description": "Match a string in a stream", | ||
| "description": "Match a string in a stream. Zero dependencies", | ||
| "repository": "juliangruber/stream-match", | ||
| "scripts": { | ||
| "release": "np", | ||
| "test": "prettier-standard '**/*.js' && standard && tap test.js" | ||
| }, | ||
| "devDependencies": { | ||
| "np": "^5.2.1", | ||
| "prettier-standard": "^15.0.1", | ||
@@ -12,0 +14,0 @@ "standard": "^14.3.1", |
+4
-4
| # stream-match | ||
| Match a regexp or string in a stream. | ||
| Match a regexp or string in a stream. Zero dependencies. | ||
@@ -9,4 +9,4 @@ ## Usage | ||
| // Given a readable stream, | ||
| await match(stream, /pattern/) | ||
| // now `pattern` has been emitted by it. | ||
| const res = await match(stream, /(p[^n+]n)/) | ||
| // `res` will containt `pattern` once it has been emitted. | ||
| ``` | ||
@@ -22,3 +22,3 @@ | ||
| ### await match(stream, pattern || string) | ||
| ### res = await match(stream, pattern || string) | ||
@@ -25,0 +25,0 @@ ## License |
+6
-7
@@ -9,15 +9,14 @@ 'use strict' | ||
| const stream = new PassThrough() | ||
| let matched = false | ||
| let res | ||
| await Promise.all([ | ||
| (async () => { | ||
| await match(stream, /beep boop/) | ||
| matched = true | ||
| res = await match(stream, /(bo+p)/) | ||
| })(), | ||
| (async () => { | ||
| t.notOk(matched) | ||
| t.notOk(res) | ||
| stream.push('beep ') | ||
| t.notOk(matched) | ||
| t.notOk(res) | ||
| stream.push('boo') | ||
| t.notOk(matched) | ||
| t.notOk(res) | ||
| stream.push('p') | ||
@@ -27,3 +26,3 @@ })() | ||
| t.ok(matched) | ||
| t.equal(res, 'boop') | ||
| stream.push('everything else') | ||
@@ -30,0 +29,0 @@ }) |
3043
4.93%82
1.23%4
33.33%