Skip to content

Commit dfcd74d

Browse files
authored
Merge pull request #1 from adhocore/develop
Develop
2 parents 81cfdfe + f433638 commit dfcd74d

File tree

6 files changed

+343
-50
lines changed

6 files changed

+343
-50
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
language: php
22

33
php:
4-
- 5.5
54
- 5.6
65
- 7.0
76
- 7.1
7+
- 7.2
88

99
install:
1010
- composer install --prefer-dist

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Heavily work in progress and not yet ready!
1111

1212
## Installation
1313

14-
Requires PHP5.5 or later.
14+
Requires PHP5.6 or later.
1515

1616
```bash
1717
composer require adhocore/underscore

src/Helper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ public static function asArray($data)
2525
return $data->get();
2626
}
2727

28+
// @codeCoverageIgnoreStart
2829
if ($data instanceof \Traversable) {
2930
return \iterator_to_array($data);
3031
}
32+
// @codeCoverageIgnoreEnd
3133

3234
if ($data instanceof \JsonSerializable) {
3335
return $data->jsonSerialize();

src/Underscore.php

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public function each(callable $fn)
4444
return $this;
4545
}
4646

47-
public function forEach(callable $fn)
48-
{
49-
return $this->each($fn);
50-
}
51-
5247
public function map(callable $fn)
5348
{
5449
$data = [];
@@ -82,38 +77,31 @@ public function inject(callable $fn, $memo)
8277

8378
public function reduceRight(callable $fn, $memo)
8479
{
85-
// fix!
86-
return array_reduce($this->data, $fn, $memo);
80+
return array_reduce(array_reverse($this->data), $fn, $memo);
8781
}
8882

8983
public function foldr(callable $fn, $memo)
9084
{
9185
return $this->reduceRight($fn, $memo);
9286
}
9387

94-
public function find(callabe $fn)
88+
public function find(callable $fn)
9589
{
9690
foreach ($this->data as $index => $value) {
97-
if ($fn($value, $key)) {
91+
if ($fn($value, $index)) {
9892
return $value;
9993
}
10094
}
10195
}
10296

103-
public function detect(callabe $fn)
97+
public function detect(callable $fn)
10498
{
10599
return $this->find($fn);
106100
}
107101

108102
public function filter(callable $fn)
109103
{
110-
$data = [];
111-
112-
foreach ($this->data as $index => $value) {
113-
if ($fn($value, $index)) {
114-
$data[$index] = $value;
115-
}
116-
}
104+
$data = array_filter($this->data, $fn, \ARRAY_FILTER_USE_BOTH);
117105

118106
return new static($data);
119107
}
@@ -125,26 +113,21 @@ public function select(callable $fn)
125113

126114
public function reject(callable $fn)
127115
{
128-
$data = [];
129-
130-
foreach ($this->data as $index => $value) {
131-
if (!$fn($value, $index)) {
132-
$data[$index] = $value;
133-
}
134-
}
116+
$data = array_filter($this->data, $this->negate($fn), \ARRAY_FILTER_USE_BOTH);
135117

136118
return new static($data);
137119
}
138120

139-
public function every(callable $fn)
121+
protected function negate(callable $fn)
140122
{
141-
foreach ($this->data as $index => $value) {
142-
if (!$fn($value, $index)) {
143-
return false;
144-
}
145-
}
123+
return function () use ($fn) {
124+
return !call_user_func_array($fn, func_get_args());
125+
};
126+
}
146127

147-
return true;
128+
public function every(callable $fn)
129+
{
130+
return $this->match($fn, true);
148131
}
149132

150133
public function all(callable $fn)
@@ -154,31 +137,35 @@ public function all(callable $fn)
154137

155138
public function some(callable $fn)
156139
{
157-
foreach ($this->data as $index => $value) {
158-
if ($fn($value, $index)) {
159-
return true;
160-
}
161-
}
162-
163-
return false;
140+
return $this->match($fn, false);
164141
}
165142

166143
public function any(callable $fn)
167144
{
168145
return $this->some($fn);
169146
}
170147

171-
public function contains($item)
148+
protected function match(callable $fn, $all = true)
172149
{
173-
return \in_array($item, $this->data);
150+
foreach ($this->data as $index => $value) {
151+
if ($all && !$fn($value, $index)) {
152+
return false;
153+
}
154+
155+
if (!$all && $fn($value, $index)) {
156+
return true;
157+
}
158+
}
159+
160+
return $all;
174161
}
175162

176-
public function includes($item)
163+
public function contains($item)
177164
{
178-
return $this->contains($item);
165+
return \in_array($item, $this->data);
179166
}
180167

181-
public function include($item)
168+
public function includes($item)
182169
{
183170
return $this->contains($item);
184171
}
@@ -190,15 +177,34 @@ public function invoke(callable $fn)
190177

191178
public function pluck($columnKey, $indexKey = null)
192179
{
193-
if (\function_exists('array_column')) {
194-
$data = \array_column($this->data, $columnKey, $indexKey);
195-
} else {
196-
$data = Helper::arrayColumn($this->data, $columnKey, $indexKey);
197-
}
180+
$data = \array_column($this->data, $columnKey, $indexKey);
198181

199182
return new static($data);
200183
}
201184

185+
public function where(array $props)
186+
{
187+
return $this->filter($this->matcher($props));
188+
}
189+
190+
public function findWhere(array $props)
191+
{
192+
return $this->find($this->matcher($props));
193+
}
194+
195+
protected function matcher(array $props)
196+
{
197+
return function ($value, $index) use ($props) {
198+
foreach ($props as $prop => $criteria) {
199+
if (\array_column([$value], $prop) != [$criteria]) {
200+
return false;
201+
}
202+
}
203+
204+
return true;
205+
};
206+
}
207+
202208
/**
203209
* {@inheritdoc}
204210
*/
@@ -262,6 +268,11 @@ public function __toString()
262268
{
263269
return \json_encode($this->data);
264270
}
271+
272+
public static function _($data)
273+
{
274+
return new static($data);
275+
}
265276
}
266277

267278
\class_alias('Ahc\Underscore\Underscore', 'Ahc\Underscore');

0 commit comments

Comments
 (0)