Giter Site home page Giter Site logo

b4dnewz / string-template Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 659 KB

A utility to format strings with many options

License: MIT License

TypeScript 98.64% JavaScript 1.36%
string string-formatter string-manipulation string-template utility string-utility template replacement

string-template's Introduction

string-template

A utility to format strings with many options

NPM version Build Status Coverage percentage

Getting started

Download it using your favourite package manager.

npm i @b4dnewz/string-template

Then import it in your code and have fun formatting strings.

import template from "@b4dnewz/string-template";

const str = `
    That's an awfully hot {item},
    did {person} kill himself?
    Probably {answer}.
`;

template(str, {
    item: "coffee pot",
    person: "Jeffrey Epstein",
    answer: "not"
});

By default the function will fail if unknown properties are found, but can be disabled using options.

Using custom pattern

The string template function will use this pattern {%s}, for finding variables in the string, where %s will be replaced with the built-in word match pattern which is not customizable.

If you want to use a different template pattern you can add the pattern option as third parameter.

const str = `Using a <:adjective:> pattern`;

const out = template(str, {
    adjective: "different"
}, {
    pattern: "<:%s:>"
});

console.log(out);

Options

pattern

Type: string

The string template pattern to use for finding replacements, it must use the string replacer %s to know where to put the word pattern.

template(input, replacements, {
    pattern: ":%s:"
});

ignoreErrors

Type: boolean

When true unknown properties in the string will not raise an error and will simply ignored.

template(input, replacements, {
    ignoreErrors: true
});

License

This package is released under MIT License © Filippo Conti

string-template's People

Watchers

 avatar  avatar

string-template's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.