Snippets
A general-purpose library for applying configured snippets to an input string, for TypeScript and Rust.
@oiper/snippets (TypeScript) and oiper-snippets (Rust) apply a configured set of snippets
to an input string. A snippet is a set of matchers and the text to substitute when one of them
matches.
The library is independent of any application, input source, or output destination. It has no opinion about where the configuration comes from or what you do with the result — you hand it a parsed configuration and a string, and it returns a string.
Given the same valid configuration and the same input, both libraries return the same output.
Install
npm install @oiper/snippetscargo add oiper-snippetsBoth are MIT licensed, and the source for both lives in github.com/OiPer/oiper-snippets.
Example
import { applySnippets, parseConfig } from '@oiper/snippets'
const config = parseConfig([
{
when: [{ value: 'brb' }, { regex: '\\bbr+b\\b', flags: 'i' }],
body: 'be right back',
},
])
applySnippets('brb, one moment', config)
// 'be right back, one moment'use oiper_snippets::{apply_snippets, parse_config};
let raw = serde_json::json!([
{
"when": [{ "value": "brb" }, { "regex": "\\bbr+b\\b", "flags": "i" }],
"body": "be right back"
}
]);
let config = parse_config(&raw)?;
let output = apply_snippets("brb, one moment", &config);
// "be right back, one moment"Configuration is parsed once and reused. Parsing validates the configuration and compiles the regexes; applying is a cheap single pass over the input.
Where to go next
- Configuration — the config format and its validation rules
- Matching — exactly how input is matched and replaced
- TypeScript API and Rust API
Non-goals
The library deliberately does not provide configuration storage, JSON or JSONC file parsing, CRUD operations, UI behaviour, recursive expansion, capture interpolation, templates, or automatic word boundaries. Those belong to whatever is calling it.