douglascrockford/fulfill
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
master
Could not load branches
Nothing to show
Could not load tags
Nothing to show
{{ refName }}
default
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code
-
Clone
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more about the CLI.
- Open with GitHub Desktop
- Download ZIP
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
4d2ea8a
Git stats
Files
Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
fulfill
Douglas Crockford
2018-08-10
Public Domain
The fulfill JavaScript function is a safer and more internationalizationable
alternative to template string interprepolation. It is safer because it does not
give the template variables access to all of the variables in the function
scope, and it has a default encoder that removes angle brackets. It is more
internationalizationable because the string can come from a source other than a
string literal in the same file. For example, the string could come from a JSON
bundle translation service.
It is packaged as a module.
fulfill(string, values, encoder)
The string can contain symbolic variables in either of two forms:
{path}
{path:encoding}
The path is a name or integer or a series of names or integers separated by
periods that finds a value in the values argument. If all goes well, the
symbolic variable will be replaced with the encoded value. If anything does not
go well, then the symbolic variable is left alone. This makes debugging easier.
It also allows for literal braces in the string without escapement.
The values argument can be an object or array that supplies the values that will
be substituted. It can be a nested data structure.
The values can also be a function that returns the value that should be
substituted.
function values(path, encoding)
The encoder can be a function that returns the value encoded as a safe string.
function encoder(value, path, encoding)
The encoder can also be an object of encoder functions. The property names are
encodings.
The default encoder removes angle brackets, making things safe for HTML.
import fulfill from "./fulfill.js";
const example = fulfill(
"{greeting}, {my.noun:upper}!",
{
greeting: "hello",
my: {noun: "world"}
},
function initial_caps(value) {
return value.slice(0, 1).toUpperCase() + value.slice(1);
}
); // example is "Hello, World!"
About
Safer, more cosmopolitan string interpolation.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published