项目作者: SeparateRecords

项目描述 :
Simple & flexible variable substitution for Deno. Perfect for environment variables.
高级语言: TypeScript
项目地址: git://github.com/SeparateRecords/deno_substitute.git
创建时间: 2021-03-21T11:44:26Z
项目社区:https://github.com/SeparateRecords/deno_substitute

开源协议:

下载


deno_substitute

Simple, familiar, fast-enough variable substitution.

Perfect for environment variables, flexible enough for whatever.


Usage

substitute substitutes variables using a function.

  1. import { substitute } from "https://deno.land/x/substitute@v0.2.1/mod.ts";
  2. substitute("Welcome $HOME", Deno.env.get);
  3. // => "Welcome /home/user"
  4. substitute("Welcome $HOME", (s) => s.toLowerCase());
  5. // => "Welcome home"

It’s strict by default but designed to recover from errors gracefully.

  1. substitute("Hello, ${}", (x) => x);
  2. // NoNameError: Expected variable name at column 8
  3. substitute("Hello, ${}", (x) => x, { strict: false });
  4. // => "Hello, ${}"

Bash and Windows CMD-style variables are both accepted, and can be disabled
individually.

  1. substitute("$HOME ${HOME}", (x) => x.toLowerCase());
  2. // => "home home"
  3. substitute("$HOME ${HOME}", (x) => x.toLowerCase(), { dollar: false });
  4. // => "$HOME ${HOME}"
  5. substitute("%USERPROFILE%", (x) => x.toLowerCase());
  6. // => "userprofile"
  7. substitute("%USERPROFILE%", (x) => x.toLowerCase(), { percent: false });
  8. // => "%USERPROFILE%"

You can use a literal $ or % by doubling it, like "$$" or "%%".


Contributing

I’ll endeavour to keep it working but I probably won’t be adding anything new
myself. It does what I need it to. I’m more than happy to accept and help out
with PRs.

Fair warning, though…

This code is extremely messy. The only goal when writing it was to make it work,
not to make it maintainable. Any negative side effects as a result of exposure
should be dealt with by a medical professional.