Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/clo4/deno_substitute

Simple & flexible variable substitution for Deno. Perfect for environment variables.
https://github.com/clo4/deno_substitute

deno environment-variables envsubst substitution

Last synced: 15 days ago
JSON representation

Simple & flexible variable substitution for Deno. Perfect for environment variables.

Awesome Lists containing this project

README

        

# deno_substitute

**Simple, familiar, fast-enough variable substitution.**

Perfect for environment variables, flexible enough for whatever.


## Usage

`substitute` substitutes variables using a function.

```typescript
import { substitute } from "https://deno.land/x/[email protected]/mod.ts";

substitute("Welcome $HOME", Deno.env.get);
// => "Welcome /home/user"
substitute("Welcome $HOME", (s) => s.toLowerCase());
// => "Welcome home"
```

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

```typescript
substitute("Hello, ${}", (x) => x);
// NoNameError: Expected variable name at column 8
substitute("Hello, ${}", (x) => x, { strict: false });
// => "Hello, ${}"
```

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

```typescript
substitute("$HOME ${HOME}", (x) => x.toLowerCase());
// => "home home"
substitute("$HOME ${HOME}", (x) => x.toLowerCase(), { dollar: false });
// => "$HOME ${HOME}"

substitute("%USERPROFILE%", (x) => x.toLowerCase());
// => "userprofile"
substitute("%USERPROFILE%", (x) => x.toLowerCase(), { percent: false });
// => "%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.