mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
feat(packages/stdlib): start getByPath and template tools
This commit is contained in:
84
packages/stdlib/src/arrays/getByPath/index.ts
Normal file
84
packages/stdlib/src/arrays/getByPath/index.ts
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
type Exist<T> = T extends undefined | null ? never : T;
|
||||||
|
|
||||||
|
type ExtractFromObject<O extends Record<PropertyKey, unknown>, K> =
|
||||||
|
K extends keyof O
|
||||||
|
? O[K]
|
||||||
|
: K extends keyof Exist<O>
|
||||||
|
? Exist<O>[K]
|
||||||
|
: never;
|
||||||
|
|
||||||
|
type ExtractFromArray<A extends readonly any[], K> = any[] extends A
|
||||||
|
? A extends readonly (infer T)[]
|
||||||
|
? T | undefined
|
||||||
|
: undefined
|
||||||
|
: K extends keyof A
|
||||||
|
? A[K]
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
type GetWithArray<O, K> = K extends []
|
||||||
|
? O
|
||||||
|
: K extends [infer Key, ...infer Rest]
|
||||||
|
? O extends Record<PropertyKey, unknown>
|
||||||
|
? GetWithArray<ExtractFromObject<O, Key>, Rest>
|
||||||
|
: O extends readonly any[]
|
||||||
|
? GetWithArray<ExtractFromArray<O, Key>, Rest>
|
||||||
|
: never
|
||||||
|
: never;
|
||||||
|
|
||||||
|
type Path<T> = T extends `${infer Key}.${infer Rest}`
|
||||||
|
? [Key, ...Path<Rest>]
|
||||||
|
: T extends `${infer Key}`
|
||||||
|
? [Key]
|
||||||
|
: [];
|
||||||
|
|
||||||
|
// Type that generate a type of a value by a path;
|
||||||
|
// e.g. ['a', 'b', 'c'] => { a: { b: { c: PropertyKey } } }
|
||||||
|
// e.g. ['a', 'b', 'c', 'd'] => { a: { b: { c: { d: PropertyKey } } } }
|
||||||
|
// e.g. ['a'] => { a: PropertyKey }
|
||||||
|
// e.g. ['a', '0'], => { a: [PropertyKey] }
|
||||||
|
// e.g. ['a', '0', 'b'] => { a: [{ b: PropertyKey }] }
|
||||||
|
// e.g. ['a', '0', 'b', '0'] => { a: [{ b: [PropertyKey] }] }
|
||||||
|
// e/g/ ['0', 'a'] => [{ a: PropertyKey }]
|
||||||
|
//
|
||||||
|
// Input: ['a', 'b', 'c'], constrain: PropertyKey
|
||||||
|
// Output: { a: { b: { c: PropertyKey } } }
|
||||||
|
|
||||||
|
export type UnionToIntersection<Union> = (
|
||||||
|
Union extends unknown
|
||||||
|
? (distributedUnion: Union) => void
|
||||||
|
: never
|
||||||
|
) extends ((mergedIntersection: infer Intersection) => void)
|
||||||
|
? Intersection & Union
|
||||||
|
: never;
|
||||||
|
|
||||||
|
|
||||||
|
type PathToType<T extends string[]> = T extends [infer Head, ...infer Rest]
|
||||||
|
? Head extends string
|
||||||
|
? Head extends `${number}`
|
||||||
|
? Rest extends string[]
|
||||||
|
? PathToType<Rest>[]
|
||||||
|
: never
|
||||||
|
: Rest extends string[]
|
||||||
|
? { [K in Head & string]: PathToType<Rest> }
|
||||||
|
: never
|
||||||
|
: never
|
||||||
|
: unknown;
|
||||||
|
|
||||||
|
export type Generate<T extends string> = UnionToIntersection<PathToType<Path<T>>>;
|
||||||
|
type Get<O, K> = GetWithArray<O, Path<K>>;
|
||||||
|
|
||||||
|
export function getByPath<O, K extends string>(obj: O, path: K): Get<O, K>;
|
||||||
|
export function getByPath(obj: Record<string, unknown>, path: string): unknown {
|
||||||
|
const keys = path.split('.');
|
||||||
|
let currentObj = obj;
|
||||||
|
|
||||||
|
for (const key of keys) {
|
||||||
|
const value = currentObj[key];
|
||||||
|
|
||||||
|
if (value === undefined || value === null) return undefined;
|
||||||
|
|
||||||
|
currentObj = value as Record<string, unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentObj;
|
||||||
|
}
|
||||||
1
packages/stdlib/src/arrays/index.ts
Normal file
1
packages/stdlib/src/arrays/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './getByPath';
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
export * from './levenshtein-distance';
|
export * from './levenshtein-distance';
|
||||||
export * from './trigram-distance';
|
export * from './trigram-distance';
|
||||||
|
export * from './template';
|
||||||
105
packages/stdlib/src/text/template/index.test-d.ts
Normal file
105
packages/stdlib/src/text/template/index.test-d.ts
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
import { describe, expectTypeOf, it } from "vitest";
|
||||||
|
import type { ClearPlaceholder, ExtractPlaceholders } from "./index";
|
||||||
|
|
||||||
|
describe('template', () => {
|
||||||
|
describe('ClearPlaceholder', () => {
|
||||||
|
it('ignores strings without braces', () => {
|
||||||
|
type actual = ClearPlaceholder<'name'>;
|
||||||
|
type expected = 'name';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes all balanced braces from placeholders', () => {
|
||||||
|
type actual1 = ClearPlaceholder<'{name}'>;
|
||||||
|
type actual2 = ClearPlaceholder<'{{name}}'>;
|
||||||
|
type actual3 = ClearPlaceholder<'{{{name}}}'>;
|
||||||
|
type expected = 'name';
|
||||||
|
|
||||||
|
expectTypeOf<actual1>().toEqualTypeOf<expected>();
|
||||||
|
expectTypeOf<actual2>().toEqualTypeOf<expected>();
|
||||||
|
expectTypeOf<actual3>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes all unbalanced braces from placeholders', () => {
|
||||||
|
type actual1 = ClearPlaceholder<'{name}}'>;
|
||||||
|
type actual2 = ClearPlaceholder<'{{name}}}'>;
|
||||||
|
type expected = 'name';
|
||||||
|
|
||||||
|
expectTypeOf<actual1>().toEqualTypeOf<expected>();
|
||||||
|
expectTypeOf<actual2>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('ExtractPlaceholders', () => {
|
||||||
|
it('string without placeholders', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello name, how are?'>;
|
||||||
|
type expected = never;
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('string with one idexed placeholder', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello {0}, how are you?'>;
|
||||||
|
type expected = '0';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('string with two indexed placeholders', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello {0}, my name is {1}'>;
|
||||||
|
type expected = '0' | '1';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('string with one key placeholder', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello {name}, how are you?'>;
|
||||||
|
type expected = 'name';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('string with two key placeholders', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello {name}, my name is {managers.0.name}'>;
|
||||||
|
type expected = 'name' | 'managers.0.name';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('string with mixed placeholders', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello {0}, how are you? My name is {1.name}'>;
|
||||||
|
type expected = '0' | '1.name';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('string with nested placeholder and balanced braces', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello {{name}}, how are you?'>;
|
||||||
|
type expected = 'name';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('string with nested placeholder and unbalanced braces', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello {{{name}, how are you?'>;
|
||||||
|
type expected = 'name';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('string with nested placeholders and balanced braces', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello {{{name}{positions}}}, how are you?'>;
|
||||||
|
type expected = 'name' | 'positions';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('string with nested placeholders and unbalanced braces', () => {
|
||||||
|
type actual = ExtractPlaceholders<'Hello {{{name}{positions}, how are you?'>;
|
||||||
|
type expected = 'name' | 'positions';
|
||||||
|
|
||||||
|
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
42
packages/stdlib/src/text/template/index.test.ts
Normal file
42
packages/stdlib/src/text/template/index.test.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { templateObject } from './index';
|
||||||
|
|
||||||
|
describe('templateObject', () => {
|
||||||
|
// it('replace template placeholders with corresponding values from args', () => {
|
||||||
|
// const template = 'Hello, {names.0}!';
|
||||||
|
// const args = { names: ['John'] };
|
||||||
|
// const result = templateObject(template, args);
|
||||||
|
// expect(result).toBe('Hello, John!');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('replace template placeholders with corresponding values from args', () => {
|
||||||
|
// const template = 'Hello, {name}!';
|
||||||
|
// const args = { name: 'John' };
|
||||||
|
// const result = templateObject(template, args);
|
||||||
|
// expect(result).toBe('Hello, John!');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('replace template placeholders with fallback value if corresponding value is undefined', () => {
|
||||||
|
// const template = 'Hello, {name}!';
|
||||||
|
// const args = { age: 25 };
|
||||||
|
// const fallback = 'Guest';
|
||||||
|
// const result = templateObject(template, args, fallback);
|
||||||
|
// expect(result).toBe('Hello, Guest!');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it(' replace template placeholders with fallback value returned by fallback function if corresponding value is undefined', () => {
|
||||||
|
// const template = 'Hello, {name}!';
|
||||||
|
// const args = { age: 25 };
|
||||||
|
// const fallback = (key: string) => `Unknown ${key}`;
|
||||||
|
// const result = templateObject(template, args, fallback);
|
||||||
|
// expect(result).toBe('Hello, Unknown name!');
|
||||||
|
// });
|
||||||
|
|
||||||
|
it('replace template placeholders with nested values from args', () => {
|
||||||
|
const template = 'Hello, {user.name}!';
|
||||||
|
const args = { user: { name: 'John' } };
|
||||||
|
const result = templateObject(template, args);
|
||||||
|
|
||||||
|
expect(result).toBe('Hello, John!');
|
||||||
|
});
|
||||||
|
});
|
||||||
52
packages/stdlib/src/text/template/index.ts
Normal file
52
packages/stdlib/src/text/template/index.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import { getByPath, type Generate } from '../../arrays';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of a value that will be used to replace a placeholder in a template.
|
||||||
|
*/
|
||||||
|
type StringPrimitive = string | number | bigint | null | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of a fallback value when a template key is not found.
|
||||||
|
*/
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
const TEMPLATE_PLACEHOLDER = /{([^{}]+)}/gm;
|
||||||
|
|
||||||
|
export type ClearPlaceholder<T extends string> =
|
||||||
|
T extends `${string}{${infer Template}`
|
||||||
|
? ClearPlaceholder<Template>
|
||||||
|
: T extends `${infer Template}}${string}`
|
||||||
|
? ClearPlaceholder<Template>
|
||||||
|
: T;
|
||||||
|
|
||||||
|
export type ExtractPlaceholders<T extends string> =
|
||||||
|
T extends `${infer Before}}${infer After}`
|
||||||
|
? Before extends `${string}{${infer Placeholder}`
|
||||||
|
? ClearPlaceholder<Placeholder> | ExtractPlaceholders<After>
|
||||||
|
: ExtractPlaceholders<After>
|
||||||
|
: never;
|
||||||
|
|
||||||
|
export function templateObject<T extends string, A extends Generate<ExtractPlaceholders<T>>>(template: T, args: A, fallback?: TemplateFallback): string {
|
||||||
|
return template.replace(TEMPLATE_PLACEHOLDER, (subs, key) => {
|
||||||
|
return getByPath(args, key) as string;
|
||||||
|
// return value !== undefined ? value : (isFunction(fallback) ? fallback(key) : fallback);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
templateObject('Hello {user.name}, your address {user.addresses.0}', {
|
||||||
|
user: {
|
||||||
|
name: 'John Doe',
|
||||||
|
addresses: [
|
||||||
|
{ street: '123 Main St', city: 'Springfield'},
|
||||||
|
{ street: '456 Elm St', city: 'Shelbyville'}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user