reductio
Version:
Reductio: Crossfilter groupings
284 lines (225 loc) • 6.17 kB
JavaScript
import parameters from './parameters.js';
function assign(target) {
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
var output = Object(target);
for (var index = 1; index < arguments.length; ++index) {
var source = arguments[index];
if (source != null) {
for (var nextKey in source) {
if(source.hasOwnProperty(nextKey)) {
output[nextKey] = source[nextKey];
}
}
}
}
return output;
}
function accessor_build(obj, p) {
// obj.order = function(value) {
// if (!arguments.length) return p.order;
// p.order = value;
// return obj;
// };
// Converts a string to an accessor function
function accessorify(v) {
if( typeof v === 'string' ) {
// Rewrite to a function
var tempValue = v;
var func = function (d) { return d[tempValue]; }
return func;
} else {
return v;
}
}
// Converts a string to an accessor function
function accessorifyNumeric(v) {
if( typeof v === 'string' ) {
// Rewrite to a function
var tempValue = v;
var func = function (d) { return +d[tempValue]; }
return func;
} else {
return v;
}
}
obj.fromObject = function(value) {
if(!arguments.length) return p;
assign(p, value);
return obj;
};
obj.toObject = function() {
return p;
};
obj.count = function(value, propName) {
if (!arguments.length) return p.count;
if (!propName) {
propName = 'count';
}
p.count = propName;
return obj;
};
obj.sum = function(value) {
if (!arguments.length) return p.sum;
value = accessorifyNumeric(value);
p.sum = value;
return obj;
};
obj.avg = function(value) {
if (!arguments.length) return p.avg;
value = accessorifyNumeric(value);
// We can take an accessor function, a boolean, or a string
if( typeof value === 'function' ) {
if(p.sum && p.sum !== value) console.warn('SUM aggregation is being overwritten by AVG aggregation');
p.sum = value;
p.avg = true;
p.count = 'count';
} else {
p.avg = value;
}
return obj;
};
obj.exception = function(value) {
if (!arguments.length) return p.exceptionAccessor;
value = accessorify(value);
p.exceptionAccessor = value;
return obj;
};
obj.filter = function(value) {
if (!arguments.length) return p.filter;
p.filter = value;
return obj;
};
obj.valueList = function(value) {
if (!arguments.length) return p.valueList;
value = accessorify(value);
p.valueList = value;
return obj;
};
obj.median = function(value) {
if (!arguments.length) return p.median;
value = accessorifyNumeric(value);
if(typeof value === 'function') {
if(p.valueList && p.valueList !== value) console.warn('VALUELIST accessor is being overwritten by median aggregation');
p.valueList = value;
}
p.median = value;
return obj;
};
obj.min = function(value) {
if (!arguments.length) return p.min;
value = accessorifyNumeric(value);
if(typeof value === 'function') {
if(p.valueList && p.valueList !== value) console.warn('VALUELIST accessor is being overwritten by min aggregation');
p.valueList = value;
}
p.min = value;
return obj;
};
obj.max = function(value) {
if (!arguments.length) return p.max;
value = accessorifyNumeric(value);
if(typeof value === 'function') {
if(p.valueList && p.valueList !== value) console.warn('VALUELIST accessor is being overwritten by max aggregation');
p.valueList = value;
}
p.max = value;
return obj;
};
obj.exceptionCount = function(value) {
if (!arguments.length) return p.exceptionCount;
value = accessorify(value);
if( typeof value === 'function' ) {
if(p.exceptionAccessor && p.exceptionAccessor !== value) console.warn('EXCEPTION accessor is being overwritten by exception count aggregation');
p.exceptionAccessor = value;
p.exceptionCount = true;
} else {
p.exceptionCount = value;
}
return obj;
};
obj.exceptionSum = function(value) {
if (!arguments.length) return p.exceptionSum;
value = accessorifyNumeric(value);
p.exceptionSum = value;
return obj;
};
obj.histogramValue = function(value) {
if (!arguments.length) return p.histogramValue;
value = accessorifyNumeric(value);
p.histogramValue = value;
return obj;
};
obj.histogramBins = function(value) {
if (!arguments.length) return p.histogramThresholds;
p.histogramThresholds = value;
return obj;
};
obj.std = function(value) {
if (!arguments.length) return p.std;
value = accessorifyNumeric(value);
if(typeof(value) === 'function') {
p.sumOfSquares = value;
p.sum = value;
p.count = 'count';
p.std = true;
} else {
p.std = value;
}
return obj;
};
obj.sumOfSq = function(value) {
if (!arguments.length) return p.sumOfSquares;
value = accessorifyNumeric(value);
p.sumOfSquares = value;
return obj;
};
obj.value = function(value, accessor) {
if (!arguments.length || typeof value !== 'string' ) {
console.error("'value' requires a string argument.");
} else {
if(!p.values) p.values = {};
p.values[value] = {};
p.values[value].parameters = parameters();
accessor_build(p.values[value], p.values[value].parameters);
if(accessor) p.values[value].accessor = accessor;
return p.values[value];
}
};
obj.nest = function(keyAccessorArray) {
if(!arguments.length) return p.nestKeys;
keyAccessorArray.map(accessorify);
p.nestKeys = keyAccessorArray;
return obj;
};
obj.alias = function(propAccessorObj) {
if(!arguments.length) return p.aliasKeys;
p.aliasKeys = propAccessorObj;
return obj;
};
obj.aliasProp = function(propAccessorObj) {
if(!arguments.length) return p.aliasPropKeys;
p.aliasPropKeys = propAccessorObj;
return obj;
};
obj.groupAll = function(groupTest) {
if(!arguments.length) return p.groupAll;
p.groupAll = groupTest;
return obj;
};
obj.dataList = function(value) {
if (!arguments.length) return p.dataList;
p.dataList = value;
return obj;
};
obj.custom = function(addRemoveInitialObj) {
if (!arguments.length) return p.custom;
p.custom = addRemoveInitialObj;
return obj;
};
}
var accessors = {
build: accessor_build
};
export default accessors;