curly-bracket-parser

    1.0.2 • Public • Published

    curly-bracket-parser

    Gem GitHub release (latest by date) Gem 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!

    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).

    It is a port my ruby gem curly_bracket_parser.

    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. (curly-bracket-parser.js and curly-bracket-parser.min.js)
    • The default version comes without any predefined default filters, so you can only use your custom filters. But you can also use the default version and embed the original LuckyCase to the document. 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.bundle.js and curly-bracket-parser.bundle.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>

    Optionally you then should add the source file to your build pipeline, if you are using webpack, brunch or any other packager.

    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"

    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"

    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.

    Install

    npm i curly-bracket-parser

    DownloadsWeekly Downloads

    63

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    301 kB

    Total Files

    23

    Last publish

    Collaborators

    • magynhard