Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thlorenz/snippetify
Splits a given JavaScript file into as many parsable snippets as possible.
https://github.com/thlorenz/snippetify
Last synced: 15 days ago
JSON representation
Splits a given JavaScript file into as many parsable snippets as possible.
- Host: GitHub
- URL: https://github.com/thlorenz/snippetify
- Owner: thlorenz
- License: mit
- Created: 2013-02-25T12:20:04.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-05-12T00:24:33.000Z (over 11 years ago)
- Last Synced: 2024-10-18T00:15:24.124Z (20 days ago)
- Language: JavaScript
- Size: 152 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# snippetify [![build status](https://secure.travis-ci.org/thlorenz/snippetify.png)](http://next.travis-ci.org/thlorenz/snippetify)
Splits a given JavaScript file into as many parsable snippets as possible.
## Example
```js
'use strict';
var fs = require('fs')
, snippetify = require('..')
, code = fs.readFileSync(__filename, 'utf-8')
, snippets = snippetify(code);function printRawCode(snippets) {
// prints all lines exactly as they appeared in the script
var lines = snippets
.map(function (x) { return '[ ' + x.raw + ' ]'; });console.log(lines.join('\n'));
}printRawCode(snippets);
```Outputs:
```js
[ 'use strict'; ]
[ var fs = require('fs') ]
[ , snippetify = require('..') ]
[ , code = fs.readFileSync(__filename, 'utf-8') ]
[ , snippets = snippetify(code); ]
[ ]
[ function printRawCode(snippets) {
// prints all lines exactly as they appeared in the script
var lines = snippets
.map(function (x) { return '[ ' + x.raw + ' ]'; });console.log(lines.join('\n'));
} ]
[ ]
[ printRawCode(snippets); ]
[ ]
```## API
```js
snippetify(script[, esprimaOpts])
``````
/**
* Splits given script into as many root level snippets as possible, one line being the smallest possible.
* Keeps root level expressions intact, i.e. does not pull out snippets from inside functions.
*
* @name snippetify
* @function
* @param script {String} The script to split into snippets.
* @param opts {Object} Options, most of which will be passed to the esprima parser (optional):
* loc : Nodes have line and column-based location info
* range : Nodes have an index-based location range (array)
* raw : Literals have extra property which stores the verbatim source
* tokens : An extra array containing all found tokens
* comment : An extra array containing all line and block comments
* tolerant : An extra array containing all errors found, attempts to continue parsing when an error is encountered
* -- Non esprima opts:
* nonstrict: Removes 'use strict' directives during parse to prevent esprima parser from throwing errors due to use strict violations
* @return {Array{Object}} each with the following properties:
* code : The snippet code that was parsed and possibly fixed
* raw : The snippet code that was parsed before it was fixed
* ast : The AST for the snippet (note ast.tokens will be present if tokens: true is set)
*/
```## More Examples
Find more examples [here](https://github.com/thlorenz/snippetify/tree/master/examples)