Small snippets manager with builtin support for variables. Variables are defined as an array of function so they can be anything, arg, getSelectedText etc. Snippet can also be a simple string if it has no variables

Install Snippets

// Menu: Snippets
// Description: Snippets collection
// Author: Jakub Olek
// Twitter: @JakubOlek
// Shortcut: opt -
const { setSelectedText } = await kit("text");
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
const snippetMap = {
useState: {
args: [() => arg("variable name"), () => arg("variable value")],
template: (name, value) =>
`const [${name}, set${capitalizeFirstLetter(name)}] = useState(${value})`,
},
name: "Jakub Olek",
date: new Date().toLocaleDateString("en-GB", {
year: "numeric",
month: "long",
day: "numeric",
}),
test: {
args: [() => arg("should")],
template: (testName) => `test("should ${testName}", function() {
})`,
},
component: {
args: [() => arg("component name")],
template: (componentName) => `function ${capitalizeFirstLetter(
componentName
)}() {
return
}`,
},
};
const snippetName = await arg("Snippet", Object.keys(snippetMap));
let result = snippetMap[snippetName];
const { args, template } = result;
if (template) {
const variables = [];
if (args) {
for (let i = 0; i < args.length; i++) {
const variable = args[i];
if (typeof variable !== "string") {
variables.push(await variable());
}
}
}
setSelectedText(template(...variables));
} else {
setSelectedText(result);
}