-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
(none)
committed
Nov 9, 2016
0 parents
commit 788f001
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
'preprocessor:regex': ['factory', require('./src/regex')] | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |