UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

1 lines 2.42 kB
{"version":3,"file":"nesting.mjs","names":[],"sources":["../../../../src/transpiler/nesting/nesting.ts"],"sourcesContent":["import type { DictionaryRegistryContent } from '@intlayer/types';\nimport {\n type DictionaryKeys,\n formatNodeType,\n NodeType,\n type TypedNodeModel,\n} from '@intlayer/types';\nimport type { DeepTransformContent } from '../../interpreter';\n\n/**\n * Recursively builds dot-notation strings for all valid paths in T.\n * Example:\n * type X = { a: { b: { c: string }}, d: number };\n * DotPath<X> = \"a\" | \"a.b\" | \"a.b.c\" | \"d\"\n */\nexport type DotPath<T> = T extends object\n ? {\n [K in keyof T & (string | number)]: T[K] extends object\n ? // Either just K, or K + '.' + deeper path\n `${K}` | `${K}.${DotPath<T[K]>}`\n : `${K}`;\n }[keyof T & (string | number)]\n : never;\n\ntype DeepReplace<T, From, To> = T extends From\n ? To\n : T extends object\n ? { [K in keyof T]: DeepReplace<T[K], From, To> }\n : T;\n\n/** Build all valid dot-notation strings for a dictionary entry. */\nexport type ValidDotPathsFor<K extends DictionaryKeys> = DotPath<\n DeepReplace<\n DeepTransformContent<DictionaryRegistryContent<K>>,\n // Replace ReactElement type with string\n {\n type: any;\n props: any;\n key: any;\n },\n string\n >\n>;\n\nexport type NestedContentState<K extends DictionaryKeys> = {\n dictionaryKey: K;\n\n /**\n * Path must match existing keys in DictionaryRegistryContent<K>.\n * Can be either:\n * - \"dot.dot.dot\" format\n */\n path?: ValidDotPathsFor<K>;\n};\n\nexport type NestedContent<K extends DictionaryKeys = never> = TypedNodeModel<\n NodeType.Nested,\n NestedContentState<K>\n>;\n\n/**\n * Function intended to be used to build intlayer dictionaries.\n *\n * Allow to extract the content of another dictionary and nest it in the current dictionary.\n *\n * Usage:\n *\n * ```ts\n * nest(\"dictionaryKey\");\n * nest(\"dictionaryKey\", \"path.to.content\");\n * ```\n *\n * The order of the keys will define the priority of the content.\n *\n */\nconst nesting = <K extends DictionaryKeys>(\n dictionaryKey: K,\n path?: ValidDotPathsFor<K>\n): NestedContent<K> =>\n formatNodeType(NodeType.Nested, {\n dictionaryKey,\n path,\n });\n\nexport { nesting as nest };\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2EA,MAAM,WACJ,eACA,SAEA,eAAe,SAAS,QAAQ;CAC9B;CACA;CACD,CAAC"}