Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ export class ModelFormat<T extends Entity> extends Format<T> {
var result = "";
for (var index = 0; index < this.tokens.length; index++) {
var token = this.tokens[index];
if (token.prefix)
result = result + token.prefix;
if (token.path) {
var value = evalPath(obj, token.path);
if (value === undefined || value === null) {
Expand All @@ -305,8 +303,13 @@ export class ModelFormat<T extends Entity> extends Format<T> {
if (Array.isArray(value))
value = value.join(", ");

if (token.prefix && ((result !== "" && value !== "") || (index === 0 && value !== "")))
result = result + token.prefix;

result = result + value;
}
else if (token.prefix)
result = result + token.prefix;
}
return result;
};
Expand Down
36 changes: 36 additions & 0 deletions src/format.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,40 @@ describe("format", () => {
let form = await model.types.Form.create({}) as any;
expect(form.toString("[Table]")).toBe("Text2, Text2");
});

test("Prefixes are not prepended if there is no value and result", async () => {
var model = new Model({
"Form": {
Name: {
label: "Name",
format: "[Prefix] [First] [MiddleInitial] [Last] [Suffix]",

@tskimmett Taylor Kimmett (tskimmett) Sep 16, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the format looks like this, and one or both of First/Last is null/empty?

"'[First]' '[Last]'"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'John'
Doe'
'
Seems like an issue

type: "Name"
}
},
"Name": {
First: {
label: "First",
type: String
},
Last: {
label: "Last",
type: String
},
MiddleInitial: {
label: "Middle Initial",
type: String
},
Prefix: {
label: "Prefix",
type: String
},
Suffix: {
label: "Suffix",
type: String
}
}
});
let form = await model.types.Form.create({ Name: { First: "John", Last: "Doe" } }) as any;
expect(form.toString("[Name]")).toBe("John Doe");
});
});
1 change: 0 additions & 1 deletion src/type.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe("Type", () => {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
let parent = await model.types.Parent.create({ Child: "1" });
console.log("test after await");
expect((parent as any).Child.Sibling.Name).toBe("Sibling Name");
});

Expand Down