Socket
Socket
Sign inDemoInstall

string.format

Package Overview
Dependencies
2
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    string.format

String prototype method -> 'Hello {name}!'.format({name:'Homer'});


Version published
Weekly downloads
59
increased by1.72%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

String.format

license Build Status npm version Github release npm downloads

Languages:[EN,ES]

Add to JS String.prototype .format() method which accepts as parameter an object or an array with values to be substituted in the string, based on a pattern.

String.format uses JSON.decycled for show Objects as String without circular references errors and some related problems.

For Date object format you can use Date.format

npm

require('string.format');
var text = "Hello {name} {lastname}!";
var params = {
  name:'Homer',
  lastname:'Simpson'
};
console.log("01 -- " + text.format(params));
//>> 01 -- Hello Homer Simpson!
var text = "Hello {user.name} {user.lastname}! {msg}";
var params = {
  user:{
    name:'Homer',
    lastname:'Simpson'
  },
  msg:'How are you?'
};
console.log("02 -- " + text.format(params));
//>>  02 -- Hello Homer Simpson! How are you?
var text = "Option one:{0},two:{1},three:{2}.";
var params = ['start','stop','restart'];
console.log("03 -- " + text.format(params));
//>> 03 -- Option one:start,two:stop,three:restart.
var text = "Your tags: {tags}, and your config:{config}.";
var params = {
  tags:['yellow','astronaut','donuts','ouch!'],
  config:{
    skin:'yellow',
    hair:false,
    intellectual:undefined
  }
};
console.log("04 -- " + text.format(params));
//>>  04 -- Your tags: ["yellow","astronaut","donuts","ouch!"], and your config:{"skin":"yellow","hair":false}.

##Example

Licence

MIT


String.format (es)

Añade a String.prototype de JS el método .format() que acepta como parámetro un objeto o un array con valores que serán sustituidos en el string en base a un patrón de sustitución.

String.format utiliza JSON.decycled para monstar objetos como strings sin errores de referencia circular y otros problemas relacionados.

Para ampliar las funciones de String.format en el caso de objetos Date puede resultarte util Date.format

npm

Licencia

MIT

Keywords

FAQs

Last updated on 24 May 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc