Skip to content

Commit ca9c217

Browse files
committed
nitpicks
1 parent 15f1c84 commit ca9c217

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-collection-macros` will be documented in this file
44

5+
## 2.6.0 - 2017-08-22
6+
7+
- Added `tail`, `eachCons`, `sliceBefore` and `chunkBy`
8+
59
## 2.5.0 - 2017-08-03
610
- Added customization options for `sectionBy`
711

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,30 +446,30 @@ Extract the tail from a collection. So everything except the first element.
446446
It's a shorthand for `slice(1)->values()`, but nevertheless very handy.
447447

448448
```php
449-
collect([1, 2, 3))->tail(); // return Collection([2, 3])
449+
collect([1, 2, 3))->tail(); // return collect([2, 3])
450450
```
451451

452452
### `eachCons`
453453

454-
Get the following consecutive neighbours in a collection from a given chunk size
454+
Get the following consecutive neighbours in a collection from a given chunk size.
455455

456456
```php
457-
collect([1, 2, 3, 4])->eachCons(2); // return Collection([[1, 2], [2, 3], [3, 4]])
457+
collect([1, 2, 3, 4])->eachCons(2); // return collect([[1, 2], [2, 3], [3, 4]])
458458
```
459459

460460
### `sliceBefore`
461461

462-
Slice the values out from a collection before the given callback is true
462+
Slice the values out from a collection before the given callback is true.
463463

464464
```php
465465
collect([20, 51, 10, 50, 66])->sliceBefore(function($item) {
466466
return $item > 50;
467-
}); // return Collection([[20],[51, 10]])
467+
}); // return collect([[20],[51, 10]])
468468
```
469469

470470
### `chunkBy`
471471

472-
Chunks the values from a collection into groups as long the given callback is true
472+
Chunks the values from a collection into groups as long the given callback is true.
473473

474474
```php
475475
collect(['A', 'A', 'B', 'A'])->chunkBy(function($item) {

src/macros.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,10 @@
418418
*/
419419
Collection::macro('eachCons', function ($chunkSize) {
420420
if ($this->count() < $chunkSize) {
421-
return new self;
421+
return new static();
422422
}
423423

424-
return (new self([$this->take($chunkSize)->values()]))
424+
return (new static([$this->take($chunkSize)->values()]))
425425
->merge($this->tail()->eachCons($chunkSize));
426426
});
427427
}
@@ -434,20 +434,19 @@
434434
*/
435435
Collection::macro('sliceBefore', function ($callback) {
436436
if ($this->isEmpty()) {
437-
return new self;
437+
return new static();
438438
}
439439

440-
$sliced = new self([
441-
new self([$this->first()]),
440+
$sliced = new static([
441+
new static([$this->first()]),
442442
]);
443443

444-
return $this->eachCons(2)->reduce(function ($sliced, $prevAndCurr) use ($callback) {
445-
list($previousItem, $item) = $prevAndCurr;
446-
if ($callback($item, $previousItem)) {
447-
$sliced->push(new self([$item]));
448-
} else {
449-
$sliced->last()->push($item);
450-
}
444+
return $this->eachCons(2)->reduce(function ($sliced, $previousAndCurrent) use ($callback) {
445+
list($previousItem, $item) = $previousAndCurrent;
446+
447+
$callback($item, $previousItem)
448+
? $sliced->push(new static([$item]))
449+
: $sliced->last()->push($item);
451450

452451
return $sliced;
453452
}, $sliced);

0 commit comments

Comments
 (0)