Skip to content

Commit

Permalink
馃毀 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed May 23, 2024
1 parent 8ce28e8 commit 204b37c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ Deno.test('with a lot of replacements', () => {
let s = lib.interpolate('hello {name} {name} {name} {name}', { name: 'world' });
assertEquals(s, 'hello world world world world');
});

Deno.test('should allow spaces in the keys', () => {
let s = lib.interpolate('hello {first name}', { 'first name': 'world' });
assertEquals(s, 'hello world');
});

Deno.test('should allow multiple keys', () => {
let s = lib.interpolate('{phrase} {name} {phrase}', { phrase: 'hello', name: 'world' });
assertEquals(s, 'hello world hello');
});

Deno.test('should allow zero-width keys to mean empty string', () => {
let s = lib.interpolate('{name} {}', { name: 'world', '': 'test' });
assertEquals(s, 'world test');
});
4 changes: 1 addition & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export type Props<T> = T extends `${string}{${infer S}}${infer Rest}`
// @internal
export type Dict<T = unknown> = Record<string, T>;

// credit: https://github.com/lukeed/tempura/blob/e1c6a38da65876751b54a26d8a6371d6cf587459/src/%24utils.js#L2
let CURLY = /{{?\s*([\s\S]*?)\s*}}?/g;

let CURLY = /{([\s\S]*?)}/g;
let cache: Record<string, any> = {};
export function interpolate<const T extends string, O extends Pretty<Props<T>>>(
message: T,
Expand Down

0 comments on commit 204b37c

Please sign in to comment.