Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
Use destructuring for optional parameters
Browse files Browse the repository at this point in the history
Unfortunately, ts does not support mixing destructuring and parameter
properties right now
(see microsoft/TypeScript#5326).
This still imporves maintainability.

Addresses MIT-166
  • Loading branch information
zechmeister committed Feb 7, 2022
1 parent ce529c0 commit 4a074c9
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 117 deletions.
122 changes: 77 additions & 45 deletions src/domain/Step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,24 @@ export class TextAnswerStep extends Step<TextAnswer> {
// We need to capture the type manually, as at runtime it's not available,
// and because Vue uses proxies, thus we can't compare constructors...
static readonly TYPE = "TextAnswerStep"
public readonly produce: string

constructor(
text: string,
answer: TextAnswer = new TextAnswer(),
public readonly produce: string = "${answer}",
description?: string,
id?: string
{
answer = new TextAnswer(),
produce = "${answer}",
description,
id,
}: {
answer?: TextAnswer
produce?: string
description?: string
id?: string
} = {}
) {
super(text, answer, description, id)
this.produce = produce
}

get type(): string {
Expand All @@ -93,28 +102,36 @@ export class TextAnswerStep extends Step<TextAnswer> {
}

clone(): TextAnswerStep {
return new TextAnswerStep(
this.text,
new TextAnswer(this.answer.value),
this.produce,
this.description
)
return new TextAnswerStep(this.text, {
answer: new TextAnswer(this.answer.value),
produce: this.produce,
description: this.description,
})
}
}

export class RichTextAnswerStep extends Step<RichTextAnswer> {
// We need to capture the type manually, as at runtime it's not available,
// and because Vue uses proxies, thus we can't compare constructors...
static readonly TYPE = "RichTextAnswerStep"
public readonly produce: string

constructor(
text: string,
answer: RichTextAnswer = new RichTextAnswer(),
public readonly produce: string = "${answer}",
description?: string,
id?: string
{
answer = new RichTextAnswer(),
produce = "${answer}",
description,
id,
}: {
answer?: RichTextAnswer
produce?: string
description?: string
id?: string
} = {}
) {
super(text, answer, description, id)
this.produce = produce
}

get type(): string {
Expand All @@ -129,12 +146,11 @@ export class RichTextAnswerStep extends Step<RichTextAnswer> {
}

clone(): RichTextAnswerStep {
return new RichTextAnswerStep(
this.text,
new RichTextAnswer(this.answer.value),
this.produce,
this.description
)
return new RichTextAnswerStep(this.text, {
answer: new RichTextAnswer(this.answer.value),
produce: this.produce,
description: this.description,
})
}
}

Expand All @@ -145,9 +161,15 @@ export class SingleChoiceAnswerStep extends Step<SingleChoiceAnswer> {

constructor(
text: string,
answer: SingleChoiceAnswer = new SingleChoiceAnswer([]),
description?: string,
id?: string
{
answer = new SingleChoiceAnswer([]),
description,
id,
}: {
answer?: SingleChoiceAnswer
description?: string
id?: string
} = {}
) {
super(text, answer, description, id)
}
Expand All @@ -169,14 +191,13 @@ export class SingleChoiceAnswerStep extends Step<SingleChoiceAnswer> {
}

clone(): SingleChoiceAnswerStep {
return new SingleChoiceAnswerStep(
this.text,
new SingleChoiceAnswer(
return new SingleChoiceAnswerStep(this.text, {
answer: new SingleChoiceAnswer(
this.choices.map((choice) => choice.clone()),
this.answer.value
),
this.description
)
description: this.description,
})
}
}

Expand All @@ -187,9 +208,15 @@ export class MultipleChoiceAnswerStep extends Step<MultipleChoiceAnswer> {

constructor(
text: string,
answer: MultipleChoiceAnswer = new MultipleChoiceAnswer([]),
description?: string,
id?: string
{
answer = new MultipleChoiceAnswer([]),
description,
id,
}: {
answer?: MultipleChoiceAnswer
description?: string
id?: string
} = {}
) {
super(text, answer, description, id)
}
Expand All @@ -211,43 +238,48 @@ export class MultipleChoiceAnswerStep extends Step<MultipleChoiceAnswer> {
}

clone(): MultipleChoiceAnswerStep {
return new MultipleChoiceAnswerStep(
this.text,
new MultipleChoiceAnswer(
return new MultipleChoiceAnswerStep(this.text, {
answer: new MultipleChoiceAnswer(
this.choices.map((choice) => choice.clone()),
this.answer.value
),
this.description
)
description: this.description,
})
}
}

export class SheetAnswerStep extends Step<SheetAnswer> {
// We need to capture the type manually, as at runtime it's not available,
// and because Vue uses proxies, thus we can't compare constructors...
static readonly TYPE = "SheetAnswerStep"
public readonly produce: string

constructor(
text: string,
answer: SheetAnswer,
public readonly produce: string = "${answer}",
description?: string,
id?: string
{
produce = "${answer}",
description,
id,
}: {
produce?: string
description?: string
id?: string
} = {}
) {
super(text, answer, description, id)
this.produce = produce
}

get type(): string {
return SheetAnswerStep.TYPE
}

clone(): SheetAnswerStep {
return new SheetAnswerStep(
this.text,
new SheetAnswer(this.answer.value),
this.produce,
this.description
)
return new SheetAnswerStep(this.text, new SheetAnswer(this.answer.value), {
produce: this.produce,
description: this.description,
})
}

updateCell(rowIndex: number, cell: string, value: string) {
Expand Down
60 changes: 27 additions & 33 deletions src/infra/JSONMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,61 +67,55 @@ export type PlaybookDTO = {
export function createStep(step: StepDTO): Step<Answer> {
if (step.type === TextAnswerStep.TYPE) {
const answer = step.answer as TextAnswerDTO
return new TextAnswerStep(
step.text,
new TextAnswer(answer.value),
step.produce,
step.description,
step.id
)
return new TextAnswerStep(step.text, {
answer: new TextAnswer(answer.value),
produce: step.produce,
description: step.description,
id: step.id,
})
}
if (step.type === RichTextAnswerStep.TYPE) {
const answer = step.answer as RichTextAnswerDTO
return new RichTextAnswerStep(
step.text,
new RichTextAnswer(answer.value),
step.produce,
step.description,
step.id
)
return new RichTextAnswerStep(step.text, {
answer: new RichTextAnswer(answer.value),
produce: step.produce,
description: step.description,
id: step.id,
})
}
if (step.type === SingleChoiceAnswerStep.TYPE) {
const answer = step.answer as SingleChoiceAnswerDTO
return new SingleChoiceAnswerStep(
step.text,
new SingleChoiceAnswer(
return new SingleChoiceAnswerStep(step.text, {
answer: new SingleChoiceAnswer(
answer.choices.map(
(choice) => new Choice(choice.text, choice.path.map(createStep))
),
answer.value
),
step.description,
step.id
)
description: step.description,
id: step.id,
})
}
if (step.type === MultipleChoiceAnswerStep.TYPE) {
const answer = step.answer as MultipleChoiceAnswerDTO
return new MultipleChoiceAnswerStep(
step.text,
new MultipleChoiceAnswer(
return new MultipleChoiceAnswerStep(step.text, {
answer: new MultipleChoiceAnswer(
answer.choices.map(
(choice) => new Choice(choice.text, choice.path.map(createStep))
),
answer.value
),
step.description,
step.id
)
description: step.description,
id: step.id,
})
}
if (step.type === SheetAnswerStep.TYPE) {
const answer = step.answer as SheetAnswerDTO
return new SheetAnswerStep(
step.text,
new SheetAnswer(answer.value),
step.produce,
step.description,
step.id
)
return new SheetAnswerStep(step.text, new SheetAnswer(answer.value), {
produce: step.produce,
description: step.description,
id: step.id,
})
}
console.error("Step type unknown, cannot deserialize JSON", step)
throw new Error("Step type unknown")
Expand Down
2 changes: 1 addition & 1 deletion test/components/contract/RichTextHelper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("RichTextHelper", () => {
...RichTextEditorConfig,
content: "<p>foo<strong>bar</strong></p><p>baz</p>",
})
const step = new RichTextAnswerStep("foo", undefined, "Prefix: ${answer}")
const step = new RichTextAnswerStep("foo", { produce: "Prefix: ${answer}" })
expect(editorContentAsAnswerValue(editor, step)).toEqual(
"foo<strong>bar</strong><p>baz</p>"
)
Expand Down
Loading

0 comments on commit 4a074c9

Please sign in to comment.