summaryrefslogtreecommitdiff
path: root/objects.js
diff options
context:
space:
mode:
authorjmoenig <jens@moenig.org>2014-07-17 14:39:37 +0200
committerjmoenig <jens@moenig.org>2014-07-17 14:39:37 +0200
commitf38fb73cd3bcfad0ae610c8f08fea230b964ccd6 (patch)
tree66190c23484b4d5d75e85632e2005cbf5c33691f /objects.js
parent7472261ca60d4409f670ec52a376a1525900ee15 (diff)
downloadsnap-byow-f38fb73cd3bcfad0ae610c8f08fea230b964ccd6.tar.gz
snap-byow-f38fb73cd3bcfad0ae610c8f08fea230b964ccd6.zip
simplify unique stageshot names
and get rid of regex
Diffstat (limited to 'objects.js')
-rw-r--r--objects.js41
1 files changed, 11 insertions, 30 deletions
diff --git a/objects.js b/objects.js
index 6e49f43..6a0d6e0 100644
--- a/objects.js
+++ b/objects.js
@@ -4086,39 +4086,20 @@ SpriteMorph.prototype.reactToDropOf = function (morph, hand) {
// SpriteMorph screenshots
-SpriteMorph.prototype.re = /\((\d+)\)$/; // RegExp to match costume names
-
SpriteMorph.prototype.newCostumeName = function (name) {
- var foundSameName = false,
- foundIndex = false,
- lastIndex = 0,
- i = 1,
- p = null,
- costume = null;
- for (i = 1; i <= this.costumes.length(); i += 1) {
- costume = this.costumes.at(i);
- if (costume !== null) {
- if (costume.name === name) {
- foundSameName = true;
- }
- if (foundSameName) {
- p = this.re.exec(costume.name);
- if (p) {
- lastIndex = Number(p[1]);
- foundIndex = true;
- }
- }
- }
- }
- if (foundSameName) {
- if (foundIndex) {
- lastIndex += 1;
- return name + '(' + lastIndex + ')'; // New index with a +1
- }
- return name + '(2)'; // start off indexing with (1)
+ function stemOf(aName) {
+ var ix = aName.indexOf('(');
+ if (ix < 0) {return aName; }
+ return aName.substring(0, ix);
}
- return name;
+
+ var stem = stemOf(name),
+ similar = this.costumes.asArray().filter(function (eachCostume) {
+ return stemOf(eachCostume.name) === stem;
+ }).length;
+
+ return stem + (similar ? '(' + (similar + 1) + ')' : '');
};
SpriteMorph.prototype.doScreenshot = function (imgSource, data) {