I am padding numbers with lead zeros. Let’s try executeScript_Sandbox -
return “1”.padstart(2, ‘0’);
[error] Line 1: undefined is not a function.
I am using latest version of Firefox and UI.Vision. It used to work before, what just happened?
I am padding numbers with lead zeros. Let’s try executeScript_Sandbox -
return “1”.padstart(2, ‘0’);
[error] Line 1: undefined is not a function.
I am using latest version of Firefox and UI.Vision. It used to work before, what just happened?
I am confused. There is no Version 8 for Firefox yet. So do you use Chrome?
My bad! It’s 8.0.1 in Chrome, yes.
Padstart is an ecmascript 2017 feature (ES8). But the new executescript_sandbox commands supports only ECMAScript 2009 (ES5).
For more details & solutions please see this post, or executescript_sandbox
I just got caught with this with the new Firefox update. I changed to executeScript but is there an equivalent to padStart for the old ES5 to use the sandbox?
Unfortunately there’s no direct ES5 equivalent. Here’s a function to mimic the behavior of padStart()
in ES5 for use with executeScript_Sandbox:
function padStartES5(str, targetLength, padString) {
str = str.toString(); // Convert the input to a string, in case it isn't already
padString = padString || ' '; // Default pad is a space if not provided
while (str.length < targetLength) {
str = padString + str; // Prepend padString until length is reached
// Truncate the padding string in case of excess length
str = str.slice(-targetLength);
}
return str;
}
// Example usage:
var originalString = '5';
console.log(padStartES5(originalString, 3, '0')); // "005"
This function takes the string to be padded, the target length of the final string, and the string to pad with. It prepends the padding string repeatedly until the original string reaches or exceeds the desired length. Then it ensures the result is the exact length required by potentially slicing off any excess.
This is a basic implementation and assumes that the padding string is not empty.
That works, thank you very much for that!