Skip to content

Instantly share code, notes, and snippets.

@crashmax-dev
Created February 17, 2026 17:31
Show Gist options
  • Select an option

  • Save crashmax-dev/64f2875895e0f0024f3b03a7c8bcbe01 to your computer and use it in GitHub Desktop.

Select an option

Save crashmax-dev/64f2875895e0f0024f3b03a7c8bcbe01 to your computer and use it in GitHub Desktop.
const STORE_KEYS = new Map<string, Set<string>>()
/**
* Создаёт новую группу ключей и возвращает функцию для добавления ключей в неё.
*
* @param group Название группы
* @returns Функция, добавляющая уникальные ключи в группу
* @throws Ошибка, если группа уже существует
*/
export function createStoreKey<Group extends string>(group: Group) {
if (STORE_KEYS.has(group)) {
throw new Error(`Группа "${group}" уже существует`)
}
const groupKeys = new Set<string>()
STORE_KEYS.set(group, groupKeys)
return <Key extends string>(key: Key): `${Group}/${Key}` => {
if (groupKeys.has(key)) {
throw new Error(`Ключ "${key}" уже существует в группе "${group}"`)
}
groupKeys.add(key)
return `${group}/${key}`
}
}
/**
* Выводит все ключи в виде дерева.
*/
export function printStoreKeysTree() {
for (const [group, keys] of STORE_KEYS.entries()) {
console.group(`📁 ${group}`)
const keyArray = Array.from(keys)
for (const key of keyArray) {
console.log(`🔑 ${key}`)
}
console.groupEnd()
}
}
if (import.meta.env.DEV) {
Object.defineProperty(window, '__PRINT_STORE_KEYS_TREE__', {
get() {
printStoreKeysTree()
},
configurable: true,
enumerable: false,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment