Skip to content

Commit a34e2ae

Browse files
committed
Incorporate the "when" keyword into the string-building loops for more natural-reading code.
1 parent f74f6b4 commit a34e2ae

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

chapters/design_patterns/builder.textile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ TodoTxtBuilder = (defaultParameters={ }) ->
2626
projects = @projects.concat(parameters.projects or [ ])
2727
priorityLevel = parameters.priority or @priority
2828
createdAt = [date.getFullYear(), date.getMonth()+1, date.getDate()].join("-")
29-
contextNames = ("@#{context}" for context in contexts).join(" ")
30-
projectNames = ("+#{project}" for project in projects).join(" ")
29+
contextNames = ("@#{context}" for context in contexts when context).join(" ")
30+
projectNames = ("+#{project}" for project in projects when project).join(" ")
3131
priority = if priorityLevel then "(#{priorityLevel})" else ""
32-
[priority, createdAt, description, contextNames, projectNames].reduce (whole, part) ->
33-
if part then (whole and whole + " ") + part else whole
32+
todoParts = [priority, createdAt, description, contextNames, projectNames]
33+
(part for part in todoParts when part.length > 0).join " "
3434

3535
builder = new TodoTxtBuilder(date: "10/13/2011")
3636

3737
builder.newTodo "Wash laundry"
3838

39-
# => 2011-10-13 Wash laundry
39+
# => '2011-10-13 Wash laundry'
4040

4141
workBuilder = new TodoTxtBuilder(date: "10/13/2011", contexts: ["work"])
4242

0 commit comments

Comments
 (0)