Comparing version 2.1.0 to 2.2.0
159
index.js
@@ -1,21 +0,18 @@ | ||
var through = require('through'); | ||
"use strict"; | ||
/** | ||
* Stringifies the content | ||
* @param {string} content | ||
* @returns {string} | ||
*/ | ||
function stringify(content) { | ||
var stringified_content; | ||
var path = require('path'), | ||
through = require('through'); | ||
stringified_content = content.replace(/\"/g, '\u005C\u0022'); | ||
stringified_content = stringified_content.replace(/^(.*)/gm, '"$1'); | ||
stringified_content = stringified_content.replace(/(.+)$/gm, '$1" +'); | ||
stringified_content = stringified_content.replace(/\+$/, ''); | ||
var DEFAULT_EXTENSIONS = [ | ||
'.text', | ||
'.txt', | ||
'.html', | ||
'.tmpl', | ||
'.tpl' | ||
]; | ||
return 'module.exports = ' + stringified_content + ';\n'; | ||
} | ||
/** | ||
* Creates the | ||
* Creates the Browserify transform function which Browserify will pass the | ||
* file to. | ||
* @param {object | array} options | ||
@@ -25,64 +22,90 @@ * @param {object} options.extensions | ||
*/ | ||
module.exports = function(options) { | ||
module.exports = function (options) { | ||
var extensions = getExtensions(options); | ||
/** | ||
* The file extensions of file which should be stringified | ||
* @type {string[]} | ||
*/ | ||
var extensions = [ | ||
'.text', | ||
'.txt', | ||
'.html', | ||
'.tmpl', | ||
'.tpl' | ||
]; | ||
/** | ||
* The function Browserify will use to transform the input. | ||
* @param {string} file | ||
* @returns {stream} | ||
*/ | ||
function browserifyTransform (file) { | ||
if (!hasStringifiableExtension(file, extensions)) return through(); | ||
if (options) { | ||
if (Object.prototype.toString.call(options) === '[object Array]') { | ||
extensions = options; | ||
} else if(options.extensions) { | ||
extensions = options.extensions; | ||
} | ||
} | ||
var chunks = []; | ||
/** | ||
* Returns whether the file ends in an extension | ||
* @param {string} file | ||
* @return {boolean} | ||
*/ | ||
function has_stringify_extension(file) { | ||
for (var i=0; i<extensions.length; ++i) { | ||
if (file.substr(-1*extensions[i].length) === extensions[i]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function write (buffer) { | ||
chunks.push(buffer); | ||
} | ||
/** | ||
* The browserify transform method | ||
* @param {string} file | ||
* @returns {stream} | ||
*/ | ||
function transform(file) { | ||
var content = ''; | ||
function end () { | ||
var contents = Buffer.concat(chunks).toString('utf8'); | ||
if (!has_stringify_extension(file)) { | ||
return through(); | ||
} | ||
this.queue(stringify(contents)); | ||
this.queue(null); | ||
} | ||
return through(write, end); | ||
}; | ||
function write(buffer) { | ||
content += buffer; | ||
} | ||
return browserifyTransform; | ||
}; | ||
function end() { | ||
this.queue(stringify(content)); | ||
this.queue(null); | ||
} | ||
/** | ||
* Stringifies the content | ||
* @param {string} content | ||
* @returns {string} | ||
*/ | ||
function stringify (content) { | ||
return 'module.exports = ' + JSON.stringify(content) + ';\n'; | ||
} | ||
return through(write, end); | ||
}; | ||
/** | ||
* Takes a set of user-supplied options, and determines which set of file- | ||
* extensions to run Stringify on. | ||
* @param {object | array} options | ||
* @param {object} options.extensions | ||
* @returns {string[]} | ||
*/ | ||
function getExtensions (options) { | ||
/** | ||
* The file extensions which are stringified by default. | ||
* @type {string[]} | ||
*/ | ||
var extensions = DEFAULT_EXTENSIONS; | ||
return transform; | ||
}; | ||
if (options) { | ||
if (Object.prototype.toString.call(options) === '[object Array]') { | ||
extensions = options; | ||
} else if (options.extensions) { | ||
extensions = options.extensions; | ||
} | ||
} | ||
// Lowercase all file extensions for case-insensitive matching. | ||
extensions = extensions.map(function (ext) { | ||
return ext.toLowerCase(); | ||
}); | ||
return extensions; | ||
} | ||
/** | ||
* Returns whether the filename ends in a Stringifiable extension. Case | ||
* insensitive. | ||
* @param {string} filename | ||
* @return {boolean} | ||
*/ | ||
function hasStringifiableExtension (filename, extensions) { | ||
var file_extension = path.extname(filename).toLowerCase(); | ||
return extensions.indexOf(file_extension) > -1; | ||
} | ||
// Test-environment specific exports... | ||
if (process.env.NODE_ENV) { | ||
module.exports.stringify = stringify; | ||
module.exports.getExtensions = getExtensions; | ||
module.exports.DEFAULT_EXTENSIONS = DEFAULT_EXTENSIONS; | ||
module.exports.hasStringifiableExtension = hasStringifiableExtension; | ||
} |
{ | ||
"name": "stringify", | ||
"description": "Browserify middleware to be able to require() text files (including templates) inside of your client-side JavaScript files.", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"main": "./index.js", | ||
@@ -9,2 +9,3 @@ "author": "John Postlethwait <john.postlethwait@gmail.com>", | ||
"James Newell <james@digitaledgeit.com.au>", | ||
"Kevin Ingersoll <kingersoll@gmail.com>", | ||
"Livoras <livoras@163.com>" | ||
@@ -36,3 +37,7 @@ ], | ||
"through": "~2.3.4" | ||
}, | ||
"devDependencies": { | ||
"mocha": "1.20.1", | ||
"should": "4.0.4" | ||
} | ||
} |
# Stringify # | ||
Browserify plugin to require() text files (like templates) inside of your client-side JavaScript files. | ||
Browserify plugin to require() text files (such as HTML templates) inside of your client-side JavaScript files. | ||
@@ -25,2 +25,3 @@ ## Installation ## | ||
``` | ||
You might have noticed that you can pass stringify an optional array of file-extensions that you want to require() in your Browserify packages as strings. By default these are used: .html, .txt, .text, and .tmpl | ||
@@ -60,3 +61,5 @@ | ||
template = require('my/template/path.hbs'), | ||
data = require('data.json'); | ||
data = { | ||
"json_data": "This is my string!" | ||
}; | ||
@@ -81,7 +84,21 @@ var hbs_template = Handlebars.compile(template); | ||
data.json | ||
```json | ||
{ | ||
"json_data": "This is my string!" | ||
} | ||
``` | ||
## Contributing ## | ||
If you would like to contribute code, please do the following: | ||
1. Fork this repository and make your changes. | ||
2. Write tests for any new functionality. If you are fixing a bug that tests did not cover, please make a test that reproduces the bug. | ||
3. Add your name to the "contributors" section in the `package.json` file. | ||
4. Squash all of your commits into a single commit via `git rebase -i`. | ||
5. Run the tests by running `npm install && make test` from the source directory. | ||
6. Assuming those pass, send the Pull Request off to me for review! | ||
Please do not iterate the package.json version number – I will do that myself when I publish it to NPM. | ||
### Style-Guide ### | ||
Please follow this simple style-guide for all code contributions: | ||
* Indent using spaces. | ||
* camelCase all callables. | ||
* Place a space after a conditional or function name, and its conditions/arguments. `function (...) {...}` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11786
7
207
102
2
1
1