Skip to content

belbis/js-string-format

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-string-format

Build Status Coverage Status

extend String class with python-esque format function

introduction

This is a small library that extends the String prototype to allow for python like format. It supports multiple ways for replacement which are described in the usage section

install

from git:

npm install "git+https://github.com/belbis/js-string-format"

node/iojs:

npm install js-string-format

browser:

<script src="/path/to/dist/stringFormat.min.js"></script>

usage

import (node):

require("js-string-format");

use:

// numerical
var ret = 'The {0} {1} fox jumps over the {2} dog.'.format(
  ['quick', 'brown', 'lazy'])

var ret = 'The {0} {1} fox jumps over the {2} dog.'.format(
    'quick', 
    'brown', 
    'lazy'
)

var ret = 'The {0} {1} fox jumps over the {2} dog.'.format(
  {
    1: 'quick',
    2: 'brown',
    3: 'lazy'
  }
)

// object
var ret = 'The quick brown {i} jumps over the lazy {j}'.format(
    {
      'i': 'fox',
       'j': 'dog'
    }
)
console.log(ret) // The quick brown fox jumps over the lazy dog.

tests

run javascript npm test

notes

this library does not sanitize the input other than simple type checking

About

js format string extension inspired by python string format

Resources

Stars

Watchers

Forks

Packages

No packages published