Skip to content

Commit

Permalink
Added String.padEnd()
Browse files Browse the repository at this point in the history
  • Loading branch information
fadrian06 committed Jan 25, 2024
1 parent c29cd5a commit 1dc3462
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Tasks.todo
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions src/Globals/JSString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
26 changes: 26 additions & 0 deletions tests/PHP/JSString/padEndTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\PHP\JSString;

use PHPUnit\Framework\TestCase;

final class padEndTest extends TestCase {
function test_Demo_String_padEnd(): void {
$str1 = String('Breaded Mushrooms');

self::expectOutputString('Breaded Mushrooms........');
echo $str1->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));
}
}

0 comments on commit 1dc3462

Please sign in to comment.