Skip to content

Commit

Permalink
🐛 fix JsArray::from() non string/array iterable issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamed committed May 16, 2021
1 parent a7f56c1 commit c09624e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 4 additions & 6 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
* @license MIT https://opensource.org/licenses/MIT
*/


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../vendor/autoload.php';

use Ahamed\JsPhp\JsArray;
use Ahamed\JsPhp\JsObject;


$range = fn($start, $end, $step) => JsArray::from(['length' => ($end-$start) / $step + 1], fn($_, $i) => $start + ($i * $step));
$array = JsArray::from('I love to play dota 2')->filter(fn($char) => $char !== ' ');

$array = JsArray::from(3);
echo '<xmp>';
print_r($array);
echo '</xmp>';
die();



17 changes: 17 additions & 0 deletions src/Traits/Arrays/BasicsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,34 @@ public static function from($iterable, callable $callable = null) : JsArray
$array = new JsArray();
$localArray = [];

/** If JsArray provided as iterable then get the array elements. */
if ($iterable instanceof JsArray)
{
$iterable = $iterable->get();
}

/** If string given as iterable then split the string and make an array of characters. */
if (\is_string($iterable))
{
$iterable = \str_split($iterable, 1);
}

/** If iterable is not an array then return an JsArray instance with empty array. */
if (!\is_array($iterable))
{
return $array->bind([], false);
}

$isAssoc = JsArray::isAssociativeArray($iterable);

/**
* Check if the iterable is an associative array.
* If so then check if this array contains the `length` property.
* If length not found then return a JsArray instance with empty array.
* Otherwise create an array from 0 to $length - 1 and if $callable provided
* then create the array by using the $callable function's return value
* otherwise fill by null and return the newly created array.
*/
if ($isAssoc && !isset($iterable['length']))
{
return $array->bind([], false);
Expand All @@ -66,6 +82,7 @@ public static function from($iterable, callable $callable = null) : JsArray
return $array->bind($localArray, false);
}

/** If $iterable is a plain array. */
foreach ($iterable as $index => $item)
{
$localArray[$index] = $callable
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/JsArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,11 @@ public function testArrayFrom()

$array = JsArray::from('I love to play dota 2');
$this->assertEquals(["I", " ", "l", "o", "v", "e", " ", "t", "o", " ", "p", "l", "a", "y", " ", "d", "o", "t", "a", " ", "2"], $array->get());

$array = JsArray::from(9);
$this->assertEquals([], $array->get());

$array = JsArray::from(5, function($x, $i) {return $i;});
$this->assertEquals([], $array->get());
}
}

0 comments on commit c09624e

Please sign in to comment.