From 6094d751a527c2f4e497794849988254d06cec0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20L=C3=B3pez=20Fueyo?= Date: Mon, 9 Jul 2018 23:13:32 +0200 Subject: [PATCH] Add definitions for node-dijkstra (#27126) * Added types for node-dijkstra * Removed default export * Changed name type to String and parameter renamed * Fixed string types. --- types/node-dijkstra/index.d.ts | 148 +++++++++++++++++++++ types/node-dijkstra/node-dijkstra-tests.ts | 12 ++ types/node-dijkstra/tsconfig.json | 24 ++++ types/node-dijkstra/tslint.json | 1 + 4 files changed, 185 insertions(+) create mode 100644 types/node-dijkstra/index.d.ts create mode 100644 types/node-dijkstra/node-dijkstra-tests.ts create mode 100644 types/node-dijkstra/tsconfig.json create mode 100644 types/node-dijkstra/tslint.json diff --git a/types/node-dijkstra/index.d.ts b/types/node-dijkstra/index.d.ts new file mode 100644 index 0000000000..20c97d7425 --- /dev/null +++ b/types/node-dijkstra/index.d.ts @@ -0,0 +1,148 @@ +// Type definitions for node-dijkstra 2.5 +// Project: https://github.com/albertorestifo/node-dijkstra +// Definitions by: Jorge López +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare class Graph { + /** + * Creates a new Graph, optionally initializing it a nodes graph representation. + * + * A graph representation is an object that has as keys the name of the point and as values + * the points reachable from that node, with the cost to get there: + * + * { + * node (Number|String): { + * neighbor (Number|String): cost (Number), + * ..., + * }, + * } + * + * In alternative to an object, you can pass a `Map` of `Map`. This will + * allow you to specify numbers as keys. + * + * @param [graph] - Initial graph definition + * @example + * + * const route = new Graph(); + * + * // Pre-populated graph + * const route = new Graph({ + * A: { B: 1 }, + * B: { A: 1, C: 2, D: 4 }, + * }); + * + * // Passing a Map + * const g = new Map() + * + * const a = new Map() + * a.set('B', 1) + * + * const b = new Map() + * b.set('A', 1) + * b.set('C', 2) + * b.set('D', 4) + * + * g.set('A', a) + * g.set('B', b) + * + * const route = new Graph(g) + */ + constructor(nodes?: any[]); + + /** + * Adds a node to the graph + * + * @param name - Name of the node + * @param neighbors - Neighboring nodes and cost to reach them + * @example + * + * const route = new Graph(); + * + * route.addNode('A', { B: 1 }); + * + * // It's possible to chain the calls + * route + * .addNode('B', { A: 1 }) + * .addNode('C', { A: 3 }); + * + * // The neighbors can be expressed in a Map + * const d = new Map() + * d.set('A', 2) + * d.set('B', 8) + * + * route.addNode('D', d) + */ + addNode(name: string, neighbors: any): Graph; + + /** + * Removes a node and all of its references from the graph + * + * @param key - Key of the node to remove from the graph + * @example + * + * const route = new Graph({ + * A: { B: 1, C: 5 }, + * B: { A: 3 }, + * C: { B: 2, A: 2 }, + * }); + * + * route.removeNode('C'); + * // The graph now is: + * // { A: { B: 1 }, B: { A: 3 } } + */ + removeNode(name: string): Graph; + + /** + * Compute the shortest path between the specified nodes + * + * @param start - Starting node + * @param goal - Node we want to reach + * @param [options] - Options + * + * @param [options.trim] - Exclude the origin and destination nodes from the result + * @param [options.reverse] - Return the path in reversed order + * @param [options.cost] - Also return the cost of the path when set to true + * + * @return Computed path between the nodes. + * + * When `option.cost` is set to true, the returned value will be an object with shape: + * - `path` *(Array)*: Computed path between the nodes + * - `cost` *(Number)*: Cost of the path + * + * @example + * + * const route = new Graph() + * + * route.addNode('A', { B: 1 }) + * route.addNode('B', { A: 1, C: 2, D: 4 }) + * route.addNode('C', { B: 2, D: 1 }) + * route.addNode('D', { C: 1, B: 4 }) + * + * route.path('A', 'D') // => ['A', 'B', 'C', 'D'] + * + * // trimmed + * route.path('A', 'D', { trim: true }) // => [B', 'C'] + * + * // reversed + * route.path('A', 'D', { reverse: true }) // => ['D', 'C', 'B', 'A'] + * + * // include the cost + * route.path('A', 'D', { cost: true }) + * // => { + * // path: [ 'A', 'B', 'C', 'D' ], + * // cost: 4 + * // } + */ + path(start: any, goal: any, options ?: PathOption): any; +} + +interface PathOption { + trim ?: boolean; + reverse ?: boolean; + cost ?: boolean; + avoid ?: any[]; +} + +export = Graph; diff --git a/types/node-dijkstra/node-dijkstra-tests.ts b/types/node-dijkstra/node-dijkstra-tests.ts new file mode 100644 index 0000000000..b6a8fbff9a --- /dev/null +++ b/types/node-dijkstra/node-dijkstra-tests.ts @@ -0,0 +1,12 @@ +import Graph = require('node-dijkstra'); + +const graph = new Graph(); +const graph2 = new Graph([{A: {B: 1}}]); + +graph + .addNode('A', {B: 1}) + .removeNode('C'); + +graph2.path('A', 'B'); +graph.path('A', 'B', {cost: false, reverse: true}); +graph.path('A', 'B', {cost: true}); diff --git a/types/node-dijkstra/tsconfig.json b/types/node-dijkstra/tsconfig.json new file mode 100644 index 0000000000..41f3b852bf --- /dev/null +++ b/types/node-dijkstra/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "esModuleInterop": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-dijkstra-tests.ts" + ] +} diff --git a/types/node-dijkstra/tslint.json b/types/node-dijkstra/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/node-dijkstra/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }