Permalink
Browse files

Update dependencies

  • Loading branch information...
t32k committed Mar 24, 2017
1 parent 3b13ffe commit 2d30c0360473b8a957537615a5e68cf3c13f7ac6
Showing with 144 additions and 141 deletions.
  1. +1 −1 README.md
  2. +36 −36 lib/analyzer.js
  3. +18 −18 lib/parser.js
  4. +1 −1 lib/stylestats.js
  5. +87 −84 package.json
  6. +1 −1 test/argument.js
View
@@ -1,4 +1,4 @@
# [StyleStats](http://www.stylestats.org/) [![Build Status](https://secure.travis-ci.org/t32k/stylestats.svg?branch=master)](http://travis-ci.org/t32k/stylestats) [![Dependency Status](https://david-dm.org/t32k/stylestats.svg)](https://david-dm.org/t32k/stylestats) [![Coverage Status](http://img.shields.io/coveralls/t32k/stylestats.svg)](https://coveralls.io/r/t32k/stylestats) [![Code Climate](http://img.shields.io/codeclimate/github/t32k/stylestats.svg)](https://codeclimate.com/github/t32k/stylestats) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
# [StyleStats](http://www.stylestats.org/) [![Build Status](https://secure.travis-ci.org/t32k/stylestats.svg?branch=master)](http://travis-ci.org/t32k/stylestats) [![Dependency Status](https://david-dm.org/t32k/stylestats.svg)](https://david-dm.org/t32k/stylestats) [![Coverage Status](http://img.shields.io/coveralls/t32k/stylestats.svg)](https://coveralls.io/r/t32k/stylestats) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
## Installation
View
@@ -10,25 +10,25 @@ class Analyzer {
* @param {Object} options
*/
constructor(data, options) {
// array of rule
// referenced in analyzeRules
// Array of rule
// Referenced in analyzeRules
this.rules = data.rules;
// array of css selector
// referenced in analyzeSelectors
// Array of css selector
// Referenced in analyzeSelectors
this.selectors = data.selectors;
// array of css declaration
// referenced in analyzeDeclarations
// Array of css declaration
// Referenced in analyzeDeclarations
this.declarations = data.declarations;
// all of css string
// All of css string
this.cssString = data.cssString;
// size of css
// Size of css
this.cssSize = data.cssSize;
// result options
// Result options
this.options = options;
}
@@ -40,13 +40,13 @@ class Analyzer {
* }
*/
analyzeRules() {
// object to return
// Object to return
const result = {
totalCssDeclarations: 0,
cssDeclarations: []
};
// analyze rules
// Analyze rules
this.rules.forEach(rule => {
if (Array.isArray(rule.declarations)) {
result.cssDeclarations.push({
@@ -56,7 +56,7 @@ class Analyzer {
}
});
// sort by css declaration count
// Sort by css declaration count
result.cssDeclarations.sort((a, b) => {
return b.count - a.count;
});
@@ -79,7 +79,7 @@ class Analyzer {
* }
*/
analyzeSelectors() {
// object to return
// Object to return
const result = {
idSelectors: 0,
universalSelectors: 0,
@@ -90,45 +90,45 @@ class Analyzer {
identifiers: []
};
// specified JavaScript hook selector
// Specified JavaScript hook selector
const regexpJs = new RegExp(this.options.javascriptSpecificSelectors);
// specified user-specified hook selector
// Specified user-specified hook selector
const regexpUser = new RegExp(this.options.userSpecifiedSelectors);
// analyze selectors
// Analyze selectors
this.selectors.forEach(selector => {
// if it contains # and dose not contain # in attribute selector
// If it contains # and dose not contain # in attribute selector
if (selector.indexOf('#') > -1) {
const id = selector.replace(/\[.+]/g, '');
if (id.indexOf('#') > -1) {
result.idSelectors += 1;
}
}
// if it contains * and dose not contain * in attribute selector
// If it contains * and dose not contain * in attribute selector
if (selector.indexOf('*') > -1) {
const universal = selector.replace(/\[.+]/g, '');
if (universal.indexOf('*') > -1) {
result.universalSelectors += 1;
}
}
// if it is unqualified attribute selector
// If it is unqualified attribute selector
if (selector.trim().match(/\[.+]$/g)) {
result.unqualifiedAttributeSelectors += 1;
}
// if it is for JavaScript hook
// If it is for JavaScript hook
if (regexpJs.test(selector.trim())) {
result.javascriptSpecificSelectors += 1;
}
// if it is for user-specified hook
// If it is for user-specified hook
if (regexpUser.test(selector.trim())) {
result.userSpecifiedSelectors += 1;
}
// add selector for statistics
// Add selector for statistics
let trimmedSelector = selector.replace(/\s?([>|+|~])\s?/g, '$1');
trimmedSelector = trimmedSelector.replace(/\s+/g, ' ');
const count = trimmedSelector.split(/\s|>|\+|~|:|[\w\]]\.|[\w\]]#|\[/).length;
@@ -141,7 +141,7 @@ class Analyzer {
result.totalIdentifiers += obj.count;
});
// sort by chained selector count
// Sort by chained selector count
result.identifiers.sort((a, b) => {
return b.count - a.count;
});
@@ -162,7 +162,7 @@ class Analyzer {
* }
*/
analyzeDeclarations() {
// object to return
// Object to return
const result = {
dataUriSize: '',
importantKeywords: 0,
@@ -174,41 +174,41 @@ class Analyzer {
properties: {}
};
// analyze declarations
// Analyze declarations
this.declarations.forEach(declaration => {
// if it contains DataURI
// If it contains DataURI
if (declaration.value.indexOf('data:image') > -1) {
result.dataUriSize += declaration.value.match(/data:image\/[A-Za-z0-9;,+=/]+/);
}
// if it contains !important keyword
// If it contains !important keyword
if (declaration.value.indexOf('!important') > -1) {
result.importantKeywords += 1;
}
// if it contains float
// If it contains float
if (declaration.property.indexOf('float') > -1) {
result.floatProperties += 1;
}
// if it contains font-family
// If it contains font-family
if (declaration.property.indexOf('font-family') > -1) {
result.uniqueFontFamilies.push(declaration.value.replace(/(!important)/g, '').trim());
}
// if it contains font-size
// If it contains font-size
if (declaration.property.indexOf('font-size') > -1) {
result.uniqueFontSizes.push(declaration.value.replace(/!important/, '').trim());
}
// if it contains colors
// If it contains colors
if (declaration.property.match(/^color$/)) {
let color = declaration.value.replace(/!important/, '');
color = color.toUpperCase().trim();
result.uniqueColors.push(color);
}
// if it contains background-image url()
// If it contains background-image url()
if (declaration.property.indexOf('background') > -1 && declaration.value.indexOf('url') > -1) {
const paths = declaration.value.match(/url\(([^)]+)\)/g);
if (paths) {
@@ -218,7 +218,7 @@ class Analyzer {
}
}
// property statistics
// Property statistics
if (result.properties[declaration.property]) {
result.properties[declaration.property] += 1;
} else {
@@ -272,7 +272,7 @@ class Analyzer {
return array.indexOf(formattedColor) === index;
}).sort();
// if it contains background-image url()
// If it contains background-image url()
result.uniqueBackgroundImages = result.uniqueBackgroundImages.filter((backgroundImage, index, array) => {
return array.indexOf(backgroundImage) === index;
}).sort();
@@ -286,7 +286,7 @@ class Analyzer {
});
});
// sort by property count
// Sort by property count
result.properties = propertiesCount.sort((a, b) => {
return b.count - a.count;
});
@@ -331,7 +331,7 @@ class Analyzer {
* }
*/
analyze() {
// get analytics
// Get analytics
const ruleAnalysis = this.analyzeRules();
const selectorAnalysis = this.analyzeSelectors();
const declarationAnalysis = this.analyzeDeclarations();
View
@@ -19,7 +19,7 @@ function requestPromise(options) {
} else if (error) {
reject(error);
} else {
reject(`Status code is ${response.statusCode}`);
reject(new Error(`Status code is ${response.statusCode}`));
}
});
});
@@ -56,7 +56,7 @@ class Parser {
* @returns {Promise}
*/
parse() {
// object to return
// Object to return
const parsedData = {
cssString: '',
cssSize: 0,
@@ -70,7 +70,7 @@ class Parser {
const that = this;
// remote file requests
// Remote file requests
const requestPromises = [];
this.urls.forEach(url => {
const options = that.options.requestOptions;
@@ -79,42 +79,42 @@ class Parser {
requestPromises.push(requestPromise(options));
});
// css string array from arguments
// they will be joined into css string
// CSS string array from arguments
// They will be joined into css string
this.cssFiles.forEach(cssFile => {
// push local css data
// Push local css data
that.styles.push(fs.readFileSync(cssFile, {
encoding: 'utf8'
}));
});
return new Promise((resolve, reject) => {
// get remote files
// Get remote files
Promise.all(requestPromises).then(results => {
if (that.urls.length > 0 && that.files.length > 0 && that.styles.length > 0) {
throw new Error('Argument is invalid');
}
// requests to stylesheet defined in html
// Requests to stylesheet defined in html
const requestPromisesInner = [];
results.forEach(result => {
if (util.isCSS(result)) {
that.styles.push(result);
} else {
// push remote css data
// Push remote css data
const type = result.headers['content-type'];
if (type.indexOf('html') > -1) {
// parse result body
// Parse result body
const $ = cheerio.load(result.body);
const $link = $('link[rel=stylesheet]');
const $style = $('style');
// add css file count
// Add css file count
parsedData.cssFiles += $link.length;
parsedData.styleElements += $style.length;
// request link[href]
// Request link[href]
$link.each(function () {
const relativePath = $(this).attr('href');
const absolutePath = url.resolve(result.request.href, relativePath);
@@ -123,7 +123,7 @@ class Parser {
requestPromisesInner.push(requestPromise(options));
});
// add text in style tags
// Add text in style tags
$style.each(function () {
that.styles.push($(this).text());
});
@@ -147,11 +147,11 @@ class Parser {
});
}
// join all css string
// Join all css string
parsedData.cssString = that.styles.join('');
parsedData.cssSize = Buffer.byteLength(parsedData.cssString, 'utf8');
// parse css string
// Parse css string
let rawRules = [];
try {
@@ -160,12 +160,12 @@ class Parser {
throw new Error(error);
}
// check number of rules
// Check number of rules
if (rawRules[0] === undefined) {
throw new Error('Rule is not found.');
}
// add rules into result
// Add rules into result
rawRules.forEach(rule => {
if (rule.type === 'rule') {
parsedData.rules.push(rule);
@@ -179,7 +179,7 @@ class Parser {
}
});
// add selectors and declarations into result
// Add selectors and declarations into result
parsedData.rules.forEach(rule => {
rule.selectors.forEach(selector => {
parsedData.selectors.push(selector);
View
@@ -19,7 +19,7 @@ class StyleStats {
this.styles = [];
const EXTENSIONS = ['.css'];
// check arguments which is url or file path or other
// Check arguments which is url or file path or other
args.forEach(arg => {
if (util.isFile(arg) && EXTENSIONS.indexOf(path.extname(arg)) !== -1) {
this.files.push(arg);
Oops, something went wrong.

0 comments on commit 2d30c03

Please sign in to comment.