Skip to content

Commit

Permalink
Item2301: add "$nop" for empty separator for $LISTJOIN; more unit tes…
Browse files Browse the repository at this point in the history
…ts for $LISTJOIN

git-svn-id: http://svn.foswiki.org/trunk@5868 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
WillNorris authored and WillNorris committed Dec 29, 2009
1 parent 9b483f5 commit 39ffac7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion SpreadSheetPlugin/data/System/SpreadSheetPlugin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ Conventions for Syntax:

#FuncLISTJOIN
---+++ LISTJOIN( separator, list ) -- convert a list into a string
* By default, list items are separated by a comma and a space. Use this function to indicate a specific =separator= string, which may include =$comma= for comma, =$n= for newline, and =$sp= for space.
* By default, list items are separated by a comma and a space. Use this function to indicate a specific =separator= string, which may include =$comma= for comma, =$n= for newline, =$sp= for space, and =$nop= for no separator between list items.
* Syntax: ==$LISTJOIN( separator, list )==
* Example: ==%<nop>CALC{"$LISTJOIN($n, Apple, Orange, Apple, Kiwi)"}%== returns the four items separated by new lines
* Related: =[[#FuncLIST][$LIST()]]=, =[[#FuncLISTSIZE][$LISTSIZE()]]=
Expand Down Expand Up @@ -771,6 +771,7 @@ Note that the =DONTSPACE= global preference overrides the =SPREADSHEETPLUGIN_DON
| Version: | %$VERSION% |
| Release: | %$RELEASE% |
| Change History: | <!-- specify latest version first -->&nbsp; |
| 29 Dec 2009: | Foswikitask:Item2301: added =$nop= to $LISTJOIN() for better empty parameter |
| 10 Nov 2009: | Added unit tests |
| 27 Oct 2009: | Foswikitask:Item2301: Fixed $LISTJOIN() to accept an empty separator |
| 20 Sep 2009: | Minor documentation update. trunk and release branch code synced (mainly perltidy - all functional changes have been in sync). |
Expand Down
14 changes: 8 additions & 6 deletions SpreadSheetPlugin/lib/Foswiki/Plugins/SpreadSheetPlugin/Calc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1026,13 +1026,15 @@ s/\$([A-Z]+)$escToken([0-9]+)\((.*?)$escToken\2\)/&doFunc($1,$3)/geo;
elsif ( $theFunc eq "LISTJOIN" ) {
my ( $sep, $str ) = _properSplit( $theAttr, 2 );
$str = "" unless ( defined($str) );
# SMELL: repairing standard delimiter ", " in the constructed string to our custom separator
$result = _listToDelimitedString( getList($str) );
$sep = ', ' unless defined $sep;
$sep =~ s/\$comma/,/go;
$sep =~ s/\$sp/ /go;
$sep =~ s/\$n/\n/go;
$result =~ s/, /$sep/go;

if ( length $sep ) {
$sep =~ s/\$comma/,/go;
$sep =~ s/\$sp/ /go;
$sep =~ s/\$nop//go; # make sure $nop appears before $n otherwise you end up with "\nop"
$sep =~ s/\$n/\n/go;
$result =~ s/, /$sep/go;
}
}
elsif ( $theFunc eq "LISTSIZE" ) {
my @arr = getList($theAttr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,16 @@ sub test_LISTITEM {

sub test_LISTJOIN {
my ($this) = @_;
$this->assert( $this->CALC( '$LISTJOIN(,1,2,3)' ) eq '1, 2, 3' );
$this->assert( $this->CALC( '$LISTJOIN($comma,1,2,3)' ) eq '1,2,3' );
$this->assert( $this->CALC( '$LISTJOIN(,1,2,3)' ) eq '123' );
$this->assert( $this->CALC( '$LISTJOIN($n,1,2,3)' ) eq "1\n2\n3" );
$this->assert( $this->CALC( '$LISTJOIN($sp,1,2,3)' ) eq "1 2 3" );
$this->assert( $this->CALC( '$LISTJOIN( ,1,2,3)' ) eq "1 2 3" );
$this->assert( $this->CALC( '$LISTJOIN( ,1,2,3)' ) eq "1 2 3" );
$this->assert( $this->CALC( '$LISTJOIN(:,1,2,3)' ) eq "1:2:3" );
$this->assert( $this->CALC( '$LISTJOIN(::,1,2,3)' ) eq "1::2::3" );
$this->assert( $this->CALC( '$LISTJOIN(0,1,2,3)' ) eq "10203" );
$this->assert( $this->CALC( '$LISTJOIN($nop,1,2,3)' ) eq '123' );
}

sub test_LISTMAP {
Expand Down

0 comments on commit 39ffac7

Please sign in to comment.