Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape curly braces #2

Merged
merged 2 commits into from
Apr 26, 2024
Merged
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Version 0.4.0

To be released.

- Curly braces can now be escaped with double curly braces


Version 0.3.0
-------------
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ logger.debug("Or you can use a function call: {value}.", () => {
});
~~~~

When using the function call, the way to log single curly braces `{` is to
double the brace `{{`:

~~~~ typescript
logger.debug("This logs {{single}} curly braces.");
~~~~


Categories
----------
Expand Down
12 changes: 12 additions & 0 deletions logtape/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,18 @@ Deno.test("parseMessageTemplate()", () => {
parseMessageTemplate("Hello, world!", { foo: 123 }),
["Hello, world!"],
);
assertEquals(
parseMessageTemplate("Hello, {{world}}!", { foo: 123 }),
["Hello, {world}!"],
);
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add a test for unpaired curly bracket?

assertEquals(
  parseMessageTemplate("Hello, {{world!", { foo: 123 }),
  ["Hello, {world!"],
);

assertEquals(
parseMessageTemplate("Hello, {foo}!", { foo: 123 }),
["Hello, ", 123, "!"],
);
assertEquals(
parseMessageTemplate("Hello, {{foo}}!", { foo: 123 }),
["Hello, {foo}!"],
);
assertEquals(
parseMessageTemplate("Hello, {bar}!", { foo: 123 }),
["Hello, ", undefined, "!"],
Expand All @@ -447,6 +455,10 @@ Deno.test("parseMessageTemplate()", () => {
parseMessageTemplate("Hello, {foo}, {bar}", { foo: 123, bar: 456 }),
["Hello, ", 123, ", ", 456, ""],
);
assertEquals(
parseMessageTemplate("Hello, {{world!", { foo: 123 }),
["Hello, {world!"],
);
});

Deno.test("renderMessage()", () => {
Expand Down
37 changes: 25 additions & 12 deletions logtape/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,6 @@ export class LoggerImpl implements Logger {
*/
const metaLogger = LoggerImpl.getLogger(["logtape", "meta"]);

const MESSAGE_TEMPLATE_PATTERN = /\{([^}]*)\}/g;

/**
* Parse a message template into a message template array and a values array.
* @param template The message template.
Expand All @@ -613,19 +611,34 @@ export function parseMessageTemplate(
template: string,
properties: Record<string, unknown>,
): readonly unknown[] {
let lastPos = 0;
const message: unknown[] = [];
while (true) {
const match = MESSAGE_TEMPLATE_PATTERN.exec(template);
if (match == null) {
message.push(template.substring(lastPos));
break;
let part = "";
for (let i = 0; i < template.length; i++) {
const char = template.charAt(i);
const nextChar = template.charAt(i + 1);

if (char == "{" && nextChar == "{") {
// Escaped { character
part = part + char;
i++;
} else if (char == "}" && nextChar == "}") {
// Escaped } character
part = part + char;
i++;
} else if (char == "{") {
// Start of a placeholder
message.push(part);
part = "";
} else if (char == "}") {
// End of a placeholder
message.push(properties[part]);
part = "";
} else {
// Default case
part = part + char;
}
message.push(template.substring(lastPos, match.index));
const key = match[1];
message.push(properties[key]);
lastPos = match.index + match[0].length;
}
message.push(part);
return message;
}

Expand Down