From c09624ee0ba3397b41deb2c6906963a8ebcd22f5 Mon Sep 17 00:00:00 2001 From: Sajeeb Ahamed Date: Mon, 17 May 2021 00:36:55 +0600 Subject: [PATCH] :bug: fix JsArray::from() non string/array iterable issue --- examples/index.php | 10 ++++------ src/Traits/Arrays/BasicsTrait.php | 17 +++++++++++++++++ tests/unit/JsArrayTest.php | 6 ++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/examples/index.php b/examples/index.php index 978d43a..1bf5bb0 100644 --- a/examples/index.php +++ b/examples/index.php @@ -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 ''; print_r($array); echo ''; die(); + + diff --git a/src/Traits/Arrays/BasicsTrait.php b/src/Traits/Arrays/BasicsTrait.php index 8188b8c..ab09a81 100644 --- a/src/Traits/Arrays/BasicsTrait.php +++ b/src/Traits/Arrays/BasicsTrait.php @@ -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); @@ -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 diff --git a/tests/unit/JsArrayTest.php b/tests/unit/JsArrayTest.php index 2ff5c78..0a9656b 100644 --- a/tests/unit/JsArrayTest.php +++ b/tests/unit/JsArrayTest.php @@ -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()); } }