Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sf

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sf - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

test/indentTest.js

232

lib/stringFormat.js

@@ -10,2 +10,16 @@ 'use strict';

function padLeft (str, char, totalWidth) {
while (str.length < totalWidth) {
str = char + str;
}
return str;
}
function padRight (str, char, totalWidth) {
while (str.length < totalWidth) {
str = str + char;
}
return str;
}
function formatNumber (num, format) {

@@ -23,6 +37,3 @@ format = format || "0";

var width = parseInt(hex[2]);
var neededPadding = width - str.length;
for (var i = 0; i < neededPadding; i++) {
str = '0' + str;
}
str = padLeft(str, '0', width);
return str;

@@ -110,7 +121,3 @@ }

function formatDate (date, format) {
if (!format || format === '') {
return date;
}
function formatDatePart (date, format) {
switch (format) {

@@ -135,4 +142,4 @@ case 'sd': // Short date - 10/12/2002

return sf("{0:MMMM} {0:dd}", date);
case 'r': // RFC1123 date string - Tue, 10 Dec 2002 22:11:29 GMT
return sf("{0:ddd}, {0:dd} {0:MMM} {0:yyyy} {0:HH}:{0:mm}:{0:ss} {0:+zz}", date);
case 'r': // RFC1123 date string - Tue, 10 Dec 2002 22:11:29 +0500
return sf("{0:ddd}, {0:dd} {0:MMM} {0:yyyy} {0:HH}:{0:mm}:{0:ss} {0:+zzzz}", date);
case 's': // Sortable date string - 2002-12-10T22:11:29

@@ -187,2 +194,8 @@ return sf("{0:yyyy}-{0:MM}-{0:dd}:{0:HH}:{0:mm}:{0:ss}", date);

return sf("{0:00}:{1:00}", wholeTimezoneOffset, date.getTimezoneOffset() - (wholeTimezoneOffset * 60));
case 'zzzz':
var wholeTimezoneOffset = Math.floor(date.getTimezoneOffset() / 60);
return sf("{0:00}{1:00}", wholeTimezoneOffset, date.getTimezoneOffset() - (wholeTimezoneOffset * 60));
case '+zzzz':
var wholeTimezoneOffset = Math.floor(date.getTimezoneOffset() / 60);
return sf("{0:+00}{1:00}", wholeTimezoneOffset, date.getTimezoneOffset() - (wholeTimezoneOffset * 60));
default:

@@ -193,2 +206,62 @@ throw new Error("unhandled date format '" + format + "'");

function formatDate (date, format) {
if (!format || format === '') {
return date;
}
var result = '';
var part = '';
for (var i = 0; i < format.length; i++) {
var char = format[i];
if ((char >= 'a' && char <= 'z')
|| (char >= 'A' && char <= 'Z')
|| char == '+') {
part += char;
continue;
}
if (part.length > 0) {
result += formatDatePart(date, part);
part = '';
}
if (char === "'") {
i++;
for (; i < format.length && format[i] !== "'"; i++) {
result += format[i];
}
continue;
}
result += char;
}
if (part.length > 0) {
result += formatDatePart(date, part);
}
return result;
}
function formatObjectIndent (obj) {
if (!obj) {
return 'null';
}
if (typeof(obj) != 'object') {
return obj.toString();
}
var results = '';
var keys = Object.keys(obj).sort();
for (var i = 0; i < keys.length; i++) {
results += keys[i] + ":";
var val = obj[keys[i]];
if (!val) {
results += '\n';
}
else if (typeof(val) === 'string' || typeof(val) === 'number' || typeof(val) === 'boolean' || val instanceof Date) {
results += " " + val + "\n";
} else {
var str = formatObjectIndent(val);
results += "\n" + sf.indent(str, {}) + "\n";
}
}
results = results.replace(/\s+$/, '');
return results;
}
function formatObject (obj, format) {

@@ -207,2 +280,6 @@ if (!format || format === '') {

if (format == 'indent') {
return formatObjectIndent(obj);
}
throw new Error("unhandled format: " + format);

@@ -225,10 +302,6 @@ }

str = str || '';
var pad = '';
for (var i = 0; i < Math.abs(val) - str.length; i++) {
pad += ' ';
}
if (val < 0) {
return str + pad;
return padRight(str, ' ', Math.abs(val));
} else if (val > 0) {
return pad + str;
return padLeft(str, ' ', val);
}

@@ -369,18 +442,135 @@ return str;

function getStringLength (str, options) {
options.tabWidth = options.tabWidth || 4;
var tabStr = padLeft('', ' ', options.tabWidth);
str = str.replace(/\t/g, tabStr);
return str.length;
}
function isSpace (char) {
if (char.match(/\s/)) {
return true;
}
return false;
}
function isSplitable (char) {
if (isSpace(char)) {
return true;
}
if (char == '(' || char == ')' || char == '.' || char == ',' || char == '?' || char == '!') {
return false;
}
if (char >= 'a' && char <= 'z') {
return false;
}
if (char >= 'A' && char <= 'Z') {
return false;
}
if (char >= '0' && char <= '9') {
return false;
}
return true;
}
function findLastSplit (line, i) {
if (i >= line.length) {
i = line.length - 1;
}
while (i > 0 && !isSplitable(line[i])) {
i--;
}
return i;
}
function findNextSplit (line, i) {
while (i < line.length && !isSplitable(line[i])) {
i++;
}
return i;
}
function wordWrapLine (line, options) {
if (line.length === 0) {
return '';
}
options._prefixLength = options._prefixLength || getStringLength(options.prefix, options);
if (line.length + options._prefixLength < options.wordwrap) {
return options.prefix + line;
}
var i = options._prefixLength + options.wordwrap;
var lastSplit = findLastSplit(line, i);
var nextSplit = findNextSplit(line, i);
var rest;
if (lastSplit === 0 && (nextSplit + options._prefixLength > options.wordwrap)) {
rest = wordWrapLine(line.substr(options.wordwrap - options._prefixLength), options);
if (rest.length > 0) {
rest = '\n' + rest;
}
return options.prefix + line.substr(0, options.wordwrap - options._prefixLength) + rest;
}
rest = wordWrapLine(line.substr(lastSplit + 1), options);
if (rest.length > 0) {
rest = '\n' + rest;
}
if (!isSpace(line[lastSplit])) {
lastSplit++;
}
return options.prefix + line.substr(0, lastSplit) + rest;
}
sf.indent = function (str, options) {
options = options || {};
options.prefix = 'prefix' in options ? options.prefix : ' ';
options.tabWidth = options.tabWidth || 4;
options._prefixLength = getStringLength(options.prefix, options);
if (options.wordwrap) {
var results = '';
var lines = str.split('\n');
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line === '') {
results += options.prefix + line + '\n';
} else {
line = wordWrapLine(line, options) + '\n';
}
results += line;
}
if (results.length > 0) {
results = results.substr(0, results.length - 1);
}
return results;
}
str = str.replace(/\n/g, '\n' + options.prefix);
str = options.prefix + str;
return str;
};
sf.log = function () {
console.log(sf.apply(this, arguments));
}
};
sf.info = function () {
console.info(sf.apply(this, arguments));
}
};
sf.warn = function () {
console.warn(sf.apply(this, arguments));
}
};
sf.error = function () {
console.error(sf.apply(this, arguments));
}
};
module.exports = sf;

2

package.json

@@ -7,3 +7,3 @@ {

"homepage": "https://github.com/nearinfinity/node-sf",
"version": "0.1.1",
"version": "0.1.2",
"engines": { "node" : ">=0.6.0" },

@@ -10,0 +10,0 @@ "maintainers": [

@@ -70,38 +70,40 @@ # sf

| Specifier | Name | Example |
|-----------|------------------------------|--------------------------------|
| sd | Short date | 10/12/2002 |
| D | Long date | December 10, 2002 |
| t | Short time | 10:11 PM |
| T | Long time | 10:11:29 PM |
| fdt | Full date & time | December 10, 2002 10:11 PM |
| F | Full date & time (long) | December 10, 2002 10:11:29 PM |
| g | Default date & time | 10/12/2002 10:11 PM |
| G | Default date & time (long) | 10/12/2002 10:11:29 PM |
| md | Month day pattern | December 10 |
| r | RFC1123 date string | Tue, 10 Dec 2002 22:11:29 GMT |
| s | Sortable date string | 2002-12-10T22:11:29 |
| d | Date single digit | 1 |
| dd | Date leading zero | 01 |
| ddd | Short day name | Mon |
| dddd | Long day name | Monday |
| f | Fraction of second (1 digit) | 1 |
| ff | Fraction of second (2 digit) | 24 |
| fff | Fraction of second (3 digit) | 345 |
| h | Hour 12-hour format 1 digit | 5 |
| hh | Hour 12-hour format 2 digits | 05 |
| H | Hour 24-hour format 1 digit | 5 |
| HH | Hour 24-hour format 2 digits | 05 |
| mm | Minutes 2 digits | 23 |
| M | Month single digit | 2 |
| MM | Month leading zero | 02 |
| MMM | Month short name | Feb |
| MMMM | Month long name | February |
| ss | Seconds 2 digits | 54 |
| tt | AM/PM | AM |
| yy | Year 2 digits | 12 |
| yyyy | Year 4 digits | 2012 |
| zz | Time zone offset | 05 |
| +zz | Time zone offset leading + | +05 |
| zzz | Time zone offset full | 05:00 |
| Specifier | Name | Example |
|-----------|---------------------------------|---------------------------------|
| sd | Short date | 10/12/2002 |
| D | Long date | December 10, 2002 |
| t | Short time | 10:11 PM |
| T | Long time | 10:11:29 PM |
| fdt | Full date & time | December 10, 2002 10:11 PM |
| F | Full date & time (long) | December 10, 2002 10:11:29 PM |
| g | Default date & time | 10/12/2002 10:11 PM |
| G | Default date & time (long) | 10/12/2002 10:11:29 PM |
| md | Month day pattern | December 10 |
| r | RFC1123 date string | Tue, 10 Dec 2002 22:11:29 +0500 |
| s | Sortable date string | 2002-12-10T22:11:29 |
| d | Date single digit | 1 |
| dd | Date leading zero | 01 |
| ddd | Short day name | Mon |
| dddd | Long day name | Monday |
| f | Fraction of second (1 digit) | 1 |
| ff | Fraction of second (2 digit) | 24 |
| fff | Fraction of second (3 digit) | 345 |
| h | Hour 12-hour format 1 digit | 5 |
| hh | Hour 12-hour format 2 digits | 05 |
| H | Hour 24-hour format 1 digit | 5 |
| HH | Hour 24-hour format 2 digits | 05 |
| mm | Minutes 2 digits | 23 |
| M | Month single digit | 2 |
| MM | Month leading zero | 02 |
| MMM | Month short name | Feb |
| MMMM | Month long name | February |
| ss | Seconds 2 digits | 54 |
| tt | AM/PM | AM |
| yy | Year 2 digits | 12 |
| yyyy | Year 4 digits | 2012 |
| zz | Time zone offset | 05 |
| +zz | Time zone offset leading + | +05 |
| zzz | Time zone offset full | 05:00 |
| zzzz | Time zone offset full | 0500 |
| +zzzz | Time zone offset full leading + | +0500 |

@@ -108,0 +110,0 @@ ## Helper Functions

@@ -90,2 +90,8 @@ var sf = require('../');

'align with date format': function (test) {
var result = sf("a{0,20:yyyy-MM-dd'T'HH:mm:ss}b", new Date(2012, 1, 2, 3, 4, 5));
test.equals(result, 'a 2012-02-02T03:04:05b');
test.done();
},
'number format (comma)': function (test) {

@@ -231,3 +237,3 @@ var result = sf("a{0:#,###}b", 5000);

var result = sf("a{0:r}b", new Date(2012, 1, 2, 3, 4, 5, 6));
test.equals(result, 'aThu, 02 Feb 2012 03:04:05 +05b');
test.equals(result, 'aThu, 02 Feb 2012 03:04:05 +0500b');
test.done();

@@ -374,2 +380,14 @@ },

'date format (rfc 822/1123 timezone)': function (test) {
var result = sf("a{0:+zzzz}b", new Date(2012, 1, 2, 3, 4, 5, 6));
test.equals(result, 'a+0500b');
test.done();
},
'date format (whole timezone no colon)': function (test) {
var result = sf("a{0:zzzz}b", new Date(2012, 1, 2, 3, 4, 5, 6));
test.equals(result, 'a0500b');
test.done();
},
'date format (bad format)': function (test) {

@@ -405,2 +423,8 @@ try {

'object with indent': function (test) {
var result = sf("{0:indent}", { a: 1, b: 'test', c: { d: { e: { f: 'down' }}}, d: true, e: new Date(2012, 1, 2, 3, 4, 5) });
test.equals(result, 'a: 1\nb: test\nc:\n d:\n e:\n f: down\nd: true\ne: Thu Feb 02 2012 03:04:05 GMT-0500 (EST)');
test.done();
},
'unterminated sub': function (test) {

@@ -407,0 +431,0 @@ try {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc