Skip to content

Commit

Permalink
Fixed regex & added new assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
hbugdoll committed Jan 8, 2024
1 parent e112936 commit de5a226
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static function isFloat($value)
return true;
}

if (preg_match('/\d+\./', $value)) {
if (!is_array($value) && preg_match('/^\d+\.$/', $value)) {
// ordinal number of the form cardinal number followed by point, e.g. "24."
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function testIsFloat()
$this->assertTrue(StringHelper::isFloat('-1'));
$float = 1.0;
$this->assertTrue(StringHelper::isFloat($float));

$this->assertTrue(StringHelper::isFloat('.5'));
$this->assertFalse(StringHelper::isFloat('5.'));

$this->assertFalse(StringHelper::isFloat('string'));
}
Expand All @@ -53,6 +56,9 @@ public function testTypeCastNumeric()
$this->assertSame(1.5, StringHelper::typeCastNumeric(1.5));
$this->assertSame(-1, StringHelper::typeCastNumeric(-1));
$this->assertSame(-1.5, StringHelper::typeCastNumeric(-1.5));

$this->assertSame(0.5, StringHelper::typeCastNumeric('.5'));
$this->assertSame('5.', StringHelper::typeCastNumeric('5.'));

$this->assertSame(1, StringHelper::typeCastNumeric(true));
$this->assertSame(0, StringHelper::typeCastNumeric(false));
Expand Down

0 comments on commit de5a226

Please sign in to comment.