From 204b37c2597357075228aaafe86db00f086930e2 Mon Sep 17 00:00:00 2001 From: Marais Rossouw Date: Thu, 23 May 2024 12:33:54 +1000 Subject: [PATCH] :construction: WIP --- src/util.test.ts | 15 +++++++++++++++ src/util.ts | 4 +--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/util.test.ts b/src/util.test.ts index f4ed094..1bda186 100644 --- a/src/util.test.ts +++ b/src/util.test.ts @@ -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'); +}); diff --git a/src/util.ts b/src/util.ts index 8460a18..aa29e83 100644 --- a/src/util.ts +++ b/src/util.ts @@ -7,9 +7,7 @@ export type Props = T extends `${string}{${infer S}}${infer Rest}` // @internal export type Dict = Record; -// 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 = {}; export function interpolate>>( message: T,