From 74cee4abef724aa0c4bb9582b0f4f3ffdbeed801 Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Wed, 13 Apr 2011 16:16:32 +0200 Subject: [PATCH] Implement Path#remove() and add tests for it. --- src/path/Path.js | 20 +++++++++++++++++++- test/tests/Path.js | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/path/Path.js b/src/path/Path.js index 9e1b64b6..81d21ffe 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -109,7 +109,25 @@ var Path = this.Path = PathItem.extend({ return segment ? this._add(segment, index) : null; }, - // TODO: remove(index) / remove(segment) / remove(fromIndex, toIndex) + remove: function() { + if (!arguments.length) { + // remove() + this.base(); + } else if (arguments.length == 1) { + if (arguments[0].point) { + // remove(segment) + arguments[0].remove(); + } else { + // remove(index) + this._segments[arguments[0]].remove(); + } + } else { + // remove(fromIndex, toIndex) + for(var i = arguments[1], l = arguments[0]; i >= l; i--) + this._segments[i].remove(); + } + }, + // TODO: pointsToCurves([tolerance[, threshold[, cornerRadius[, scale]]]]) // TODO: curvesToPoints([maxPointDistance[, flatness]]) // TODO: reduceSegments([flatness]) diff --git a/test/tests/Path.js b/test/tests/Path.js index a4ebafaa..1a21dec0 100644 --- a/test/tests/Path.js +++ b/test/tests/Path.js @@ -53,4 +53,26 @@ test('path.join(path)', function() { new Segment(new Point(10, 0)), new Segment(new Point(20, 10)), new Segment(new Point(10, 5))]); equals(path.closed, true); +}); + +test('path.remove()', function() { + var doc = new Document(); + var path = new Path(); + path.add(0, 0); + path.add(10, 0); + path.add(20, 0); + path.add(30, 0); + + path.remove(0); + equals(path.segments.length, 3); + + path.remove(path.segments[0]); + equals(path.segments.length, 2); + + path.remove(0, 1); + equals(path.segments.length, 0); + + path.remove(); + + equals(doc.activeLayer.children.length, 0); }); \ No newline at end of file