Reply To: Stripping out the initial common numbers

#97975
Mike Dean
Member

Ah, my mistake. It’s just a few more lines to run this based on a selection, so I’ve updated the script below. Just select a series of numbers and run the script and it should clean up the series. It should also work on numbers greater than 6 digits as long as it’s always the first 3 numbers that are common.


(function(){
var selection = app.selection[0];

if(selection!="[object Text]" && selection!="[object Paragraph]" ){
alert("Please select text and try again.");
return;
}

selection.contents = removeCommonPrefix(selection.contents.split(", ")).join(", ");

function removeCommonPrefix(numbers) {
var updatedNumbers =[];
var i = numbers.length -1;
while (i >= 1) {
var currentNumber = numbers[i];
var currentNumberPrefix = numbers[i].toString().slice(0, 3);
var preceedingNumberPrefix = numbers[i -1].toString().slice(0, 3);
var numberSuffix = numbers[i].toString().slice(3);
if (currentNumberPrefix == preceedingNumberPrefix) {
var number = numberSuffix;
} else {
var number = currentNumber;
}
updatedNumbers.unshift(number);
i--;
}
//the first number will always need the initial 3, so at the end of the loop add it to the start
updatedNumbers.unshift(numbers[0]);
return updatedNumbers;
}
})();

This article was last modified on September 11, 2017

Comments (0)

Loading comments...