Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
(none) committed Nov 9, 2016
0 parents commit 788f001
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (C) 2016 MAK

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
karma-regex-preprocessor
========================

A karma preprocessor which does one or more regular expression substitutions on the file contents. It effectively just calls `String.prototype.replace()` with the parameters specified in the configuration.

The main motivation for this was that I needed something to quickly simulate the nginx [HttpSubModule](http://wiki.nginx.org/HttpSubModule).


Installation
------------

For the time being install from Github:

```sh
$ npm install 'git+https://github.com/makern/karma-regex-preprocessor.git' --save-dev
```


Configuration
-------------

Example configuration:

```js
// karma.conf.js
module.exports = function(config) {
config.set({
preprocessors: {
'**/*.js': ['regex']
},

regexPreprocessor: {
rules: [
// Simple string replace of 'foo' with 'bar'
[ 'foo', 'bar' ],
// Prefix all numbers with '-'
[ /[\d]+/g, '-$1'],
// Use a function to calculate replacement
[ '%rand%', function(match) { return Math.random() } ]
]
}
});
};
```


License
-------

The MIT License (MIT)
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
'preprocessor:regex': ['factory', require('./src/regex')]
};

21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "karma-regex-preprocessor",
"version": "0.0.1",
"description": "Regular expression replace preprocessor for karma test runner",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository":{
"type":"git",
"url":"https://github.com/makern/karma-regex-preprocessor.git"
},
"keywords": [
"karma-plugin",
"karma-preprocessor"
],
"author": "MAK",
"license": "MIT",
"dependencies": {
}
}
23 changes: 23 additions & 0 deletions src/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

var createRegexPreprocessor = function (config, helper, logger) {

var log = logger.create('preprocessor.regex');
var regexConfig = config.regexPreprocessor || {};

var rules = regexConfig.rules || [];

return function (content, file, done) {
log.debug('Processing "%s".', file.originalPath);

for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
content = content.replace(rule[0], rule[1]);
}

done(content);
}
};

createRegexPreprocessor.$inject = ['config', 'helper', 'logger'];

module.exports = createRegexPreprocessor;

0 comments on commit 788f001

Please sign in to comment.