Skip to content

Instantly share code, notes, and snippets.

@ericdum
Created November 24, 2013 02:00
Show Gist options
  • Save ericdum/7622426 to your computer and use it in GitHub Desktop.
Save ericdum/7622426 to your computer and use it in GitHub Desktop.
将第一个CSV字符串参数,按纵横比分类。 测试数据:"120×240 ,120×600 ,,160×600 ,200×200 ,250×250 ,300×250 ,336×280 ,468×60 ,728×90 ,970×90 ,,120×240 ,180×150 ,234×60 ,300×600 ,,160×600 ,250×250 ,300×250 ,336×280 ,468×60 ,640×90 ,728×90 ,760×90 ,950×90 , ,120×240 ,250×300 ,300×100,660×90 ,250×180,600×90,650×90 ,360×300,500×200,,1000×90 ,960×90 ,300×250 ,220×120 ,300×50 ,…
/**
*
* 将第一个CSV字符串参数,按纵横比分类
* usage: analytics_specs("800*1600, 400*800, 800*2000", "x", ",")
*/
function analytics_specs( string, a, b ) {
a = a || '×';
b = b || ',';
var array = string.split(b);
//去掉空格
for(var i in array){
array[i] = array[i].replace(/\s+/,'')
}
//分长宽比储存
var stored = '';
var result = {};
if(this == window) this.get_ratio = analytics_specs.prototype.get_ratio;
for(var i in array){
//去重
if( stored.match(new RegExp("(^|,)"+array[i]+"($|,)")) ) continue;
stored += array[i]+",";
//按比例储存
var s = array[i].split(a);
var r = (s[0]/s[1]).toString();
r = this.get_ratio(r);
if( r in result ) {
result[r].push(array[i]);
} else {
result[r] = [array[i]];
}
}
for(var i in result){
var s = result[i];
s.sort(function(v1, v2){
return v1.split(a)[0] > v2.split(a)[1];
})
result[i] = s;
}
for(var i in result){
console.log( i );
for(var j in result[i]){
console.log( result[i][j] );
}
}
return result;
}
analytics_specs.prototype.get_ratio = function ( r ) {
r = parseFloat(r);
if( r <= 1/4 ) return '0.25';
else if( r < 3/4 ) return '0.5';
else if( r < 4/3 ) return '1';
else if( r < 4/1 ) return '2';
else if( r >= 4/1 ) return '4';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment