Skip to content

Commit

Permalink
Merge pull request #2 from windupbird144/main
Browse files Browse the repository at this point in the history
Escape curly braces
  • Loading branch information
dahlia committed Apr 26, 2024
2 parents 0248f0b + c94ae94 commit ccb2a2e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Version 0.4.0

To be released.

- Curly braces can now be escaped with double curly braces.
[[#1], [#2] by Diyar Oktay]

[#1]: https://github.com/dahlia/logtape/issues/1
[#2]: https://github.com/dahlia/logtape/pull/2


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}!"],
);
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

0 comments on commit ccb2a2e

Please sign in to comment.