mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Landing pull request 523. Adds character frequency reporting tool, use: make freq. Fixes #10372.
More Details: - https://github.com/jquery/jquery/pull/523 - http://bugs.jquery.com/ticket/10372
This commit is contained in:
parent
46219b57ba
commit
9f5d56a8b5
7
Makefile
7
Makefile
@ -83,6 +83,13 @@ size: jquery min
|
||||
echo "You must have NodeJS installed in order to size jQuery."; \
|
||||
fi
|
||||
|
||||
freq: jquery min
|
||||
@@if test ! -z ${JS_ENGINE}; then \
|
||||
${JS_ENGINE} ${BUILD_DIR}/freq.js; \
|
||||
else \
|
||||
echo "You must have NodeJS installed to report the character frequency of minified jQuery."; \
|
||||
fi
|
||||
|
||||
min: jquery ${JQ_MIN}
|
||||
|
||||
${JQ_MIN}: ${JQ}
|
||||
|
79
build/freq.js
Normal file
79
build/freq.js
Normal file
@ -0,0 +1,79 @@
|
||||
#! /usr/bin/env node
|
||||
|
||||
var fs = require( "fs" );
|
||||
|
||||
function isEmptyObject( obj ) {
|
||||
for ( var name in obj ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function extend( obj ) {
|
||||
var dest = obj,
|
||||
src = [].slice.call( arguments, 1 );
|
||||
|
||||
Object.keys( src ).forEach(function( key ) {
|
||||
var copy = src[ key ];
|
||||
|
||||
for ( var prop in copy ) {
|
||||
dest[ prop ] = copy[ prop ];
|
||||
}
|
||||
});
|
||||
|
||||
return dest;
|
||||
};
|
||||
|
||||
function charSort( obj, callback ) {
|
||||
|
||||
var ordered = [],
|
||||
table = {},
|
||||
copied;
|
||||
|
||||
copied = extend({}, obj );
|
||||
|
||||
(function order() {
|
||||
|
||||
var largest = 0,
|
||||
c;
|
||||
|
||||
for ( var i in obj ) {
|
||||
if ( obj[ i ] >= largest ) {
|
||||
largest = obj[ i ];
|
||||
c = i;
|
||||
}
|
||||
}
|
||||
|
||||
ordered.push( c );
|
||||
delete obj[ c ];
|
||||
|
||||
if ( !isEmptyObject( obj ) ) {
|
||||
order();
|
||||
} else {
|
||||
ordered.forEach(function( val ) {
|
||||
table[ val ] = copied[ val ];
|
||||
});
|
||||
|
||||
callback( table );
|
||||
}
|
||||
|
||||
})();
|
||||
}
|
||||
function charFrequency( src, callback ) {
|
||||
var obj = {};
|
||||
|
||||
src.replace(/[^\w]|\d/gi, "").split("").forEach(function( c ) {
|
||||
obj[ c ] ? ++obj[ c ] : ( obj[ c ] = 1 );
|
||||
});
|
||||
|
||||
return charSort( obj, callback );
|
||||
}
|
||||
|
||||
|
||||
charFrequency( fs.readFileSync( "dist/jquery.min.js", "utf8" ), function( obj ) {
|
||||
var chr;
|
||||
|
||||
for ( chr in obj ) {
|
||||
console.log( " " + chr + " " + obj[ chr ] );
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user