Code coverage report for lib/css2modernizr.js

Statements: 54.29% (19 / 35)      Branches: 41.67% (5 / 12)      Functions: 50% (3 / 6)      Lines: 54.29% (19 / 35)      Ignored: none     

All files » lib/ » css2modernizr.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                    1       1 1 1 1 1 1     1 1   1 1 1 1                                     1   1                       1               1 1     1  
/*
 * css2modernizr
 * https://github.com/vovanbo/css2modernizr
 *
 * Copyright (c) 2014 Vladimir Bolshakov
 * Licensed under the MIT license.
 */
 
'use strict';
 
var _ = require('lodash'),
    postcss = require('postcss'),
    tests = require('./tests_and_dependencies.json');
 
function CSS2Modernizr(css, prefix) {
  this.prefix = prefix || '';
  this.css = css || '';
  this.config = '';
  this.used_modules = [];
  this.download_url = 'http://modernizr.com/download/';
}
 
CSS2Modernizr.prototype.usage = function () {
  var regex, css, results, modules;
 
  results = [];
  regex = new RegExp("\\." + this.prefix + "(no-)?(" + _.keys(tests).join('|').toLowerCase() + ")", "g");
  css = postcss.parse(this.css);
  css.eachRule(function (rule) {
    _.forEach(rule.selectors, function(selector) {
      var found, key, index;
      found = regex.exec(selector);
      if (found) {
        key = found[2];
        index = _.findKey(results, { 'name': key });
        if (index) {
          results[index]["count"]++;
        } else {
          results.push({
            'name': key,
            'count': 1
          });
        }
      }
    });
  });
 
  results = _.sortBy(results, "count").reverse();
 
  Iif (!_.isEmpty(results)) {
    this.config = this.download_url + '#-';
    modules = [];
    _.forEach(results, function (result) {
      var test = tests[result['name']];
      modules.push(test.dependencies);
    });
 
    this.used_modules = _.uniq(_.flatten(modules)).sort();
    this.config += this.used_modules.join("-") + ( this.prefix ? '-cssclassprefix:' + this.prefix.replace(/\-/g, '!') : '' );
  }
 
  return {
    'prefix': this.prefix,
    'usage': results,
    'download_url': this.config,
    'used_modules': this.used_modules
  };
};
 
var css2modernizr = function(css, prefix) {
  return new CSS2Modernizr(css, prefix);
};
 
module.exports = css2modernizr;