Generic graphlib typings.

Remove all usages of any in graph class.
Simplify array for loop.
Make graphlib typings non global.
Add clean typings npm script.
This commit is contained in:
Oscar Lorentzon 2016-08-24 17:30:07 -04:00
parent 11d287e217
commit c8f3164bbd
5 changed files with 37 additions and 25 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ typings/*
!typings/created/
# File types
*.DS_Store
*.log
*.tgz
.#*

View File

@ -75,6 +75,7 @@
"build-docs": "typedoc --mode file --target ES5 --module commonjs --theme default --excludeExternals --name MapillaryJS --out docs/",
"build-min": "browserify src/Mapillary.ts --plugin tsify --transform brfs --standalone Mapillary | uglifyjs > dist/mapillary-js.min.js",
"clean": "rm -rf dist && mkdir dist",
"clean-typings": "rm -rf typings/modules && rm -rf typings/globals && rm typings/index.d.ts",
"copy-assets": "cp -a styles/*.svg dist",
"lint": "npm run lint-spec && npm run lint-src",
"lint-spec": "tslint -c tslint.json ./spec/**/*.ts ./spec/**/**/*.ts",

View File

@ -36,7 +36,7 @@ export class Graph {
private _sequences: SequenceHash;
private _sequenceHashes: {[skey: string]: SequenceHash};
private _graph: any;
private _graph: graphlib.Graph<Node, IEdgeData>;
private _nodeIndex: rbush.RBush<ISpatialItem>;
private _unWorthyNodes: {[key: string]: boolean};
@ -55,7 +55,7 @@ export class Graph {
this._sequences = {};
this._sequenceHashes = {};
this._nodeIndex = rbush<ISpatialItem>(16, [".lon", ".lat", ".lon", ".lat"]);
this._graph = new graphlib.Graph({multigraph: true});
this._graph = new graphlib.Graph<Node, IEdgeData>({ multigraph: true });
this._unWorthyNodes = {};
this._edgeCalculator = new EdgeCalculator();
this._spatial = new Spatial();
@ -159,13 +159,13 @@ export class Graph {
* @return {IEdge}
*/
public getEdges(node: Node): IEdge[] {
let outEdges: any[] = this._graph.outEdges(node.key);
let outEdges: graphlib.Edge[] = this._graph.outEdges(node.key);
return _.map(outEdges, (outEdge: any) => {
let edge: any = this._graph.edge(outEdge);
return _.map(outEdges, (outEdge: graphlib.Edge) => {
let data: IEdgeData = this._graph.edge(outEdge);
return {
data: <IEdgeData>edge,
data: data,
from: outEdge.v,
to: outEdge.w,
};
@ -195,7 +195,7 @@ export class Graph {
}
public nextKey(node: Node, dir: EdgeDirection): string {
let outEdges: any[] = this._graph.outEdges(node.key);
let outEdges: graphlib.Edge[] = this._graph.outEdges(node.key);
for (let outEdge of outEdges) {
let edgeData: IEdgeData = this._graph.edge(outEdge);
@ -214,14 +214,9 @@ export class Graph {
* @param {IEdge[]} edges
*/
private _addEdgesToNode(node: Node, edges: IEdge[]): void {
let outEdges: any[] = this._graph.outEdges(node.key);
let outEdges: graphlib.Edge[] = this._graph.outEdges(node.key);
for (let outEdgeKey in outEdges) {
if (!outEdges.hasOwnProperty(outEdgeKey)) {
continue;
}
let outEdge: any = outEdges[outEdgeKey];
for (let outEdge of outEdges) {
this._graph.removeEdge(outEdge);
}

View File

@ -7,7 +7,6 @@
"es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504",
"falcor": "file:typings/created/falcor/falcor.d.ts",
"falcor-http-datasource": "file:typings/created/falcor-http-datasource/falcor-http-datasource.d.ts",
"graphlib": "file:typings/created/graphlib/graphlib.d.ts",
"lib": "file:typings/created/lib/lib.d.ts",
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#aed176536a202b9a2475ce1989ea6d2d0226a185",
"rest": "github:DefinitelyTyped/DefinitelyTyped/rest/rest.d.ts#001ca36ba58cef903c4c063555afb07bbc36bb58",
@ -18,6 +17,7 @@
},
"dependencies": {
"earcut": "file:typings/created/earcut/earcut.d.ts",
"graphlib": "file:typings/created/graphlib/graphlib.d.ts",
"latlon-geohash": "file:typings/created/latlon-geohash/latlon-geohash.d.ts",
"pbf": "file:typings/created/pbf/pbf.d.ts",
"rbush": "file:typings/created/rbush/rbush.d.ts",

View File

@ -1,13 +1,28 @@
declare module "graphlib" {
export = graphlib;
declare module graphlib {
interface Options {
compound?: boolean;
directed?: boolean;
multigraph?: boolean;
}
module graphlib {
export class Graph {
constructor();
constructor(params: any);
hasNode(key: string): any;
node(key: string): any;
setNode(key: string, node: any): any;
}
interface Edge {
v: string;
w: string;
name?: string;
}
class Graph<TNodeLabel, TEdgeLabel> {
constructor(options: Options);
setNode(v: string, label?: TNodeLabel): void;
hasNode(v: string): boolean;
node(v: string): TNodeLabel;
setEdge(v: string, w: string, label?: TEdgeLabel, name?: string): void;
outEdges(v: string, w?: string): Edge[];
edge(edge: Edge): TEdgeLabel;
removeEdge(edge: Edge): void;
}
}
export = graphlib;