Skip to content

Commit

Permalink
V2.0.5 Changes
Browse files Browse the repository at this point in the history
* added a new utility `shapeToPathArray`
* added support for `<path>` for `shapeToPathArray`;
* the `shapeToPath` utility will throw if your shape is already a SVGPathElement;
*  `ry` attribute is now optional for `EllipseAttr` with `shapeToPath`
* `rx` and `ry` attributes are now optional for `RectAttr` with `shapeToPath`
* more strict `isPathArray`
* updated tests
  • Loading branch information
thednp committed Mar 25, 2023
1 parent 0d72e64 commit 557f2ea
Show file tree
Hide file tree
Showing 18 changed files with 643 additions and 515 deletions.
55 changes: 49 additions & 6 deletions cypress/e2e/svg-path-commander.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,25 +562,50 @@ describe('SVGPathCommander Static Methods', () => {
})
});

['wombat', 'line', 'circle', 'ellipse', 'rect', 'polygon', 'polyline', 'glyph'].forEach((SHAPE) => {
it('Convert shape to pathArray with incomplete values should return false', () => {
['line', 'circle', 'ellipse', 'rect', 'polygon', 'polyline', 'glyph'].forEach((SHAPE) => {
cy.log(`**${SHAPE}** with no specific attributes`).then(() => {
// @ts-ignore
expect(SVGPathCommander.shapeToPathArray({ type: SHAPE, fill: 'red' })).to.be.false;
})
})
});

['wombat', 'line', 'circle', 'ellipse', 'rect', 'polygon', 'polyline', 'glyph', 'path'].forEach((SHAPE) => {
it(`Convert <${SHAPE}> to path`, () => {
cy.get('@svg').invoke('prop', 'innerHTML', `<line id="line" x1="0" y1="0" x2="182" y2="72" stroke="turquoise" stroke-width="2" />
<circle id="circle" cx="27.5" cy="36.9" r="23.5" fill="orangered"/>
<ellipse id="ellipse" cx="68.3" cy="37" rx="15.1" ry="23.4" fill="darkorange"/>
<ellipse id="ellipse" cx="68.3" cy="37" rx="15.1" fill="darkorange"/>
<wombat id="wombat" fill="black"/>
<polygon id="polygon" points="107.4,13 113.7,28.8 127.9,31.3 117.6,43.5 120.1,60.8 107.4,52.6 94.6,60.8 97.1,43.5 86.8,31.3 101,28.8" fill="yellow"/>
<polyline id="polyline" points="107.39,17.78 112.43,30.42 123.79,32.42 115.55,42.18 117.55,56.02 107.39,49.46 97.15,56.02 99.15,42.18 90.91,32.42 102.27,30.42" fill="none" stroke="black" stroke-width="2"/>
<rect id="rect" x="131" y="13.2" width="47.5" height="47.6" rx="25" fill="yellowgreen"/>
<path id="path" d="M143.5 22.72H166s3 0 3 3v22.56s0 3 -3 3h-22.5s-3 0 -3 -3V25.72s0 -3 3 -3" fill="rgba(255,255,255,0.3)"/>
<glyph id="glyph" d="M143.5 22.72H166s3 0 3 3v22.56s0 3 -3 3h-22.5s-3 0 -3 -3V25.72s0 -3 3 -3" fill="rgba(255,255,255,0.3)"/>`)
if (SHAPE === 'wombat') {
cy.get('@svg').find(SHAPE).then((shape) => {
try {
SVGPathCommander.shapeToPathArray(shape[0] as unknown as SVGCircleElement, shape[0].ownerDocument)
} catch (er) {
expect(er).to.be.instanceOf(TypeError);
expect(er).to.have.property('message', `${error}: "${SHAPE}" is not SVGElement`);
}
try {
SVGPathCommander.shapeToPath(shape[0] as unknown as SVGCircleElement, true, shape[0].ownerDocument)
} catch (er) {
expect(er).to.be.instanceOf(TypeError);
expect(er).to.have.property('message', `${error}: "${SHAPE}" is not SVGElement`);
}
})
} else if (SHAPE === 'path') {
cy.get('@svg').find(SHAPE).then((shape) => {
try {
SVGPathCommander.shapeToPath(shape[0] as unknown as SVGCircleElement, true, shape[0].ownerDocument)
} catch (er) {
expect(er).to.be.instanceOf(TypeError);
expect(er).to.have.property('message', `${error}: "${SHAPE}" is already SVGPathElement`);
}
})
} else {
cy.get('@svg').find(SHAPE).should('exist').then((shape) => {
SVGPathCommander.shapeToPath(shape[0] as unknown as ShapeTypes, true, shape[0].ownerDocument)
Expand All @@ -592,12 +617,30 @@ describe('SVGPathCommander Static Methods', () => {
});
});

shapeObjects.forEach((SHAPE) => {
it(`Convert "${SHAPE.type}" Object to pathArray`, () => {
cy.wrap(SVGPathCommander.shapeToPathArray(SHAPE as unknown as ShapeTypes))
.should('have.length.greaterThan', 0)
});
});

it(`Convert wombat Object to pathArray should throw error`, () => {
try {
SVGPathCommander.shapeToPathArray({ type: 'wombat', fill: 'red' } as unknown as ShapeTypes)
} catch (er) {
expect(er).to.be.instanceOf(TypeError);
expect(er).to.have.property('message', `${error}: "wombat" is not SVGElement`);
}
});

shapeObjects.forEach((SHAPE) => {
it(`Convert "${SHAPE.type}" Object to path`, () => {
cy.get('@svg').should('exist').invoke('append', SVGPathCommander.shapeToPath(SHAPE as unknown as ShapeTypes))
.get(`#${SHAPE.type}`).should('exist')
.and('have.attr', 'd')
.and('have.length.greaterThan', 0)
// cy.document().then(doc => {
cy.get('@svg').should('exist').invoke('append', SVGPathCommander.shapeToPath(SHAPE as unknown as ShapeTypes))
.get(`#${SHAPE.type}`).should('exist')
.and('have.attr', 'd')
.and('have.length.greaterThan', 0)
// })
});
});

Expand Down
2 changes: 1 addition & 1 deletion dist/svg-path-commander.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/svg-path-commander.cjs.map

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions dist/svg-path-commander.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ export interface EllipseAttr {
cx: number;
cy: number;
rx: number;
ry: number;
[key: string]: string | number;
ry?: number;
[key: string]: string | number | undefined;
}
export interface RectAttr {
type: "rect";
width: number;
height: number;
x: number;
y: number;
rx: number;
ry: number;
[key: string]: string | number;
rx?: number;
ry?: number;
[key: string]: string | number | undefined;
}
export interface GlyphAttr {
type: "glyph";
Expand Down Expand Up @@ -316,6 +316,7 @@ declare class SVGPathCommander {
static isCurveArray: (path: unknown) => path is CurveArray;
static isNormalizedArray: (path: unknown) => path is NormalArray;
static shapeToPath: (element: ShapeTypes | ShapeOps, replace?: boolean | undefined, ownerDocument?: Document | undefined) => false | SVGPathElement;
static shapeToPathArray: (element: ShapeTypes | ShapeOps, ownerDocument?: Document | undefined) => false | PathArray;
static parsePathString: (pathInput: string | PathArray) => PathArray;
static roundPath: (path: PathArray, roundOption?: number | "off" | undefined) => PathArray;
static splitPath: (pathInput: PathArray) => PathArray[];
Expand Down
2 changes: 1 addition & 1 deletion dist/svg-path-commander.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/svg-path-commander.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 557f2ea

Please sign in to comment.