Skip to content

Commit

Permalink
a new take on toList and more tests for passing a different separator
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmatic69 authored and markstory committed Nov 25, 2009
1 parent 398113f commit 53bfc8c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
14 changes: 2 additions & 12 deletions cake/libs/view/helpers/text.php
Expand Up @@ -331,18 +331,8 @@ function excerpt($text, $phrase, $radius = 100, $ending = '...') {
* @return string
* @access public
*/
function toList($list, $and = 'and') {
$return = '';
$count = count($list) - 1;
$counter = 0;
foreach ($list as $i => $item) {
$return .= $item;
if ($count > 0 && $counter < $count) {
$return .= ($counter < $count - 1 ? ', ' : " {$and} ");
}
$counter++;
}
return $return;
function toList($list, $and = 'and', $separator = ', ') {
return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop(array_slice($list, -1));
}
}
?>
10 changes: 8 additions & 2 deletions cake/tests/cases/libs/view/helpers/text.test.php
Expand Up @@ -363,8 +363,14 @@ function testListGeneration() {
$result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y');
$this->assertEqual($result, 'Dusty, Lucky y Ned');

$result = $this->Text->toList(array( 1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'y');
$this->assertEqual($result, 'Dusty, Lucky y Ned');
$result = $this->Text->toList(array( 1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'y');
$this->assertEqual($result, 'Dusty, Lucky y Ned');

$result = $this->Text->toList(array( 1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'and', ' + ');
$this->assertEqual($result, 'Dusty + Lucky and Ned');

$result = $this->Text->toList(array( 'name1' => 'Dusty', 'name2' => 'Lucky'));
$this->assertEqual($result, 'Dusty and Lucky');
}
}
?>

0 comments on commit 53bfc8c

Please sign in to comment.