Skip to content

Commit 38eadaa

Browse files
committed
feat: add more of the array methods
1 parent 27f815e commit 38eadaa

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/UnderscoreArray.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,76 @@ public function uniq($fn = null)
7575
{
7676
return $this->unique($fn);
7777
}
78+
79+
public function difference($data)
80+
{
81+
return new static(\array_diff($this->data, $this->asArray($data)));
82+
}
83+
84+
public function without($data)
85+
{
86+
return $this->difference($data);
87+
}
88+
89+
public function union($data)
90+
{
91+
return new static(\array_unique(
92+
\array_merge($this->data, $this->asArray($data))
93+
));
94+
}
95+
96+
public function intersection($data)
97+
{
98+
$data = $this->asArray($data);
99+
100+
return $this->filter(function ($value, $index) use ($data) {
101+
return \in_array($value, $data);
102+
});
103+
}
104+
105+
public function zip($data)
106+
{
107+
$data = $this->asArray($data);
108+
109+
return $this->map(function ($value, $index) use ($data) {
110+
return [$value, isset($data[$index]) ? $data[$index] : null];
111+
});
112+
}
113+
114+
public function unzip()
115+
{
116+
//
117+
}
118+
119+
public function object($className = null)
120+
{
121+
return $this->map(function ($value, $index) {
122+
return $className ? new $className($value, $index) : (object) \compact('value', 'index');
123+
});
124+
}
125+
126+
public function firstIndex($fn)
127+
{
128+
return $this->find($fn, false);
129+
}
130+
131+
public function lastIndex($fn)
132+
{
133+
return (new static(\array_reverse($this->data, true)))->find($fn, false);
134+
}
135+
136+
public function indexOf($value)
137+
{
138+
return \array_search($value, $this->data);
139+
}
140+
141+
public function lastIndexOf($value)
142+
{
143+
return \array_search($value, \array_reverse($this->data, true));
144+
}
145+
146+
public function range($start, $stop, $step = 1)
147+
{
148+
return new static(\range($start, $stop, $step));
149+
}
78150
}

0 commit comments

Comments
 (0)