Coder Social home page Coder Social logo

educastellano / template-format Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 184 KB

Simple string formatting with support for nested data

JavaScript 100.00%
string string-manipulation string-formatter template template-string string-formatting string-format

template-format's Introduction

template-format

Simple string formatting with support for nested data.

Install

npm install template-format

Syntax

const formattedText = format(text, data, options={})
Parameter Type Default Description
text string Text to format
object object The object or array containing the data to be used for formatting.
options object {} Optional Extra options, read below.
options.regex regex /{(.*?)}/g Optional Alternative regex for different format syntaxes (i.e.: Hello {{name}}!). Must include the global match modifier (g).
options.skipUndefined object false Optional Skips formatting parameters which are missing in the object, keeping the original text. Otherwise they'll be replaced by an empty string.
options.spreadToken string $n Optional Token used on arrays to indicate that the following attributes have to be applied in each element (See example below).
options.spreadSeparator string , Optional String used on arrays to separate of the formatting in each element.

Usage

import format from 'template-format'

With objects

format('Hello {name}, happy {age} bday!', { name: 'Bob', age: 32 })
// 'Hello Bob, happy 32 bday!'

With arrays

format('Hello {0}, happy {1} bday!', ['Bob', 32])
// 'Hello Bob, happy 32 bday!'

With nested data

format('Hello {bob.name}, happy {bob.age} bday! I call you at {bob.contact.phone}', {
        bob: {
            name: 'Bob', 
            age: 32, 
            contact: {
                phone: '978090909'
            }
        }
    })
// 'Hello Bob, happy 32 bday! I call you at 978090909'

Spread arrays

format('Hello {people.$n.name}!', { people: [{ name: 'Bob' }, { name: 'Mary' }] })
// 'Hello Bob,Mary!'

Other Options

  • Skip undefined attributes:
format('Hello {name}, happy {age} bday!', { name: 'Bob' })
// 'Hello Bob, happy bday!'
format('Hello {name}, happy {age} bday!', { name: 'Bob' }, { skipUndefined: true })
// 'Hello Bob, happy {age} bday!'
  • Using a different format syntax:
format('Hello {{name}}, happy {{age}} bday!', { name: 'Bob', age: 32}, { regex: /{{(.*?)}}/g })
// 'Hello Bob, happy 32 bday!'
  • Custom spreading
format('Hello {people.$$.name}!', { people: [{ name: 'Bob' }, { name: 'Mary' }] }, { spreadToken: '$$', spreadSeparator: ', ' })
// 'Hello Bob, Mary!'

Changelog

  • 1.2.0

    • Support to spread formatting on arrays
    • Customizable spreading with spreadToken and spreadSeparator
  • 1.1.0

    • Support to skip undefined attributes
    • Support for alternative format syntaxes
  • 1.0.0

    • Initial release ๐ŸŽ‰

License

ISC License

template-format's People

Contributors

dependabot[bot] avatar educastellano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ajmariduena

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.