Skip to content

Commit

Permalink
Added lines to collection helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nikuscs committed Nov 6, 2024
1 parent 9027c54 commit 82ae380
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ testbench.yaml
vendor
node_modules
.php-cs-fixer.cache
.history
38 changes: 38 additions & 0 deletions src/Macros/StrMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function register(): void
self::prefix();
self::split();
self::extractors();
self::linesToCollection();
}

public static function username(): void
Expand Down Expand Up @@ -374,4 +375,41 @@ public static function normalizeCharacters(): void
return strtr($string, $chars);
});
}

/**
* Convert a delimited string to a collection of lines
*/
public static function linesToCollection(): void
{
Str::macro('lines_to_collection', function (
string|array $value,
string $delimiter = ',',
bool $unique = true
): Collection {
// Handle array input
$items = is_array($value)
? $value
: explode($delimiter, $value);

Check failure on line 392 in src/Macros/StrMacros.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $separator of function explode expects non-empty-string, string given.

// Create collection and process items
return Collection::make($items)
->map(fn ($item) => trim($item))
->filter()
->when($unique, function (Collection $items) {

Check failure on line 398 in src/Macros/StrMacros.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $callback of method Illuminate\Support\Collection<(int|string),string>::when() expects (callable(Illuminate\Support\Collection<int|string, non-falsy-string>, bool): Illuminate\Support\Collection<int|string, string>)|null, Closure(Illuminate\Support\Collection): Illuminate\Support\Collection<int|string, non-falsy-string> given.
// Use a temporary hash map for faster duplicate checking
$seen = [];

return $items->filter(function ($item) use (&$seen) {
$hash = md5($item);
if (isset($seen[$hash])) {
return false;
}
$seen[$hash] = true;

return true;
});
})
->values(); // Reset array keys
});
}
}
72 changes: 72 additions & 0 deletions tests/Macros/StrTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Flavorly\LaravelHelpers\Tests\Macros;

use Flavorly\LaravelHelpers\Tests\TestCase;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class StrTest extends TestCase
{
/** @test */
public function it_can_convert_lines_to_collection()
{
// Test basic string conversion
$result = Str::linesToCollection('apple,banana,cherry');
$this->assertInstanceOf(Collection::class, $result);
$this->assertEquals(['apple', 'banana', 'cherry'], $result->all());

// Test with custom delimiter
$result = Str::linesToCollection('apple|banana|cherry', '|');
$this->assertEquals(['apple', 'banana', 'cherry'], $result->all());

// Test with duplicates and unique=true (default)
$result = Str::linesToCollection('apple,banana,apple,cherry');
$this->assertEquals(['apple', 'banana', 'cherry'], $result->all());

// Test with duplicates and unique=false
$result = Str::linesToCollection('apple,banana,apple,cherry', ',', false);
$this->assertEquals(['apple', 'banana', 'apple', 'cherry'], $result->all());

// Test with spaces and trimming
$result = Str::linesToCollection(' apple , banana , cherry ');
$this->assertEquals(['apple', 'banana', 'cherry'], $result->all());

// Test with empty values
$result = Str::linesToCollection('apple,,banana,,cherry');
$this->assertEquals(['apple', 'banana', 'cherry'], $result->all());

// Test with array input
$result = Str::linesToCollection(['apple', 'banana', 'apple', 'cherry']);
$this->assertEquals(['apple', 'banana', 'cherry'], $result->all());

// Test with mixed case duplicates
$result = Str::linesToCollection('Apple,apple,APPLE,banana');
$this->assertEquals(['Apple', 'apple', 'APPLE', 'banana'], $result->all());
}

/** @test */
public function it_handles_edge_cases_for_lines_to_collection()
{
// Empty string
$result = Str::linesToCollection('');
$this->assertInstanceOf(Collection::class, $result);
$this->assertEquals([], $result->all());

// Single value
$result = Str::linesToCollection('apple');
$this->assertEquals(['apple'], $result->all());

// Only delimiters
$result = Str::linesToCollection(',,,');
$this->assertEquals([], $result->all());

// Only spaces
$result = Str::linesToCollection(' ');
$this->assertEquals([], $result->all());

// Unicode characters
$result = Str::linesToCollection('café,résumé,naïve');
$this->assertEquals(['café', 'résumé', 'naïve'], $result->all());
}
}

0 comments on commit 82ae380

Please sign in to comment.