Added CrossFilter definition file and tests

This commit is contained in:
Schmuli Raskin
2013-10-31 13:25:59 +02:00
parent 934e166b17
commit 21bb287c2a
4 changed files with 248 additions and 1 deletions

View File

@@ -48,6 +48,7 @@ List of Definitions
* [Chrome App](http://developer.chrome.com/apps/) (by [Adam Lay](https://github.com/AdamLay))
* [CodeMirror](http://codemirror.net) (by [François de Campredon](https://github.com/fdecampredon))
* [Commander](http://github.com/visionmedia/commander.js) (by [Marcelo Dezem](https://github.com/mdezem))
* [Crossfilter](https://github.com/square/crossfilter) (by [Schmulik Raskin](https://github.com/schmuli))
* [d3.js](http://d3js.org/) (from TypeScript samples)
* [dhtmlxGantt](http://dhtmlx.com/docs/products/dhtmlxGantt) (by [Maksim Kozhukh](http://github.com/mkozhukh))
* [dhtmlxScheduler](http://dhtmlx.com/docs/products/dhtmlxScheduler) (by [Maksim Kozhukh](http://github.com/mkozhukh))
@@ -234,7 +235,6 @@ List of Definitions
Requested Definitions
---------------------
* [jQuery ScrollTo](https://github.com/balupton/jquery-scrollto)
* [Crossfilter](https://github.com/square/crossfilter)
* [dc.js](https://github.com/NickQiZhu/dc.js)
* [google.visualizations](https://developers.google.com/chart/)
* [Prelude.ls](http://gkz.github.com/prelude-ls/)

View File

@@ -0,0 +1,136 @@
/// <reference path="crossfilter.d.ts" />
interface Payment {
date: string;
quantity: number;
total: number;
tip: number;
type: string;
}
var payments = crossfilter<Payment>([
{date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
{date: "2011-11-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:54:06Z", quantity: 1, total: 100, tip: 0, type: "cash"},
{date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
{date: "2011-11-14T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
]);
var paymentsByTotal = payments.dimension((d) => d.total);
// Filters
paymentsByTotal.filter([100, 200]); // selects payments whose total is between 100 and 200
paymentsByTotal.filter(120); // selects payments whose total equals 120
paymentsByTotal.filter(d => d % 2); // selects payments whose total is odd
paymentsByTotal.filter(null); // selects all payments
paymentsByTotal.filterExact(120); // selects payments whose total equals 120
paymentsByTotal.filterRange([100, 200]); // selects payments whose total is between 100 and 200
paymentsByTotal.filterFunction(d => d % 2); // selects payments whose total is odd
// Selects payments whose total is between 0 and 10 or 20 and 30:
paymentsByTotal.filterFunction(d => 0 <= d && d < 10 || 20 <= d && d < 30);
paymentsByTotal.filterAll(); // selects all payments
var topPayments = paymentsByTotal.top(4); // the top four payments, by total
topPayments[0]; // the biggest payment
topPayments[1]; // the second-biggest payment
var allPayments = paymentsByTotal.top(Infinity);
var bottomPayments = paymentsByTotal.bottom(4); // the bottom four payments, by total
bottomPayments[0]; // the smallest payment
bottomPayments[1]; // the second-smallest payment
var paymentGroupsByTotal = paymentsByTotal.group(total => Math.floor(total / 100));
paymentGroupsByTotal.size();
paymentGroupsByTotal.reduce((p, v) => p + 1, (p, v) => p - 1, () => 0);
paymentGroupsByTotal.reduceCount();
var paymentsByType = payments.dimension(d => d.type),
paymentVolumeByType = paymentsByType.group().reduceSum(d => d.total),
topTypes = paymentVolumeByType.top(1);
topTypes[0].key; // the top payment type (e.g., "tab")
topTypes[0].value; // the payment volume for that type (e.g., 900)
interface Group {
count: number;
total: number;
}
function reduceAdd(p: Group, v: Payment) {
++p.count;
p.total += v.total;
return p;
}
function reduceRemove(p: Group, v: Payment) {
--p.count;
p.total -= v.total;
return p;
}
function reduceInitial() : Group {
return {count: 0, total: 0};
}
function orderValue(p: Group) {
return p.total;
}
var topTotals = paymentVolumeByType
.reduce(reduceAdd, reduceRemove, reduceInitial)
.order(orderValue)
.top(2);
topTotals[0].key; // payment type with highest total (e.g., "tab")
topTotals[0].value; // reduced value for that type (e.g., {count:8, total:920})
paymentGroupsByTotal.orderNatural();
var paymentCountByType = paymentsByType.group();
topTypes = paymentCountByType.top(1);
topTypes[0].key; // the top payment type (e.g., "tab")
topTypes[0].value; // the count of payments of that type (e.g., 8)
var types = paymentCountByType.all();
paymentsByTotal.dispose();
crossfilter.bisect([], null, 0, 0);
var bisectBy = crossfilter.bisect.by(t => t);
bisectBy([], null, 0, 0);
bisectBy.left([], null, 0, 0);
bisectBy.right([], null, 0, 0);
crossfilter.heap([], 0, 0);
var heapBy = crossfilter.heap.by(t => t);
heapBy([], 0, 0);
heapBy.sort([], 0, 0);
crossfilter.heapselect([], 0, 0, 0);
var heapselectBy = crossfilter.heapselect.by<Payment>(t => t);
heapselectBy([], 0, 0, 0);
crossfilter.insertionsort([], 0, 0);
var insertionsortBy = crossfilter.insertionsort.by<Payment>(t => t);
insertionsortBy([], 0, 0);
crossfilter.quicksort([], 0, 0);
var quicksortBy = crossfilter.quicksort.by<Payment>(t => t);
quicksortBy([], 0, 0);
crossfilter.permute([], []);

111
crossfilter/crossfilter.d.ts vendored Normal file
View File

@@ -0,0 +1,111 @@
// Type definitions for CrossFilter
// Project: https://github.com/square/crossfilter
// Definitions by: Schmulik Raskin <https://github.com/schmuli>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module CrossFilter {
export interface Selector<T> {
(value: T): any;
}
export interface CrossFilterStatic {
<T>(data: T[]): CrossFilter<T>;
version: string;
permute<T>(array: T[], index: number[]): T[];
bisect: {
<T>(array: T[], value: T, lo: number, hi: number): number;
by<T>(value: Selector<T>): Bisector<T>;
}
heap: {
<T>(array: T[], lo: number, hi: number): T[];
by<T>(value: Selector<T>): Heap<T>;
}
heapselect: {
<T>(array: T[], lo: number, hi: number, k: number): T[];
by<T>(value: Selector<T>): HeapSelect<T>;
}
insertionsort: {
<T>(array: T[], lo: number, hi: number): T[];
by<T>(value: Selector<T>): Sort<T>;
}
quicksort: {
<T>(array: T[], lo: number, hi: number): T[];
by<T>(value: Selector<T>): Sort<T>;
}
}
export interface Bisection<T> {
(array: T[], value: T, lo: number, hi: number): number;
}
export interface Bisector<T> extends Bisection<T> {
left: Bisection<T>
right: Bisection<T>
}
export interface Heap<T> {
(array: T[], lo: number, hi: number): T[];
sort(array: T[], lo: number, hi: number): T[];
}
export interface HeapSelect<T> {
(array: T[], lo: number, hi: number, k: number): T[];
}
export interface Sort<T> {
(array: T[], lo: number, hi: number): T[];
}
export interface GroupAll<T> {
reduce<TValue>(add: (p: TValue, v: T) => TValue, remove: (p: TValue, v: T) => TValue, initial: () => TValue): GroupAll<T>;
reduceCount(): GroupAll<T>;
reduceSum(value: Selector<T>): GroupAll<T>;
dispose(): GroupAll<T>;
value(): T;
}
export interface Grouping<TKey, TValue> {
key: TKey;
value: TValue;
}
export interface Group<T, TKey, TValue> {
top(k: number): Grouping<TKey, TValue>[];
all(): Grouping<TKey, TValue>[];
reduce<TGroup>(add: (p: TGroup, v: T) => TGroup, remove: (p: TGroup, v: T) => TGroup, initial: () => TGroup): Group<T, TKey, TGroup>;
reduceCount(): Group<T, TKey, number>;
reduceSum<TGroup>(value: (data: T) => TGroup): Group<T, TKey, TGroup>;
order(value?: Selector<TValue>): Group<T, TKey, TValue>;
orderNatural(): Group<T, TKey, TValue>;
size(): number;
dispose(): Group<T, TKey, TValue>;
}
export interface CrossFilter<T> {
add(records: T[]): CrossFilter<T>;
remove(): CrossFilter<T>;
size(): number;
groupAll(): GroupAll<T>;
dimension<TDimension>(value: (data: T) => TDimension): Dimension<T, TDimension>;
}
export interface Dimension<T, TDimension> {
filter(value: TDimension[]): Dimension<T, TDimension>;
filter(value: TDimension): Dimension<T, TDimension>;
filter(value: Selector<TDimension>): Dimension<T, TDimension>;
filterExact(value: TDimension): Dimension<T, TDimension>;
filterRange(value: TDimension[]): Dimension<T, TDimension>;
filterFunction(value: Selector<TDimension>): Dimension<T, TDimension>;
filterAll(): Dimension<T, TDimension>;
top(k: number): T[];
bottom(k: number): T[];
dispose(): void;
group(): Group<T, TDimension, number>;
group<TGroup>(groupValue: (data: T) => TGroup): Group<T, TDimension, TGroup>;
groupAll(): GroupAll<T>;
}
}
declare var crossfilter: CrossFilter.CrossFilterStatic;

View File