1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 02:44:45 +00:00

refactor(core/stdlib): update test descriptions and improve placeholder handling

This commit is contained in:
2025-05-20 19:20:26 +07:00
parent 049b5b351a
commit 6d68246d16
3 changed files with 38 additions and 30 deletions

View File

@@ -1,7 +1,7 @@
import { describe, expectTypeOf, it } from "vitest"; import { describe, expectTypeOf, it } from 'vitest';
import type { ClearPlaceholder, ExtractPlaceholders } from "./index"; import type { ClearPlaceholder, ExtractPlaceholders } from './index';
describe('template', () => { describe.skip('template', () => {
describe('ClearPlaceholder', () => { describe('ClearPlaceholder', () => {
it('ignores strings without braces', () => { it('ignores strings without braces', () => {
type actual = ClearPlaceholder<'name'>; type actual = ClearPlaceholder<'name'>;

View File

@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { templateObject } from '.'; import { templateObject } from '.';
describe.todo('templateObject', () => { describe.skip('templateObject', () => {
it('replace template placeholders with corresponding values from args', () => { it('replace template placeholders with corresponding values from args', () => {
const template = 'Hello, {names.0}!'; const template = 'Hello, {names.0}!';
const args = { names: ['John'] }; const args = { names: ['John'] };

View File

@@ -1,25 +1,20 @@
import { getByPath, type Generate } from '../../collections'; import { get } from '../../collections';
import { isFunction } from '../../types'; import { isFunction, type Path, type PathToType, type Stringable, type Trim, type UnionToIntersection } from '../../types';
/** /**
* Type of a value that will be used to replace a placeholder in a template. * Type of a value that will be used to replace a placeholder in a template.
*/ */
type StringPrimitive = string | number | bigint | null | undefined; export type TemplateValue = Stringable | string;
/** /**
* Type of a fallback value when a template key is not found. * Type of a fallback value when a template key is not found.
*/ */
type TemplateFallback = string | ((key: string) => string); export type TemplateFallback = string | ((key: string) => string);
/**
* Type of an object that will be used to replace placeholders in a template.
*/
type TemplateArgsObject = StringPrimitive[] | { [key: string]: TemplateArgsObject | StringPrimitive };
/** /**
* Type of a template string with placeholders. * Type of a template string with placeholders.
*/ */
const TEMPLATE_PLACEHOLDER = /{([^{}]+)}/gm; const TEMPLATE_PLACEHOLDER = /\{\s*([^{}]+?)\s*\}/gm;
/** /**
* Removes the placeholder syntax from a template string. * Removes the placeholder syntax from a template string.
@@ -27,13 +22,14 @@ const TEMPLATE_PLACEHOLDER = /{([^{}]+)}/gm;
* @example * @example
* type Base = ClearPlaceholder<'{user.name}'>; // 'user.name' * type Base = ClearPlaceholder<'{user.name}'>; // 'user.name'
* type Unbalanced = ClearPlaceholder<'{user.name'>; // 'user.name' * type Unbalanced = ClearPlaceholder<'{user.name'>; // 'user.name'
* type Spaces = ClearPlaceholder<'{ user.name }'>; // 'user.name'
*/ */
export type ClearPlaceholder<T extends string> = export type ClearPlaceholder<In extends string> =
T extends `${string}{${infer Template}` In extends `${string}{${infer Template}`
? ClearPlaceholder<Template> ? ClearPlaceholder<Template>
: T extends `${infer Template}}${string}` : In extends `${infer Template}}${string}`
? ClearPlaceholder<Template> ? ClearPlaceholder<Template>
: T; : Trim<In>;
/** /**
* Extracts all placeholders from a template string. * Extracts all placeholders from a template string.
@@ -41,25 +37,37 @@ export type ClearPlaceholder<T extends string> =
* @example * @example
* type Base = ExtractPlaceholders<'Hello {user.name}, {user.addresses.0.street}'>; // 'user.name' | 'user.addresses.0.street' * type Base = ExtractPlaceholders<'Hello {user.name}, {user.addresses.0.street}'>; // 'user.name' | 'user.addresses.0.street'
*/ */
export type ExtractPlaceholders<T extends string> = export type ExtractPlaceholders<In extends string> =
T extends `${infer Before}}${infer After}` In extends `${infer Before}}${infer After}`
? Before extends `${string}{${infer Placeholder}` ? Before extends `${string}{${infer Placeholder}`
? ClearPlaceholder<Placeholder> | ExtractPlaceholders<After> ? ClearPlaceholder<Placeholder> | ExtractPlaceholders<After>
: ExtractPlaceholders<After> : ExtractPlaceholders<After>
: never; : never;
export function templateObject<T extends string, A extends Generate<ExtractPlaceholders<T>>>(template: T, args: A, fallback?: TemplateFallback): string { /**
* Generates a type for a template string with placeholders.
*
* @example
* type Base = GenerateTypes<'Hello {user.name}, your address {user.addresses.0.street}'>; // { user: { name: string; addresses: { 0: { street: string; }; }; }; }
* type WithTarget = GenerateTypes<'Hello {user.age}', number>; // { user: { age: number; }; }
*/
export type GenerateTypes<T extends string, Target = string> = UnionToIntersection<PathToType<Path<T>, Target>>;
export function templateObject<
T extends string,
A extends GenerateTypes<ExtractPlaceholders<T>, TemplateValue>
>(template: T, args: A, fallback?: TemplateFallback) {
return template.replace(TEMPLATE_PLACEHOLDER, (_, key) => { return template.replace(TEMPLATE_PLACEHOLDER, (_, key) => {
const value = getByPath(args, key) as string; const value = get(args, key)?.toString();
return value !== undefined ? value : (isFunction(fallback) ? fallback(key) : ''); return value !== undefined ? value : (isFunction(fallback) ? fallback(key) : '');
}); });
} }
// templateObject('Hello {user.name}, your address {user.addresses.0.street}', { templateObject('Hello {user.name}, your address {user.addresses.0.city}', {
// user: { user: {
// name: 'John', name: 'John',
// addresses: [ addresses: [
// { city: 'New York', street: '5th Avenue' }, { city: 'Kolpa' },
// ], ],
// }, },
// }); });