From 45828fbb51a9c93731cab3e7936623a2e299eebb Mon Sep 17 00:00:00 2001 From: Cerberuser Date: Wed, 25 Jul 2018 00:13:48 +0700 Subject: [PATCH] Cytoscape: type guards, stylesheet fix, mapping/filtering functions fix (#27523) --- types/cytoscape/cytoscape-tests.ts | 15 ++++++++++---- types/cytoscape/index.d.ts | 33 ++++++++++++++++-------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/types/cytoscape/cytoscape-tests.ts b/types/cytoscape/cytoscape-tests.ts index 484b682fcc..0cffa42b31 100644 --- a/types/cytoscape/cytoscape-tests.ts +++ b/types/cytoscape/cytoscape-tests.ts @@ -341,7 +341,8 @@ const positions = oneOf({a: {x: 100, y: 100}}, (node: cytoscape.NodeCollection): // layout.stop(); // }); -// TODO: cy.style +cy.style(cy.style()); +cy.style([cy.style()]); // $ExpectType string cy.png({ @@ -548,11 +549,17 @@ cy.collection().merge(diff.left).merge(diff.right).merge(diff.both).unmerge(coll nodes.map(n => n.degree(false)); edges.map(e => e.source()); eles.map(e => e.id()); +eles.map(e => e.isNode() ? e.degree(false) : e.source()); +eles.map(e => e.isEdge() ? e.source() : e.degree(false)); -eles.sort((a, b) => 1).map((ele, i, eles) => [i, ele]); +eles.sort((a, b) => a.id.length - b.id.length).map((ele, i, eles) => [i, ele]); eles.reduce((prev, ele, i, eles) => [...prev, [ele, i]], []).concat(['finish']); -const min = eles.min((ele, i, eles) => ele.id.length + i); min.ele.scratch('min', min.value).scratch('min').value; -const max = eles.max((ele, i, eles) => ele.id.length + i); max.ele.scratch('max', max.value); + +const min = eles.min((ele, i, eles) => ele.isNode() ? ele.degree(false) : ele.source().degree(false)); +min.ele.scratch('min', min.value).scratch('min').value; +const max = eles.max((ele, i, eles) => ele.isEdge() ? ele.source().degree(false) : ele.degree(false)); +max.ele.scratch('max', max.value); + nodes.min(n => n.degree(false)); nodes.max(n => n.degree(false)); edges.max(n => n.source().id().length); diff --git a/types/cytoscape/index.d.ts b/types/cytoscape/index.d.ts index dddfb10fa3..c38acead19 100644 --- a/types/cytoscape/index.d.ts +++ b/types/cytoscape/index.d.ts @@ -1016,7 +1016,10 @@ declare namespace cytoscape { * Get the entry point to modify the visual style of the graph after initialisation. * http://js.cytoscape.org/#core/style */ - interface ElementStylesheet extends StylesheetStyle { + interface ElementStylesheetStyle extends StylesheetStyle { + json(): any; + } + interface ElementStylesheetCSS extends StylesheetCSS { json(): any; } @@ -1024,11 +1027,11 @@ declare namespace cytoscape { /** * Get the current style object. */ - style(): ElementStylesheet | string; + style(): ElementStylesheetStyle | ElementStylesheetCSS; /** * Assign a new stylesheet to replace the existing one. */ - style(sheet: Stylesheet): Stylesheet; + style(sheet: Stylesheet | Stylesheet[] | string): Stylesheet; } /** @@ -1140,7 +1143,7 @@ declare namespace cytoscape { CollectionLayout, CollectionSelection, CollectionStyle, CollectionAnimation, CollectionComparision, CollectionIteration, - CollectionBuildingFiltering, CollectionAlgorithms { } + CollectionBuildingFiltering, CollectionAlgorithms { } /** * ele --> Cy.Singular @@ -1163,8 +1166,8 @@ declare namespace cytoscape { /** * The output is a collection of node and edge elements OR single element. */ - type CollectionArgument = EdgeCollection | NodeCollection | SingularElementArgument; - type CollectionReturnValue = EdgeCollection & NodeCollection & SingularElementReturnValue; + type CollectionArgument = Collection | EdgeCollection | NodeCollection | SingularElementArgument; + type CollectionReturnValue = Collection & EdgeCollection & NodeCollection & SingularElementReturnValue; /** * edges -> Cy.EdgeCollection @@ -1430,13 +1433,13 @@ declare namespace cytoscape { * Get whether the element is a node. * http://js.cytoscape.org/#ele.isNode */ - isNode(): boolean; + isNode(): this is NodeSingular; /** * Get whether the element is an edge. * http://js.cytoscape.org/#ele.isEdge */ - isEdge(): boolean; + isEdge(): this is EdgeSingular; } /** * http://js.cytoscape.org/#collection/data @@ -2335,7 +2338,7 @@ declare namespace cytoscape { /** * http://js.cytoscape.org/#collection/building--filtering */ - interface CollectionBuildingFiltering { + interface CollectionBuildingFiltering { /** * Get an element in the collection from its ID in a very performant way. * @param id The ID of the element to get. @@ -2478,7 +2481,7 @@ declare namespace cytoscape { * ele - The element being considered. * http://js.cytoscape.org/#eles.filter */ - filter(selector: Selector | ((ele: TOut, i: number, eles: CollectionArgument) => boolean)): CollectionReturnValue; + filter(selector: Selector | ((ele: TIn, i: number, eles: CollectionArgument) => boolean)): CollectionReturnValue; /** * Get the nodes that match the specified selector. * @@ -2504,7 +2507,7 @@ declare namespace cytoscape { * * http://js.cytoscape.org/#eles.sort */ - sort(sort: (ele1: TOut, ele2: TOut) => number): CollectionReturnValue; + sort(sort: (ele1: TIn, ele2: TIn) => number): CollectionReturnValue; /** * Get an array containing values mapped from the collection. @@ -2517,7 +2520,7 @@ declare namespace cytoscape { * * http://js.cytoscape.org/#eles.map */ - map(fn: (ele: TOut, i: number, eles: CollectionArgument) => T, thisArg?: any): T[]; + map(fn: (ele: TIn, i: number, eles: CollectionArgument) => T, thisArg?: any): T[]; /** * Reduce a single value by applying a @@ -2534,7 +2537,7 @@ declare namespace cytoscape { * also stated explicitly as generic * http://js.cytoscape.org/#eles.reduce */ - reduce(fn: (prevVal: T, ele: TOut, + reduce(fn: (prevVal: T, ele: TIn, i: number, eles: CollectionArgument) => T, initialValue: T): T; /** @@ -2548,7 +2551,7 @@ declare namespace cytoscape { * * http://js.cytoscape.org/#eles.min */ - min(fn: (ele: TOut, i: number, eles: CollectionArgument) => T, thisArg?: any): { + min(fn: (ele: TIn, i: number, eles: CollectionArgument) => T, thisArg?: any): { /** * The minimum value found. */ @@ -2570,7 +2573,7 @@ declare namespace cytoscape { * * http://js.cytoscape.org/#eles.max */ - max(fn: (ele: TOut, i: number, eles: CollectionArgument) => T, thisArg?: any): { + max(fn: (ele: TIn, i: number, eles: CollectionArgument) => T, thisArg?: any): { /** * The minimum value found. */