forked from jquery/jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreq.js
79 lines (58 loc) · 1.21 KB
/
freq.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
76
77
78
#! /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 ] );
}
});