Skip to content

Commit

Permalink
Merge pull request #20 from ba-st/CollectionFormatter
Browse files Browse the repository at this point in the history
Added CollectionFormatter
  • Loading branch information
gcotelli authored Jan 7, 2018
2 parents 2a044bd + e919f6b commit 48201b8
Show file tree
Hide file tree
Showing 25 changed files with 155 additions and 23 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm a CollectionFormatterTest, a test class for testing the behavior of CollectionFormatter
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests
testFormat

| formatter |
formatter := CollectionFormatter separatingWith: $,.
self assert: (formatter format: #(1 2 3)) equals: '1, 2, 3'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests
testFormatEmptyCollection

| formatter |
formatter := CollectionFormatter separatingWith: $, andLastWith: $y.
self assert: (formatter format: #()) equals: String new
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests
testFormatOn

| formatter stream |
stream := WriteStream on: String new.
formatter := CollectionFormatter separatingWith: $,.
formatter format: #(1 2 3) on: stream.
self assert: stream contents equals: '1, 2, 3'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests
testFormatOneElementCollection

| formatter |
formatter := CollectionFormatter separatingWith: $, andLastWith: $y.
self assert: (formatter format: #(1)) equals: '1'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests
testFormatTwoElementCollectionWithLastSeparator

| formatter |
formatter := CollectionFormatter separatingWith: $, andLastWith: $y.
self assert: (formatter format: #(1 2)) equals: '1 y 2'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests
testFormatWithElementFormater

| formatter |

formatter := CollectionFormatter
separatingWith: $,
applyingToEach: [ :element | '/<1p>/' expandMacrosWith: element ].
self assert: (formatter format: #(1 2 3)) equals: '/1/, /2/, /3/'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests
testFormatWithLastSeparator

| formatter |
formatter := CollectionFormatter separatingWith: $, andLastWith: $y.
self assert: (formatter format: #(1 2 3)) equals: '1, 2 y 3'
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"commentStamp" : "fortizpenaloza 1/5/2018 14:55",
"super" : "TestCase",
"category" : "Buoy-Collections-Tests",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [ ],
"name" : "CollectionFormatterTest",
"type" : "normal"
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
I'm a collection formatter allowing format a collection separating its elements with a provided separator. I allow to use a diferent separator for the last element and also a block to perform a element specific formatting.

Implementation Notes:
I use a index to detect the last element and use the specific separator if present.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instance creation
separatingWith: aSeparator

^ self
separatingWith: aSeparator
andOptionallyLastWith: Optional unused
applyingToEach: [ :element | element asString ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instance creation
separatingWith: aSeparator andLastWith: aLastSeparator

^ self
separatingWith: aSeparator
andOptionallyLastWith: (Optional containing: aLastSeparator)
applyingToEach: [ :element | element asString ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instance creation
separatingWith: aSeparator andLastWith: aLastSeparator applyingToEach: anElementFormatter

^ self
separatingWith: aSeparator
andOptionallyLastWith: (Optional containing: aLastSeparator)
applyingToEach: anElementFormatter
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instance creation
separatingWith: aSeparator andOptionallyLastWith: anOptionalSeparator applyingToEach: anElementFormatter

^ self new
initializeSeparatingEachWith: aSeparator
andOptionallyLastWith: anOptionalSeparator
applyingToEach: anElementFormatter
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instance creation
separatingWith: aSeparator applyingToEach: anElementFormatter

^ self
separatingWith: aSeparator
andOptionallyLastWith: Optional unused
applyingToEach: anElementFormatter
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
formatting
format: aCollection

^ self format: aCollection on: (WriteStream on: String new)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
formatting
format: aCollection on: aStream

| index size |

index := 1.
size := aCollection size.

aCollection
do: [ :element |
index := index + 1.
aStream nextPutAll: (elementFormatter value: element) ]
separatedBy: [ index = size
ifTrue: [ lastSeparator
withContentDo: [ :content | self put: content on: aStream ]
ifUnused: [ self putSeparatorOn: aStream ] ]
ifFalse: [ self putSeparatorOn: aStream ] ].

^aStream contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
initialization
initializeSeparatingEachWith: aSeparator andOptionallyLastWith: anOptional applyingToEach: aBlock

separator := aSeparator.
lastSeparator := anOptional.
elementFormatter := aBlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
formatting
put: aSeparator on: aStream

^ aStream
nextPut: Character space;
nextPut: aSeparator;
nextPut: Character space
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
formatting
putSeparatorOn: aStream

^ aStream
nextPut: separator;
nextPut: Character space
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"commentStamp" : "fortizpenaloza 1/5/2018 14:55",
"super" : "Object",
"category" : "Buoy-Collections",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"separator",
"elementFormatter",
"lastSeparator"
],
"name" : "CollectionFormatter",
"type" : "normal"
}
1 change: 0 additions & 1 deletion source/Buoy-Collections.package/monticello.meta/version

This file was deleted.

0 comments on commit 48201b8

Please sign in to comment.