string.format

1.3.1 • Public • Published

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

Package Sidebar

Install

npm i string.format

Weekly Downloads

78

Version

1.3.1

License

MIT

Last publish

Collaborators

  • bifuer
  • lilxelo