Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Mar 24, 2014
1 parent 425f7ba commit 1ce697a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/analyzer.js
Expand Up @@ -2,7 +2,7 @@ var _ = require('underscore');
var gzipSize = require('gzip-size');

/**
* Analyzer
* Analyzer class
* @param rules
* @param selectors
* @param declarations
Expand Down Expand Up @@ -67,14 +67,20 @@ Analyzer.prototype.analyzeRules = function () {
* }
*/
Analyzer.prototype.analyzeSelectors = function () {
var regexp = new RegExp(this.options.javascriptSpecificSelectors, 'g');

// object to return
var result = {
idSelectors: 0,
universalSelectors: 0,
unqualifiedAttributeSelectors: 0,
javascriptSpecificSelectors: 0,
identifiers: []
};

// specified JavaScript hook selector
var regexp = new RegExp(this.options.javascriptSpecificSelectors, 'g');

// analyze selectors
this.selectors.forEach(function(selector) {
if (selector.indexOf('#') > -1) {
result.idSelectors += 1;
Expand Down Expand Up @@ -114,6 +120,8 @@ Analyzer.prototype.analyzeSelectors = function () {
* }
*/
Analyzer.prototype.analyzeDeclarations = function () {

// object to return
var result = {
dataUriSize: '',
importantKeywords: 0,
Expand All @@ -122,6 +130,8 @@ Analyzer.prototype.analyzeDeclarations = function () {
uniqueColor: [],
properties: {}
};

// analyze declarations
this.declarations.forEach(function(declaration) {
if (declaration.value.indexOf('data:image') > -1) {
result.dataUriSize += declaration.value.match(/data\:image\/[A-Za-z0-9;,\+\=\/]+/);
Expand All @@ -145,17 +155,20 @@ Analyzer.prototype.analyzeDeclarations = function () {
} else {
result.properties[declaration.property] = 1;
}

});

// Return byte size.
result.dataUriSize = Buffer.byteLength(result.dataUriSize, 'utf8');

// Sort `font-size` property.
result.uniqueFontSize = _.sortBy(_.uniq(result.uniqueFontSize).slice(), function(item) {
return item.replace(/[^0-9\.]/g, '') - 0;
});

// Sort `color` property.
result.uniqueColor = _.sortBy(_.uniq(_.without(result.uniqueColor, 'TRANSPARENT')));
// Sort propertie count.

// Sort properties count.
var propertiesCount = [];
Object.keys(result.properties).forEach(function(key) {
propertiesCount.push({
Expand All @@ -166,6 +179,7 @@ Analyzer.prototype.analyzeDeclarations = function () {
result.properties = propertiesCount.sort(function decreasingOrder(a, b) {
return b.count - a.count;
});

return result;
};

Expand Down Expand Up @@ -198,6 +212,8 @@ Analyzer.prototype.analyzeDeclarations = function () {
* }
*/
Analyzer.prototype.analyze = function () {

// get analytics
var ruleAnalysis = this.analyzeRules();
var selectorAnalysis = this.analyzeSelectors();
var declarationAnalysis = this.analyzeDeclarations();
Expand Down
20 changes: 20 additions & 0 deletions lib/parser.js
Expand Up @@ -5,6 +5,11 @@ var Promise = require('promise');
var cssParse = require('css-parse');
var request = require('request');

/**
* get promised request
* @param url
* @returns {Promise}
*/
function requestSync(url) {
return new Promise(function(resolve, reject) {
request(url, function(error, response, body) {
Expand All @@ -19,11 +24,21 @@ function requestSync(url) {
});
}

/**
* Parser class
* @param {Array} urls
* @param {Array} files
* @constructor
*/
function Parser(urls, files) {
this.urls = urls;
this.files = files;
}

/**
* Parse css data
* @param callback
*/
Parser.prototype.parse = function (callback) {
var result = {
cssString: '',
Expand All @@ -44,16 +59,21 @@ Parser.prototype.parse = function (callback) {
// they will be joined into css string
var styles = [];
this.files.forEach(function(file) {
// push local css data
styles.push(fs.readFileSync(file, {
encoding: "utf-8"
}));
});

Promise.all(requestPromises).done(function onFulfilled(value) {
// push remote css data
styles.push(value.join(''));

// join all css string
result.cssString = styles.join('');
result.cssSize = Buffer.byteLength(result.cssString, 'utf8');

// parse css string
var rawRules = cssParse(result.cssString).stylesheet.rules;
rawRules.forEach(function(rule) {
if (rule.type === 'rule') {
Expand Down

0 comments on commit 1ce697a

Please sign in to comment.