├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '4' 4 | - '0.12' 5 | - '0.10' 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # snippet-stream 2 | 3 | Split a stream of JS source code into parsable snippets 4 | 5 | ``` 6 | npm install snippet-stream 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/snippet-stream.svg?style=flat)](http://travis-ci.org/mafintosh/snippet-stream) 10 | 11 | Uses [snippetify](https://github.com/thlorenz/snippetify) for all the heavy lifting 12 | 13 | ## Usage 14 | 15 | ``` js 16 | var snippets = require('snippet-stream') 17 | 18 | // create a snippet stream 19 | var stream = snippets() 20 | 21 | // write some js to it 22 | stream.write('var a = 1\n') 23 | stream.write('function foo () {\n') 24 | stream.write(' return a + 1\n') 25 | stream.write('}\n') 26 | stream.write('foo()') 27 | stream.end() 28 | 29 | stream.on('data', function (data) { 30 | console.log('snippet:') 31 | console.log(data) 32 | }) 33 | ``` 34 | 35 | Running the above produces the following output 36 | 37 | ``` 38 | snippet: 39 | var a = 1 40 | 41 | snippet: 42 | function foo () { 43 | return a + 1 44 | } 45 | 46 | snippet: 47 | foo() 48 | ``` 49 | 50 | ## Streaming eval 51 | 52 | You can use this to create a streaming eval function 53 | 54 | ``` js 55 | var snippets = require('snippet-stream') 56 | 57 | // create a snippet stream 58 | var stream = snippets() 59 | var scope = {} 60 | 61 | // write some js to it 62 | stream.write('var a = 1\n') 63 | stream.write('function foo () {\n') 64 | stream.write(' return a + 1\n') 65 | stream.write('}\n') 66 | stream.write('console.log(foo())') 67 | stream.end() 68 | 69 | stream.on('data', function (data) { 70 | eval.call(scope, data) 71 | }) 72 | ``` 73 | 74 | ## License 75 | 76 | MIT 77 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var snippets = require('./') 2 | 3 | // create a snippet stream 4 | var stream = snippets() 5 | 6 | // write some js to it 7 | stream.write('var a = 1\n') 8 | stream.write('function foo () {\n') 9 | stream.write(' return a + 1\n') 10 | stream.write('}\n') 11 | stream.write('console.log(foo())') 12 | stream.end() 13 | 14 | stream.on('data', function (data) { 15 | console.log('snippet:') 16 | console.log(data) 17 | }) 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through = require('through2') 2 | var snippetify = require('snippetify') 3 | var StringDecoder = require('string_decoder').StringDecoder 4 | 5 | module.exports = function () { 6 | var buffer = '' 7 | var decoder = new StringDecoder() 8 | 9 | return through.obj(write, flush) 10 | 11 | function write (data, enc, cb) { 12 | buffer += decoder.write(data) 13 | 14 | try { 15 | var raws = snippetify(buffer).map(toRaw) 16 | } catch (err) { 17 | return cb() 18 | } 19 | 20 | var partials = 1 21 | while (raws.length - partials >= 0 && raws.length && !raws[raws.length - partials]) partials++ 22 | 23 | for (var i = 0; i < raws.length - partials; i++) { 24 | buffer = buffer.slice(raws[i].length + 1) // +1 for newline 25 | this.push(raws[i] + '\n') 26 | } 27 | 28 | cb() 29 | } 30 | 31 | function flush (cb) { 32 | this.push(buffer) 33 | cb() 34 | } 35 | } 36 | 37 | function toRaw (item) { 38 | return item.raw 39 | } 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "snippet-stream", 3 | "version": "1.0.2", 4 | "description": "Split a stream of JS source code into parsable snippets", 5 | "main": "index.js", 6 | "dependencies": { 7 | "snippetify": "^0.2.1", 8 | "through2": "^2.0.0" 9 | }, 10 | "devDependencies": { 11 | "standard": "^5.3.1", 12 | "tape": "^4.2.2" 13 | }, 14 | "scripts": { 15 | "test": "standard && tape test.js" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/mafintosh/snippet-stream.git" 20 | }, 21 | "author": "Mathias Buus (@mafintosh)", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/mafintosh/snippet-stream/issues" 25 | }, 26 | "homepage": "https://github.com/mafintosh/snippet-stream" 27 | } 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var snippets = require('./') 2 | var tape = require('tape') 3 | 4 | tape('splits into snippets', function (t) { 5 | var s = snippets() 6 | 7 | s.write('var a = 1\n') 8 | s.write('function foo () {\n') 9 | s.write(' return a + 1\n') 10 | s.write('}\n') 11 | s.write('var b;') 12 | s.end() 13 | 14 | var expected = ['var a = 1\n', 'function foo () {\n return a + 1\n}\n', 'var b;'] 15 | 16 | s.on('data', function (data) { 17 | t.same(data, expected.shift(), 'snippet match expected output') 18 | }) 19 | 20 | s.on('end', function () { 21 | t.same(expected.length, 0, 'no more snippets') 22 | t.end() 23 | }) 24 | }) 25 | 26 | tape('partial valid expression', function (t) { 27 | var s = snippets() 28 | 29 | s.write('var a = 1\n') 30 | s.write('foo\n') 31 | s.write('()\n') 32 | s.write('var b = 2') 33 | s.end() 34 | 35 | var expected = ['var a = 1\n', 'foo\n()\n', 'var b = 2'] 36 | 37 | s.on('data', function (data) { 38 | t.same(data, expected.shift(), 'snippet match expected output') 39 | }) 40 | 41 | s.on('end', function () { 42 | t.same(expected.length, 0, 'no more snippets') 43 | t.end() 44 | }) 45 | }) 46 | --------------------------------------------------------------------------------