diff --git a/Tasks.todo b/Tasks.todo index 7915ebe..afa77f2 100644 --- a/Tasks.todo +++ b/Tasks.todo @@ -34,7 +34,7 @@ JSPHP: ☐ String.match() ☐ String.matchAll() ☐ String.normalize() - ☐ String.padEnd() + ✔ String.padEnd() @done (24/01/2024 23:30:43) ☐ String.padStart() ☐ String.repeat() ☐ String.replace() diff --git a/src/Globals/JSString.php b/src/Globals/JSString.php index 4eb5332..653be75 100644 --- a/src/Globals/JSString.php +++ b/src/Globals/JSString.php @@ -249,6 +249,21 @@ function lastIndexOf(string $searchString, ?int $position = null): int { return $lastIndex; } + /** + * Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. + * The padding is applied from the end (right) of the current string. + * + * @param int $maxLength The length of the resulting string once the current string has been padded. + * If this parameter is smaller than the current string's length, the current string will be returned as it is. + * + * @param string $fillString The string to pad the current string with. + * If this string is too long, it will be truncated and the left-most part will be applied. + * The default value for this parameter is " " (U+0020). + */ + function padEnd(int $maxLength, string $fillString = ' '): self { + return new self(str_pad($this->value, $maxLength, $fillString)); + } + // TODO: Implement JS string methods } diff --git a/tests/PHP/JSString/padEndTest.php b/tests/PHP/JSString/padEndTest.php new file mode 100644 index 0000000..44f0794 --- /dev/null +++ b/tests/PHP/JSString/padEndTest.php @@ -0,0 +1,26 @@ +padEnd(25, '.'); + + $str2 = String('200'); + self::assertEquals('200 ', $str2->padEnd(5)); + } + + function test_Using_padEnd(): void { + self::assertEquals('abc ', String('abc')->padEnd(10)); + self::assertEquals('abcfoofoof', String('abc')->padEnd(10, 'foo')); + self::assertEquals('abc123', String('abc')->padEnd(6, '123456')); + self::assertEquals('abc', String('abc')->padEnd(1)); + } +}