mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 19:04:46 +00:00
Compare commits
2 Commits
d7363704d6
...
feat/templ
| Author | SHA1 | Date | |
|---|---|---|---|
| 7d111fcacd | |||
| c29cefdaac |
35
packages/stdlib/src/collections/get/index.ts
Normal file
35
packages/stdlib/src/collections/get/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { type Collection, type Path } from '../../types';
|
||||
|
||||
export type ExtractFromObject<O extends Record<PropertyKey, unknown>, K> =
|
||||
K extends keyof O
|
||||
? O[K]
|
||||
: K extends keyof NonNullable<O>
|
||||
? NonNullable<O>[K]
|
||||
: never;
|
||||
|
||||
export 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;
|
||||
|
||||
export type ExtractFromCollection<O, K> =
|
||||
K extends []
|
||||
? O
|
||||
: K extends [infer Key, ...infer Rest]
|
||||
? O extends Record<PropertyKey, unknown>
|
||||
? ExtractFromCollection<ExtractFromObject<O, Key>, Rest>
|
||||
: O extends readonly any[]
|
||||
? ExtractFromCollection<ExtractFromArray<O, Key>, Rest>
|
||||
: never
|
||||
: never;
|
||||
|
||||
type Get<O, K> = ExtractFromCollection<O, Path<K>>;
|
||||
|
||||
|
||||
export function get<O extends Collection, K extends string>(obj: O, path: K) {
|
||||
return path.split('.').reduce((acc, key) => (acc as any)?.[key], obj) as Get<O, K> | undefined;
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
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
|
||||
: string;
|
||||
|
||||
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 +1 @@
|
||||
export * from './getByPath';
|
||||
export * from './get';
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, expectTypeOf, it } from "vitest";
|
||||
import { describe, expectTypeOf, it } from 'vitest';
|
||||
import type { ClearPlaceholder, ExtractPlaceholders } from "./index";
|
||||
|
||||
describe('template', () => {
|
||||
describe.skip('template', () => {
|
||||
describe('ClearPlaceholder', () => {
|
||||
it('ignores strings without braces', () => {
|
||||
type actual = ClearPlaceholder<'name'>;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { templateObject } from '.';
|
||||
|
||||
describe.todo('templateObject', () => {
|
||||
describe.skip('templateObject', () => {
|
||||
it('replace template placeholders with corresponding values from args', () => {
|
||||
const template = 'Hello, {names.0}!';
|
||||
const args = { names: ['John'] };
|
||||
|
||||
@@ -1,25 +1,20 @@
|
||||
import { getByPath, type Generate } from '../../collections';
|
||||
import { isFunction } from '../../types';
|
||||
import { get } from '../../collections';
|
||||
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 StringPrimitive = string | number | bigint | null | undefined;
|
||||
export type TemplateValue = Stringable | string;
|
||||
|
||||
/**
|
||||
* 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 };
|
||||
export type TemplateFallback = string | ((key: string) => string);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@@ -27,13 +22,14 @@ const TEMPLATE_PLACEHOLDER = /{([^{}]+)}/gm;
|
||||
* @example
|
||||
* type Base = 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> =
|
||||
T extends `${string}{${infer Template}`
|
||||
export type ClearPlaceholder<In extends string> =
|
||||
In extends `${string}{${infer Template}`
|
||||
? ClearPlaceholder<Template>
|
||||
: T extends `${infer Template}}${string}`
|
||||
: In extends `${infer Template}}${string}`
|
||||
? ClearPlaceholder<Template>
|
||||
: T;
|
||||
: Trim<In>;
|
||||
|
||||
/**
|
||||
* Extracts all placeholders from a template string.
|
||||
@@ -41,25 +37,37 @@ export type ClearPlaceholder<T extends string> =
|
||||
* @example
|
||||
* type Base = ExtractPlaceholders<'Hello {user.name}, {user.addresses.0.street}'>; // 'user.name' | 'user.addresses.0.street'
|
||||
*/
|
||||
export type ExtractPlaceholders<T extends string> =
|
||||
T extends `${infer Before}}${infer After}`
|
||||
export type ExtractPlaceholders<In extends string> =
|
||||
In 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 {
|
||||
/**
|
||||
* 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) => {
|
||||
const value = getByPath(args, key) as string;
|
||||
const value = get(args, key)?.toString();
|
||||
return value !== undefined ? value : (isFunction(fallback) ? fallback(key) : '');
|
||||
});
|
||||
}
|
||||
|
||||
// templateObject('Hello {user.name}, your address {user.addresses.0.street}', {
|
||||
// user: {
|
||||
// name: 'John',
|
||||
// addresses: [
|
||||
// { city: 'New York', street: '5th Avenue' },
|
||||
// ],
|
||||
// },
|
||||
// });
|
||||
templateObject('Hello {user.name}, your address {user.addresses.0.city}', {
|
||||
user: {
|
||||
name: 'John',
|
||||
addresses: [
|
||||
{ city: 'Kolpa' },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
71
packages/stdlib/src/types/ts/collections.test-d.ts
Normal file
71
packages/stdlib/src/types/ts/collections.test-d.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { describe, expectTypeOf, it } from 'vitest';
|
||||
import type { Path, PathToType } from './collections';
|
||||
|
||||
describe('collections', () => {
|
||||
describe('Path', () => {
|
||||
it('parse simple object path', () => {
|
||||
type actual = Path<'user.name'>;
|
||||
type expected = ['user', 'name'];
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('parse simple array path', () => {
|
||||
type actual = Path<'user.0'>;
|
||||
type expected = ['user', '0'];
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('parse complex object path', () => {
|
||||
type actual = Path<'user.addresses.0.street'>;
|
||||
type expected = ['user', 'addresses', '0', 'street'];
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('parse double dot path', () => {
|
||||
type actual = Path<'user..name'>;
|
||||
type expected = ['user', '', 'name'];
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
});
|
||||
|
||||
describe('PathToType', () => {
|
||||
it('convert simple object path', () => {
|
||||
type actual = PathToType<['user', 'name']>;
|
||||
type expected = { user: { name: unknown } };
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('convert simple array path', () => {
|
||||
type actual = PathToType<['user', '0']>;
|
||||
type expected = { user: unknown[] };
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('convert complex object path', () => {
|
||||
type actual = PathToType<['user', 'addresses', '0', 'street']>;
|
||||
type expected = { user: { addresses: { street: unknown }[] } };
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('convert double dot path', () => {
|
||||
type actual = PathToType<['user', '', 'name']>;
|
||||
type expected = { user: { '': { name: unknown } } };
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('convert to custom target', () => {
|
||||
type actual = PathToType<['user', 'name'], string>;
|
||||
type expected = { user: { name: string } };
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
});
|
||||
});
|
||||
28
packages/stdlib/src/types/ts/collections.ts
Normal file
28
packages/stdlib/src/types/ts/collections.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* A collection definition
|
||||
*/
|
||||
export type Collection = Record<PropertyKey, any> | any[];
|
||||
|
||||
/**
|
||||
* Parse a collection path string into an array of keys
|
||||
*/
|
||||
export type Path<T> =
|
||||
T extends `${infer Key}.${infer Rest}`
|
||||
? [Key, ...Path<Rest>]
|
||||
: T extends `${infer Key}`
|
||||
? [Key]
|
||||
: [];
|
||||
|
||||
/**
|
||||
* Convert a collection path array into a Target type
|
||||
*/
|
||||
export type PathToType<T extends string[], Target = unknown> =
|
||||
T extends [infer Head, ...infer Rest]
|
||||
? Head extends `${number}`
|
||||
? Rest extends string[]
|
||||
? PathToType<Rest, Target>[]
|
||||
: never
|
||||
: Rest extends string[]
|
||||
? { [K in Head & string]: PathToType<Rest, Target> }
|
||||
: never
|
||||
: Target;
|
||||
@@ -1,4 +1,6 @@
|
||||
export * from './array';
|
||||
export * from './collections';
|
||||
export * from './function';
|
||||
export * from './promise';
|
||||
export * from './string';
|
||||
export * from './union';
|
||||
|
||||
@@ -1,7 +1,18 @@
|
||||
import { describe, expectTypeOf, it } from 'vitest';
|
||||
import type { HasSpaces, Trim } from './string';
|
||||
import type { HasSpaces, Trim, Stringable } from './string';
|
||||
|
||||
describe('string', () => {
|
||||
describe('Stringable', () => {
|
||||
it('should be a string', () => {
|
||||
expectTypeOf(Number(1)).toMatchTypeOf<Stringable>();
|
||||
expectTypeOf(String(1)).toMatchTypeOf<Stringable>();
|
||||
expectTypeOf(Symbol()).toMatchTypeOf<Stringable>();
|
||||
expectTypeOf(new Array(1)).toMatchTypeOf<Stringable>();
|
||||
expectTypeOf(new Object()).toMatchTypeOf<Stringable>();
|
||||
expectTypeOf(new Date()).toMatchTypeOf<Stringable>();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Trim', () => {
|
||||
it('remove leading and trailing spaces from a string', () => {
|
||||
type actual = Trim<' hello '>;
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
/**
|
||||
* Stringable type
|
||||
*/
|
||||
export interface Stringable {
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim leading and trailing whitespace from `S`
|
||||
*/
|
||||
|
||||
31
packages/stdlib/src/types/ts/union.test-d.ts
Normal file
31
packages/stdlib/src/types/ts/union.test-d.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { describe, expectTypeOf, it } from 'vitest';
|
||||
import type { UnionToIntersection } from './union';
|
||||
|
||||
describe('union', () => {
|
||||
describe('UnionToIntersection', () => {
|
||||
it('convert a union type to an intersection type', () => {
|
||||
type actual = UnionToIntersection<{ a: string } | { b: number }>;
|
||||
type expected = { a: string } & { b: number };
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('convert a union type to an intersection type with more than two types', () => {
|
||||
type actual = UnionToIntersection<{ a: string } | { b: number } | { c: boolean }>;
|
||||
type expected = { a: string } & { b: number } & { c: boolean };
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('no change when the input is already an intersection type', () => {
|
||||
type actual = UnionToIntersection<{ a: string } & { b: number }>;
|
||||
type expected = { a: string } & { b: number };
|
||||
|
||||
expectTypeOf<actual>().toEqualTypeOf<expected>();
|
||||
});
|
||||
|
||||
it('never when union not possible', () => {
|
||||
expectTypeOf<UnionToIntersection<string | number>>().toEqualTypeOf<never>();
|
||||
});
|
||||
});
|
||||
});
|
||||
10
packages/stdlib/src/types/ts/union.ts
Normal file
10
packages/stdlib/src/types/ts/union.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Convert a union type to an intersection type
|
||||
*/
|
||||
export type UnionToIntersection<Union> = (
|
||||
Union extends unknown
|
||||
? (distributedUnion: Union) => void
|
||||
: never
|
||||
) extends ((mergedIntersection: infer Intersection) => void)
|
||||
? Intersection & Union
|
||||
: never;
|
||||
Reference in New Issue
Block a user