Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript Generator Mode


May 09, 2021 CoffeeScript


Table of contents


Generator mode

Problem

You need to prepare a complex, multi-part object that you want to operate more than once or with different configurations.

Solution

Create a generator to encapsulate the production of the object.

The Todo .txt format provides an advanced but still plain text way to maintain to-do lists. Manually entering each item is lossy and error-prone, but the TodoTxtBuilder class solves our troubles:

class TodoTxtBuilder
    constructor: (defaultParameters={ }) ->
        @date = new Date(defaultParameters.date) or new Date
        @contexts = defaultParameters.contexts or [ ]
        @projects = defaultParameters.projects or [ ]
        @priority =  defaultParameters.priority or undefined
    newTodo: (description, parameters={ }) ->
        date = (parameters.date and new Date(parameters.date)) or @date
        contexts = @contexts.concat(parameters.contexts or [ ])
        projects = @projects.concat(parameters.projects or [ ])
        priorityLevel = parameters.priority or @priority
        createdAt = [date.getFullYear(), date.getMonth()+1, date.getDate()].join("-")
        contextNames = ("@#{context}" for context in contexts when context).join(" ")
        projectNames = ("+#{project}" for project in projects when project).join(" ")
        priority = if priorityLevel then "(#{priorityLevel})" else ""
        todoParts = [priority, createdAt, description, contextNames, projectNames]
        (part for part in todoParts when part.length > 0).join " "

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

builder.newTodo "Wash laundry"

# => '2011-10-13 Wash laundry'

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

workBuilder.newTodo "Show the new design pattern to Lucy", contexts: ["desk", "xpSession"]

# => '2011-10-13 Show the new design pattern to Lucy @work @desk @xpSession'

workBuilder.newTodo "Remind Sean about the failing unit tests", contexts: ["meeting"], projects: ["compilerRefactor"], priority: 'A'

# => '(A) 2011-10-13 Remind Sean about the failing unit tests @work @meeting +compilerRefactor'

Discuss

The TodoTxtBuilder class is responsible for generating all text, allowing programmers to focus on the unique elements of each work item. In addition, command-line tools or GUI can insert this code and remain supported, providing an easy, later format.

Pre-construction

Not every time we create a new instance, we move the burden to a separate object that can be adjusted during object creation.

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

builder.newTodo "Order new netbook"

# => '2011-10-13 Order new netbook'

builder.projects.push "summerVacation"

builder.newTodo "Buy suntan lotion"

# => '2011-10-13 Buy suntan lotion +summerVacation'

builder.contexts.push "phone"

builder.newTodo "Order tickets"

# => '2011-10-13 Order tickets @phone +summerVacation'

delete builder.contexts[0]

builder.newTodo "Fill gas tank"

# => '2011-10-13 Fill gas tank +summerVacation'

Practice

  • Expand the project-and-context-tag generation code to filter out duplicate entries.

  • Some Todo .txt like to insert labels for items and contexts into task descriptions. Add code to identify the end tags for these labels and filters.