Skip to content

Engineer2B/karma-regex-preprocessor

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

karma-regex-preprocessor

npm

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.

Historically, the main motivation for this was that https://github.com/mode needed something to quickly simulate the nginx HttpSubModule.

Installation

Install from npm:

npm install karma-regex-preprocessor --save-dev

Configuration

Example configuration:

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

    regexPreprocessor: {
      rules: [
        {
          // Replacements for File1.
          fileName: "File1.js", replacement: [
            // Simple string replace of 'foo' with 'bar'.
            { replace: "foo", with: "bar" },
            // Prefix all numbers with '-'.
            { replace: /[\d]+/g, with: "-$1" },
            // Use a function to calculate replacement.
            { replace: "%rand%", with: function (match) { return Math.random() } }
          ]
        },
        {
          // Replacements for File2.
          fileName: "File2.js", replacement: [
            // Simple string replace of 'foo' with 'baz'.
            { replace: "foo", with: "baz" },
            // Prefix all numbers with '€'.
            { replace: /[\d]+/g, with: "€$1" },
            // Use a function to calculate replacement.
            { replace: "%rand%", with: function (match) { return Math.random()*5 } }
          ]
        }
      ]
    }
  });
};

License

The MIT License (MIT)

About

A karma preprocessor which does one or more regular expression substitutions the contents of a file.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%