Skip to content

Commit

Permalink
✨ add JsArray::from() static method for array generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamed committed May 16, 2021
1 parent 97d4765 commit a7f56c1
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
8 changes: 4 additions & 4 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
use Ahamed\JsPhp\JsArray;
use Ahamed\JsPhp\JsObject;

$array = new JsArray(['I play dota', 'dota is a good game']);
$mapped = $array->flatMap(fn($item) => explode(' ', $item))
->filter(fn($item) => boolval($item));

$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 !== ' ');

echo '<xmp>';
print_r($mapped);
print_r($array);
echo '</xmp>';
die();

33 changes: 33 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,39 @@ $array->forEach($callback($item [, $index [, $key]]));
`NULL`
___

### # JsArray::from
The `JsArray::from` static method responsible for creating a JsArray instance from given sequential array or string. This can also create an array if you provide a special associative array `['length' => integer_value]`.

#### Example
```php
$array = JsArray::from([1, 2, 3, 4]);
print_r($array->get()); // Output: [1, 2, 3, 4]

$array = JsArray::from("foo");
print_r($array->get()); // Output: ['f', 'o', 'o'];

$array = JsArray::from(['length' => 4], fn($x, $i) => $i);
print_r($array->get()); // Output: [0, 1, 2, 3]

$array = JsArray::from([1, 2, 3], fn($x) => $x * 2);
print_r($array->get()); // Output: [2, 4, 6]
```
#### Syntax
```php
$array = JsArray::from($iterable [, $callable]);
```

#### Parameters
- ___`$iterable`___
The iterable array or string to convert to JsArray. The `$iterable` can contains either array or string. There may be a special associative array with providing length property by which you can generate an array with the length value.
- ___`$callable`___ _(optional)_
The map function to call on every item of the array.

#### Return Value
A JsArray Instance with generated array items.

---

### # includes
The `includes()` method determines whether an array includes a certain value among its entries, returning `true` or `false` as appropriate. This checks a loose equity for finding the value and for string it's case sensitive.

Expand Down
60 changes: 60 additions & 0 deletions src/Traits/Arrays/BasicsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,66 @@
*/
trait BasicsTrait
{
/**
* Array::from method for generating an array (or JsArray instance) from an iterable
* with a user defined callable modifier.
*
* @param array|string $iterable The iterable.
* @param callable $callable The user defined callable.
*
* @return JsArray The created JsArray instance.
* @since 1.0.0
*/
public static function from($iterable, callable $callable = null) : JsArray
{
$array = new JsArray();
$localArray = [];

if ($iterable instanceof JsArray)
{
$iterable = $iterable->get();
}

if (\is_string($iterable))
{
$iterable = \str_split($iterable, 1);
}

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

if ($isAssoc && !isset($iterable['length']))
{
return $array->bind([], false);
}
elseif ($isAssoc && isset($iterable['length']))
{
$length = (int) $iterable['length'];

for ($i = 0; $i < $length; $i++)
{
if (!$callable)
{
$localArray[] = null;
}
else
{
$localArray[] = \call_user_func_array($callable, [null, $i]);
}
}

return $array->bind($localArray, false);
}

foreach ($iterable as $index => $item)
{
$localArray[$index] = $callable
? \call_user_func_array($callable, [$item, $index])
: $item;
}

return $array->bind($localArray, false);
}

/**
* Array at method. This method will return the value of the array in the
* provided at index. The benefit over the square bracket index is that it
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/JsArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,32 @@ public function testCountInstance($data, $result)

$this->assertEquals(count($array), $result);
}

/**
*
*/
public function testArrayFrom()
{
$array = JsArray::from([1, 2, 3]);
$this->assertEquals([1, 2, 3], $array->get());

$array = JsArray::from(['length' => 5]);
$this->assertEquals([null, null, null, null, null], $array->get());

$array = JsArray::from(['length' => 4], function($_, $index) {
return $index;
});
$this->assertEquals([0, 1, 2, 3], $array->get());

$array = JsArray::from([1, 2, 3, 4, 5], function($item) {
return $item * $item;
});
$this->assertEquals([1, 4, 9, 16, 25], $array->get());

$array = JsArray::from(['name' => 'John Doe', 'age' => 24]);
$this->assertEquals([], $array->get());

$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());
}
}

0 comments on commit a7f56c1

Please sign in to comment.