diff --git a/packages/stdlib/src/arrays/getByPath/index.ts b/packages/stdlib/src/arrays/getByPath/index.ts index 1a1ad7e..6ceea36 100644 --- a/packages/stdlib/src/arrays/getByPath/index.ts +++ b/packages/stdlib/src/arrays/getByPath/index.ts @@ -44,7 +44,7 @@ type Path = T extends `${infer Key}.${infer Rest}` // Output: { a: { b: { c: PropertyKey } } } export type UnionToIntersection = ( - Union extends unknown + Union extends unknown ? (distributedUnion: Union) => void : never ) extends ((mergedIntersection: infer Intersection) => void) @@ -62,7 +62,7 @@ type PathToType = T extends [infer Head, ...infer Rest] ? { [K in Head & string]: PathToType } : never : never - : unknown; + : string; export type Generate = UnionToIntersection>>; type Get = GetWithArray>; diff --git a/packages/stdlib/src/text/template/index.test.ts b/packages/stdlib/src/text/template/index.test.ts index 63f9797..b896be1 100644 --- a/packages/stdlib/src/text/template/index.test.ts +++ b/packages/stdlib/src/text/template/index.test.ts @@ -33,10 +33,16 @@ describe('templateObject', () => { // }); it('replace template placeholders with nested values from args', () => { - const template = 'Hello, {user.name}!'; - const args = { user: { name: 'John' } }; - const result = templateObject(template, args); + const result = templateObject('Hello {{user.name}, your address {user.addresses.0.street}', { + user: { + name: 'John Doe', + addresses: [ + { street: '123 Main St', city: 'Springfield'}, + { street: '456 Elm St', city: 'Shelbyville'} + ] + } + }); - expect(result).toBe('Hello, John!'); + expect(result).toBe('Hello {John Doe, your address 123 Main St'); }); }); \ No newline at end of file diff --git a/packages/stdlib/src/text/template/index.ts b/packages/stdlib/src/text/template/index.ts index 7c6cbee..6418480 100644 --- a/packages/stdlib/src/text/template/index.ts +++ b/packages/stdlib/src/text/template/index.ts @@ -1,4 +1,5 @@ import { getByPath, type Generate } from '../../arrays'; +import { isFunction } from '../../types'; /** * Type of a value that will be used to replace a placeholder in a template. @@ -20,6 +21,13 @@ type TemplateArgsObject = StringPrimitive[] | { [key: string]: TemplateArgsObjec */ const TEMPLATE_PLACEHOLDER = /{([^{}]+)}/gm; +/** + * Removes the placeholder syntax from a template string. + * + * @example + * type Base = ClearPlaceholder<'{user.name}'>; // 'user.name' + * type Unbalanced = ClearPlaceholder<'{user.name'>; // 'user.name' + */ export type ClearPlaceholder = T extends `${string}{${infer Template}` ? ClearPlaceholder