diff --git a/types/topo/index.d.ts b/types/topo/index.d.ts new file mode 100644 index 0000000000..92c8f2153e --- /dev/null +++ b/types/topo/index.d.ts @@ -0,0 +1,48 @@ +// Type definitions for topo 3.0 +// Project: https://github.com/hapijs/topo#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +export = Topo; + +/** + * The Topo object is the container for topologically sorting a list of nodes with non-circular interdependencies. + */ +declare class Topo { + /** + * An array of the topologically sorted nodes. This list is renewed upon each call to `topo.add()`. + */ + nodes: TNode[]; + + /** + * Specifies an additional node or list of nodes to be topologically sorted. + * + * @param nodes a mixed value or array of mixed values to be added as nodes to the topologically sorted list. + * @param options optional sorting information about the `nodes`: + * - `group` - a string naming the group to which `nodes` should be assigned. The group name `'?'` is reserved. + * - `before` - a string or array of strings specifying the groups that `nodes` must precede in the topological sort. + * - `after` - a string or array of strings specifying the groups that `nodes` must succeed in the topological sort. + * - `sort` - a numerical value used to sort items when performing a `topo.merge()`. + * @returns an array of the topologically sorted nodes. + */ + add( + nodes: TNode | TNode[], + options?: { + group?: TGroup; + before?: TGroup | TGroup[]; + after?: TGroup | TGroup[]; + sort?: number; + } + ): TNode[]; + + /** + * Merges another `Topo` object into the current object. + * If the order in which items have been added to each list matters, use the `sort` option in `topo.add()` + * with an incrementing value providing an absolute sort order among all items added to either object. + * + * @param others the other object or array of objects to be merged into the current one. `null` values are ignored. + * @returns an array of the topologically sorted nodes. Will throw if a dependency error is found as a result of the combined items. + */ + merge(others: Topo | Array>): TNode[]; +} diff --git a/types/topo/topo-tests.ts b/types/topo/topo-tests.ts new file mode 100644 index 0000000000..d880f15e80 --- /dev/null +++ b/types/topo/topo-tests.ts @@ -0,0 +1,15 @@ +import Topo = require('topo'); + +const morning = new Topo(); + +morning.add('Nap', { after: ['breakfast', 'prep'] }); // $ExpectType string[] +morning.add('Nap', { after: 'foo' }); // $ExpectError +morning.add(['Make toast', 'Pour juice'], { before: 'breakfast', group: 'prep' }); // $ExpectType string[] +morning.add('Eat breakfast', { group: 'breakfast' }); // $ExpectType string[] + +morning.nodes; // $ExpectType string[] + +const dinner = new Topo(); +dinner.add('Eat dinner', { group: 'dinner', after: 'breakfast' }); // $ExpectType string[] + +morning.merge(dinner); diff --git a/types/topo/tsconfig.json b/types/topo/tsconfig.json new file mode 100644 index 0000000000..8ebfca72df --- /dev/null +++ b/types/topo/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "topo-tests.ts" + ] +} diff --git a/types/topo/tslint.json b/types/topo/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/topo/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }