npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

curly-bracket-parser

v1.3.5

Published

Simple parser to replace variables inside templates/strings and files for node js and browser.

Downloads

1,021

Readme

curly-bracket-parser

npm package GitHub release (latest by date) downloads License: MIT

Javascript library providing a simple parser to replace curly brackets {{like_this}} inside strings like URLs, texts or even files (node only) easily. Available for node js and browser!

Custom filters

Additional support for build-in filters and custom filters make them more powerful. {{example|my_filter}}

LuckyCase case formats are supported as default filters by node js dependency, in browser optionally if LuckyCase is loaded as well (bundled version).

Variable trees

Support for variable trees, e.g.: {{my.tree.variable}}

Ruby version

It is a port my ruby gem curly_bracket_parser, but got already some additional features.

Contents

Installation

Option 1: node js - yarn

In your project root directory execute the following command:

yarn add curly-bracket-parser

Option 2: node js - npm

In your project root directory execute the following command:

npm install curly-bracket-parser

Option 3: Browser

There are two versions, default and bundled.

  • The bundled version has LuckyCase and its cases as default filters included. The dependencies Typifier and RubyNice are included as well. (curly-bracket-parser.bundle.js and curly-bracket-parser.bundle.min.js)
  • The default version comes without any predefined default filters, so you can only use your custom filters. But you need then to add Typifier as isolated dependency as well. You can optionally embed the original LuckyCase to the document as well. CurlyBracketParser will recognize, if LuckyCase is available and then provide them as default filters. So if you don't need the LuckyCase case filters, you get a much smaller file size without the bundle. (curly-bracket-parser.js and curly-bracket-parser.min.js)

Download the curly-bracket-parser.min.js or curly-bracket-parser.bundle.min.js at the release page and put it in an appropriate folder, e.g. js/lib and reference it with an script tag in your project:

<script type="text/javascript" src="js/lib/curly-bracket-parser.min.js"></script>

If you are using a packager, you should add the source file to your build pipeline.

Usage examples

You can either parse variables inside strings or even directly in files.

Basic

    const url = "https://my-domain.com/items/{{item_id}}";
    const final_url = CurlyBracketParser.parse(url, { item_id: 123 });
    // => "https://my-domain.com/items/123"

Nested variables inside variables are supported as well:

    const tmpl = "This is my template with {{my_nested_variable}}";
    const my_nested_variable = "my {{nested}} variable"; 
    const parsed_tmpl = CurlyBracketParser.parse(tmpl, { my_nested_variable: my_nested_variable, nested: 'pizza'});
    // => "This is my template with my pizza variable"

Filters

You can register your own filters, or if you use the bundled version, all cases of LuckyCase.

    const url = "https://my-domain.com/catalog/{{item_name|snake_case}}";
    const final_url = CurlyBracketParser.parse(url, { item_name: 'MegaSuperItem' });
    // => "https://my-domain.com/catalog/mega_super_item"

For a list of built-in filters in the bundled version visit LuckyCase.

Define your custom filter

    CurlyBracketParser.registerFilter('7times', (string) => {
        return string + string + string + string + string + string + string;
    })

    const text = "Paul went out and screamed: A{{scream|7times}}h";
    const final_text = CurlyBracketParser.parse(text, { scream: 'a' });
    // => "Paul went out and screamed: Aaaaaaaah"

Value variables

For special cases you can directly define or set variables inside the template - usually it does only make sense, if you combine them with custom filters.

You can either use quotes to define a string or numbers (integer or floating point) directly.

Empty values are possible as well. They are equal to a empty string.

    const tmpl = `This is a {{'string'|pascal_case}} and today is {{"today"|date_filter}}. Peter is {{'1990-10-05'|iso_date_age}} years old. His girlfriends name is {{girl|pascal_case}} and she is {{16|double_number}} years old. This article has been written at {{|date_now_formatted}}`;
    const parsed = CurlyBracketParser.parse(tmpl, { girl: "anna" });
    // => "This is a String and today is 2022-06-27. Peter is 32 years old. His girlfriends name is Anna and she is 32 years old. This article has been written at 6/28/2022, 12:46:40 PM."

Files

test.html

<h1>{{title|sentence_case}}</h1>
    const parsed_file = CurlyBracketParser.parseFile('./test.html', { title: 'WelcomeAtHome' });
    // => "<h1>Welcome at home</h1>"

Use .parseFileWrite instead to write the parsed string directly into the file!

As browsers are not allowed to write to to file system, .parseFileWrite is only available on node. Running .parseFile in browser fires a HTTP GET request (ajax) with the given path to read the file.

Default variables

You can define default variables, which will be replaced automatically without passing them by parameters, but can be overwritten with parameters.

Because of providing anonymous functions, your variables can dynamically depend on other states (e.g. current date).

    CurlyBracketParser.registerDefaultVar('version', () => {
        return '1.0.2';  
    });

    const text = "You are running version {{version}}"
    CurlyBracketParser.parse(text);
    // => "You are running version 1.0.2"
    CurlyBracketParser.parse(text, { version: '0.7.0' });
    // => "You are running version 0.7.0"

Documentation

Check out the jsdoc documentation here.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/magynhard/curly-bracket-parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.