npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

string_format

v0.0.6

Published

String.prototype.format

Downloads

20

Readme

string_format

npm version badge Build Status License

A small JavaScript library for formatting strings. It's inspired by and modelled on Python's str.format().

When format is invoked on a string, placeholders within the string are replaced with values determined by the arguments provided. A placeholder is a sequence of characters beginning with { and ending with }.

Install

npm install string_format

Synopsys

format(template, $0, $1, …, $N)

Examples

For more examples see test page

Usage

require('string_format');

General

Placeholders may contain numbers which refer to positional arguments:

'{0}, you have {1} unread message{2}'.format('Holly', 2, 's');
// 'Holly, you have 2 unread messages'

Unmatched placeholders produce no output:

'{0}, you have {1} unread message{2}'.format('Steve', 1);
// 'Steve, you have 1 unread messageundefined'

A format string may reference a positional argument multiple times:

'{0} x {0} x {0} = {1}'.format(3, 3 * 3 *3);
// '3 x 3 x 3 = 27'

Positional arguments may be referenced implicitly:

'{}, you have {} unread message'.format('Steve', 1);
// 'Steve, you have 1 unread message'

A format string must not contain both implicit and explicit references:

'My name is {} {}. Do you like the name {0}?'.format('Lemony', 'Snicket');
// ValueError: cannot switch from implicit to explicit numbering

{{ and }} in format strings produce { and }:

'{{}} creates an empty {} in {}'.format('dictionary', 'Python');
// '{} creates an empty dictionary in Python'

Dot notation may be used to reference object properties:

var bobby = {
   first_name: 'Bobby', 
   last_name: 'Fischer'
};

var garry = {
   first_name: 'Garry',
   last_name: 'Kasparov'
};

'{0.first_name} {0.last_name} vs. {1.first_name} {1.last_name}'.format(bobby, garry)
// 'Bobby Fischer vs. Garry Kasparov'

When referencing the first positional argument, 0. may be omitted:

var repo = {
   owner    : 'pypy', 
   slug     : 'pypy', 
   followers: [1, 2, 3]
};

'{owner}/{slug} has {followers.length} followers'.format(repo);
// 'pypy/pypy has 3 followers'

If the referenced property is a method, it is invoked and the result is used as the replacement string:

var me = {
   name: 'David',
   dob: new Date
};

'{name} was born in {dob.getFullYear}'.format(me);
// 'David was born in 2015'

'{pop}'.format(['one', 'two', 'three']);
// three

Arrays

'{0[1]}'.format([0, 1]); 
// 1

'{0["0"]}'.format([1])); 
// 1

'{0[1]} {1[1]}'.format([0, 1], [0, 2]));
// 1 2


'{0[1][1][1]}'.format([0, [0, [2, [3]]]]);
// 3

'{0[1]()}'.format([0, function () {
    return 1;
}])); 
// 1

'{0[1](1)[1][1]}'.format([0, function (value) {
    return [0, [0, value]];
}]);
// 1

'{0[1](1)[1][1]}:{1[1][1][1]}:{2}:{3.method[1]}'.format([0, function (value) {
   return [0, [0, value]];
}], 
[0, [0, [2, [3]]]], 2, {
   method: [0, [1]]
});
// 1:3:2:1

Escaping

'{{'.format(null); 
// {


'{{}}'.format(null);
// {}

'{{{0}}}'.format(123);
// {123}

Functions

'{0}'.format(function () {
   return 1;
});
// 1

{0()}'.format(function () {
   return 1;
});
// 1

'{0()}{1()}'.format(
   function () {
       return 1;
   },
   function () {
       return 1;
   });
// 11

'{0(1)}'.format(function (value) {
   return value;
});
// 1

'{0()()}'.format(function () {
   return function () {
       return 1;
   };
});
// 1

'{0(1)(2)[1][1]}:{1}'.format(function (a) {
   return function (b) {
       return [0, [1, a + b]];
   };
}, 1);

// 3:1

Methods

'{0.method[1]}'.format({ method: [0, 1] });
// 1

'{method(1)}'.format({
   method: function (x) {
       return x;
   }
});
// 1

'{object.method(1, 2)}'.format({
   object: {
       method: function (a, b) {
           return a + b;
       }
   }
});

// 3

'{0.method[0]()}'.format({
   method: [function () {
       return 1;
   }]
});
// 1

'{0.method[0](1)}'.format({
   method: [function (value) {
       return value;
   }]
});
// 1

'{object.method[1]} + {object2.method[1]} = 2'.format({
    object : {
        method: [0, 1]
    },
    object2: {
        method: [0, 1]
    }
}, 2);
// 1 + 1 = 2


'{0.method(1, 2)}'.format({
   method: function (a, b) {
       return a + b;
   }
});
// 3


'{0.method(1, 2)(3, 4)}'.format({
   method: function (a, b) {
       return function (c, d) {
           return a + b + c + d;
       };
   }
});

// 10

'{0.method(0)(1)[0][1](1, 2)}'.format({
   method: function (a) {
       return function (b) {
           return [[0, function (c, d) {
               return a + b + c + d;
           }]];
       };
   }
});

// 4

Undefined values

'{0}'.format(null);
// null

'{0}'.format(undefined);
// undefined

Tests

npm test

Info

  • http://docs.python.org/2/library/string.html
  • https://github.com/deleted/string-format