Skip to content

Commit

Permalink
refactor: remove points property
Browse files Browse the repository at this point in the history
  • Loading branch information
kmkzt committed May 16, 2020
1 parent 62b2d00 commit a3d9b56
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 104 deletions.
2 changes: 1 addition & 1 deletion src/__snapshots__/renderer.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`renderer Renderer download svg 1`] = `
Object {
"data": "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMCIgaGVpZ2h0PSIwIiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggc3Ryb2tlLXdpZHRoPSIxIiBzdHJva2U9IiMwMDAiIGZpbGw9Im5vbmUiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgZD0iTSAwIDAgQyAwLjIgMC4yIDAuNiAwLjggMSAxIEMgMS40IDEuMiAxLjYgMS4yIDIgMSBDIDIuNCAwLjggMi44IDAuMiAzIDAiPjwvcGF0aD48cGF0aCBzdHJva2Utd2lkdGg9IjEiIHN0cm9rZT0iIzAwMCIgZmlsbD0ibm9uZSIgc3Ryb2tlLWxpbmVqb2luPSJtaXR0ZXIiIHN0cm9rZS1saW5lY2FwPSJzcXVhcmUiIGQ9Ik0gNCA0IEwgOSA0IEwgOSA4IEwgMyAwIEwgNCA0IFoiPjwvcGF0aD48L3N2Zz4=",
"data": "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMCIgaGVpZ2h0PSIwIiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggc3Ryb2tlLXdpZHRoPSIxIiBzdHJva2U9IiMwMDAiIGZpbGw9Im5vbmUiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgZD0iTSAwIDAgQyAwLjIgMC4yIDAuNiAwLjggMSAxIEMgMS40IDEuMiAxLjYgMS4yIDIgMSBDIDIuNCAwLjggMi44IDAuMiAzIDAiPjwvcGF0aD48cGF0aCBzdHJva2Utd2lkdGg9IjEiIHN0cm9rZT0iIzAwMCIgZmlsbD0ibm9uZSIgc3Ryb2tlLWxpbmVqb2luPSJtaXR0ZXIiIHN0cm9rZS1saW5lY2FwPSJzcXVhcmUiIGQ9Ik0gNCA0IEwgOSA0IEwgOSA4IEwgMyAwIFoiPjwvcGF0aD48L3N2Zz4=",
"extension": "svg",
}
`;
40 changes: 13 additions & 27 deletions src/__snapshots__/svg.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,10 @@ Array [
},
"type": 2,
},
Command {
"cl": Point {
"x": 2.6,
"y": -0.2,
},
"cr": Point {
"x": 0.4,
"y": -0.2,
},
"point": Point {
"x": 0,
"y": 0,
},
"type": 2,
},
]
`;

exports[`svg Path formatCommand and getCommandString Circuler Close 2`] = `"M 0 0 C 0.2 0.2 0.6 0.8 1 1 C 1.4 1.2 1.6 1.2 2 1 C 2.4 0.8 2.8 0.2 3 0 C 2.6 -0.2 0.4 -0.2 0 0 Z"`;
exports[`svg Path formatCommand and getCommandString Circuler Close 2`] = `"M 0 0 C 0.2 0.2 0.6 0.8 1 1 C 1.4 1.2 1.6 1.2 2 1 C 2.4 0.8 2.8 0.2 3 0 Z"`;

exports[`svg Path formatCommand and getCommandString Circuler Normal 1`] = `
Array [
Expand Down Expand Up @@ -143,30 +128,27 @@ Array [
"type": 0,
},
Command {
"cl": undefined,
"cr": undefined,
"point": Point {
"x": 1,
"y": 1,
},
"type": 1,
},
Command {
"cl": undefined,
"cr": undefined,
"point": Point {
"x": -1,
"y": -1,
},
"type": 1,
},
Command {
"point": Point {
"x": 0,
"y": 0,
},
"type": 1,
},
]
`;

exports[`svg Path formatCommand and getCommandString Line Close 2`] = `"M 0 0 L 1 1 L -1 -1 L 0 0 Z"`;
exports[`svg Path formatCommand and getCommandString Line Close 2`] = `"M 0 0 L 1 1 L -1 -1 Z"`;

exports[`svg Path formatCommand and getCommandString Line Normal 1`] = `
Array [
Expand All @@ -178,13 +160,17 @@ Array [
"type": 0,
},
Command {
"cl": undefined,
"cr": undefined,
"point": Point {
"x": 1,
"y": 1,
},
"type": 1,
},
Command {
"cl": undefined,
"cr": undefined,
"point": Point {
"x": -1,
"y": -1,
Expand Down Expand Up @@ -218,7 +204,7 @@ Object {
}
`;

exports[`svg Renderer toBase64 1`] = `"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTAwIiBoZWlnaHQ9IjUwMCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxwYXRoIHN0cm9rZS13aWR0aD0iMSIgc3Ryb2tlPSIjMDAwIiBmaWxsPSJub25lIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBzdHJva2UtbGluZWNhcD0icm91bmQiIGQ9Ik0gMCAwIEMgMC4yIDAuMiAwLjYgMC44IDEgMSBDIDEuNCAxLjIgMS42IDEuMiAyIDEgQyAyLjQgMC44IDIuOCAwLjIgMyAwIj48L3BhdGg+PHBhdGggc3Ryb2tlLXdpZHRoPSIxIiBzdHJva2U9IiMwMDAiIGZpbGw9Im5vbmUiIHN0cm9rZS1saW5lam9pbj0ibWl0dGVyIiBzdHJva2UtbGluZWNhcD0ic3F1YXJlIiBkPSJNIDQgNCBMIDkgNCBMIDkgOCBMIDMgMCBMIDQgNCBaIj48L3BhdGg+PC9zdmc+"`;
exports[`svg Renderer toBase64 1`] = `"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTAwIiBoZWlnaHQ9IjUwMCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxwYXRoIHN0cm9rZS13aWR0aD0iMSIgc3Ryb2tlPSIjMDAwIiBmaWxsPSJub25lIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBzdHJva2UtbGluZWNhcD0icm91bmQiIGQ9Ik0gMCAwIEMgMC4yIDAuMiAwLjYgMC44IDEgMSBDIDEuNCAxLjIgMS42IDEuMiAyIDEgQyAyLjQgMC44IDIuOCAwLjIgMyAwIj48L3BhdGg+PHBhdGggc3Ryb2tlLXdpZHRoPSIxIiBzdHJva2U9IiMwMDAiIGZpbGw9Im5vbmUiIHN0cm9rZS1saW5lam9pbj0ibWl0dGVyIiBzdHJva2UtbGluZWNhcD0ic3F1YXJlIiBkPSJNIDQgNCBMIDkgNCBMIDkgOCBMIDMgMCBaIj48L3BhdGg+PC9zdmc+"`;

exports[`svg Renderer toElement 1`] = `
<svg
Expand All @@ -234,7 +220,7 @@ exports[`svg Renderer toElement 1`] = `
stroke-width="1"
/>
<path
d="M 4 4 L 9 4 L 9 8 L 3 0 L 4 4 Z"
d="M 4 4 L 9 4 L 9 8 L 3 0 Z"
fill="none"
stroke="#000"
stroke-linecap="square"
Expand All @@ -257,7 +243,7 @@ Object {
"strokeWidth": 1,
},
Object {
"d": "M 4 4 L 9 4 L 9 8 L 3 0 L 4 4 Z",
"d": "M 4 4 L 9 4 L 9 8 L 3 0 Z",
"fill": "none",
"stroke": "#000",
"strokeLinecap": "square",
Expand Down
102 changes: 68 additions & 34 deletions src/svg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ describe('svg', () => {
expect(vec.value).toBe(1.4142135623730951)
expect(vec.angle).toBe(0.7853981633974483)
})

it('clone', () => {
const po = new Point(0, 0)
const clone = po.clone()
clone.x = 2
expect(po.x).toBe(0)
expect(clone.x).toBe(2)
})
})
describe('Vector', () => {
it('toPoint', () => {
Expand All @@ -40,56 +48,82 @@ describe('svg', () => {
})
describe('Command', () => {
it('MOVE', () => {
const commands = new Command(CommandType.MOVE, new Point(0, 0))
expect(commands.toString()).toBe('M 0 0')
const cmd = new Command(CommandType.MOVE, new Point(0, 0))
expect(cmd.toString()).toBe('M 0 0')
})
it('LINE', () => {
const commands = new Command(CommandType.LINE, new Point(1, 1))
expect(commands.toString()).toBe('L 1 1')
const cmd = new Command(CommandType.LINE, new Point(1, 1))
expect(cmd.toString()).toBe('L 1 1')
})
it('CURVE', () => {
const commands = new Command(CommandType.CURVE, new Point(1, 1))
expect(commands.toString()).toBe('L 1 1')
commands.cl = new Point(0.25, 0.25)
commands.cr = new Point(0.75, 0.25)
expect(commands.toString()).toBe('C 0.25 0.25 0.75 0.25 1 1')
const cmd = new Command(CommandType.CURVE, new Point(1, 1))
expect(cmd.toString()).toBe('L 1 1')
cmd.cl = new Point(0.25, 0.25)
cmd.cr = new Point(0.75, 0.25)
expect(cmd.toString()).toBe('C 0.25 0.25 0.75 0.25 1 1')
})

describe('clone', () => {
let cmd
let clone
beforeEach(() => {
cmd = new Command(CommandType.CURVE, new Point(1, 1))
cmd.cl = new Point(0.25, 0.25)
cmd.cr = new Point(0.75, 0.25)
clone = cmd.clone()
})
it('point is not overwritten', () => {
clone.x = 1.5
expect(cmd.point.x).toBe(1)
expect(clone.point.x).toBe(1)
})
it('cl is not overwritten', () => {
clone.cl = new Point(0.1, 0.1)
expect(cmd.cl.x).toBe(0.25)
expect(clone.cl.x).toBe(0.1)
})
it('cr is not overwritten', () => {
clone.cr = new Point(1.2, 0.1)
expect(cmd.cr.x).toBe(0.75)
expect(clone.cr.x).toBe(1.2)
})
})
})
describe('Path', () => {
it('addPoint', () => {
const path = new Path().addPoint(new Point(1, 1))
expect(path.points.length).toBe(1)
expect(path.points[0].x).toBe(1)
expect(path.points[0].y).toBe(1)
let path: Path
beforeEach(() => {
path = new Path({ strokeWidth: 1 })
.addPoint(new Point(1, 1))
.addPoint(new Point(2, 2))
})
it('addCommand', () => {
const path = new Path().addCommand(
new Command(CommandType.LINE, new Point(1, 1))
)
expect(path.commands.length).toBe(1)
expect(path.commands[0].type).toBe(CommandType.LINE)
it('addPoint', () => {
expect(path.commands.length).toBe(2)
expect(path.commands[0].type).toBe(CommandType.MOVE)
expect(path.commands[0].point.x).toBe(1)
expect(path.commands[0].point.y).toBe(1)
})
it('addCommand', () => {
path.addCommand(new Command(CommandType.LINE, new Point(1, 1)))
expect(path.commands.length).toBe(3)
expect(path.commands[2].type).toBe(CommandType.LINE)
expect(path.commands[2].point.x).toBe(1)
expect(path.commands[2].point.y).toBe(1)
})
it('scale', () => {
const path = new Path({ strokeWidth: 1 })
.addPoint(new Point(1, 1))
.scale(2)
path.scale(2)
expect(path.strokeWidth).toBe(2)
expect(path.points[0].x).toBe(2)
expect(path.points[0].y).toBe(2)
expect(path.commands[0].type).toBe(CommandType.MOVE)
expect(path.commands[0].point.x).toBe(2)
expect(path.commands[0].point.y).toBe(2)
})
it('clone', () => {
const path = new Path({ strokeWidth: 1 }).addPoint(new Point(1, 1))
const clone = path.clone().addPoint(new Point(2, 2))
clone.points[0].x = 3
clone.formatCommand()
expect(path.points.length).toBe(1)
expect(path.commands.length).toBe(0)
expect(path.points[0].x).toBe(1)
expect(clone.points.length).toBe(2)
const origin = new Path({ strokeWidth: 1 }).addPoint(new Point(1, 1))
const clone = origin.clone().addPoint(new Point(2, 2))
clone.commands[0].point.x = 3
expect(origin.commands.length).toBe(1)
expect(clone.commands.length).toBe(2)
expect(clone.points[0].x).toBe(3)
expect(origin.commands[0].point.x).toBe(1)
expect(clone.commands[0].point.x).toBe(3)
})
describe('toJson and toElement', () => {
const path = new Path({ circuler: true, close: false })
Expand Down
Loading

0 comments on commit a3d9b56

Please sign in to comment.