0.2.0 • Published 5 years ago

sf v0.2.0

Weekly downloads
3,306
License
MIT
Repository
github
Last release
5 years ago

sf

Build Status

String formatting library for node.js.

Installation

$ npm install sf

Quick Examples

var sf = require("sf");

sf("{who} has a {what}", { who: 'dog', what: 'tail' });
// returns: dog has a tail

sf("{0} has a {1}", 'dog', 'tail');
// returns: dog has a tail

sf("{0:#,##0.00}", 2123.1);
// returns: 2,123.10

sf("{0,15:#,##0.00}", 2123.1);
// returns:        2,123.10

sf("{a.b}", { a: { b: 'test' }});
// returns: test

sf("{a.z.c}", { a: { b: 'test' }});
// throws an error

sf("{a.?z.?c}", { a: { b: 'test' }});
// returns:

sf("{a[0]}", { a: [ 'foo', 'bar' ]});
// returns: foo

sf("{a[-1]}", { a: [ 'foo', 'bar' ]});
// returns: bar

sf.log("{who} has a {what}", { who: 'dog', what: 'tail' });
// outputs to standard out: dog has a tail

sf("{0:^d 'Days,' h:mm:ss.fff}", new sf.TimeSpan(8173818181));
// returns: 94 Days, 14:30:18.181

Format Specifiers

The format is similar to C#'s string.format. The text inside the curly braces is {indexOrName,alignment}. If alignment is positive the text is right aligned. If alignment is negative it will be left aligned.

Object

SpecifierName
jsonJSON.stringify
inspectutil.inspect

Numbers

SpecifierNameExampleOutput
0Zero placeholder{0:00.0000}02.1200
#Digit placeholder{0:#,###}1,234
xLowercase hex{0:x4}01fc
XUppercase hex{0:X4}01FC

Dates

SpecifierNameExample
sdShort date10/12/2002
DLong dateDecember 10, 2002
tShort time10:11 PM
TLong time10:11:29 PM
fdtFull date & timeDecember 10, 2002 10:11 PM
FFull date & time (long)December 10, 2002 10:11:29 PM
gDefault date & time10/12/2002 10:11 PM
GDefault date & time (long)10/12/2002 10:11:29 PM
mdMonth day patternDecember 10
rRFC1123 date stringTue, 10 Dec 2002 22:11:29 +0500
sSortable date string2002-12-10T22:11:29
dDate single digit1
ddDate leading zero01
dddShort day nameMon
ddddLong day nameMonday
fFraction of second (1 digit)1
ffFraction of second (2 digit)24
fffFraction of second (3 digit)345
hHour 12-hour format 1 digit5
hhHour 12-hour format 2 digits05
HHour 24-hour format 1 digit5
HHHour 24-hour format 2 digits05
mmMinutes 2 digits23
MMonth single digit2
MMMonth leading zero02
MMMMonth short nameFeb
MMMMMonth long nameFebruary
ssSeconds 2 digits54
ttAM/PMAM
yyYear 2 digits12
yyyyYear 4 digits2012
zzTime zone offset05
+zzTime zone offset leading ++05
zzzTime zone offset full05:00
zzzzTime zone offset full0500
+zzzzTime zone offset full leading ++0500

sf.TimeSpan

SpecifierNameExample
yYears2
MMonths6
dDays8
hHours10
mMinutes15
sSeconds5
fFraction of Seconds9

If you prefix the specifier with '^' you will get the total number of that value. For example '^s' will output the total number of seconds in the time span. Where as 's' will only output the number of seconds in a minute.

If you repeat characters the value will be prefixed with zeros.

sf.indent(str, options)

Helper function to word wrap and indent a string.

Arguments

  • str - The string to indent and wrap.
  • options
  • prefix - The prefix to appear at the beginning of each new line.
  • wordwrap - The maximum length of each line.

Helper Functions