
Research
/Security News
Contagious Interview Campaign Escalates With 67 Malicious npm Packages and New Malware Loader
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
stringbuilder
Advanced tools
An string builder for Node.js
npm install stringbuilder
var StringBuilder = require('stringbuilder')
// create an StringBuilder();
var sb = new StringBuilder( {newline:'\r\n'} );
var sbInside = new StringBuilder( {newline:'\r\n'} );
// you can configure all new intances of the StringBuilder
// as default win32='\r\n' others='\n'
// StringBuilder.configure({newline:'\r\n'});
sb.append('some text') // append text
sb.append('{0:YYYY}', new Date()) // append text formatted
sb.appendLine('some text') // append a new line
sb.appendLine('{0:$ 0.1}', 50.1044) // append a new line formatted
ab.append( sbInside ); // append other StringBuilder into sb
// you can append text into `sbInside` after that
sbInside.append('another text');
extends the String
var StringBuilder = require('stringbuilder')
StringBuilder.extend('string');
'The current year is {0:YYYY}'.format(new Date(2013)); // The current year is 2013
or the same
String.format('The current year is {0:YYYY}', new Date(2013)); // The current year is 2013
var StringBuilder = require('stringbuilder')
, fs = require('fs');
// Make an markdown file of the beatles
var data = {
band: "The Beatles"
, formed: new Date(1960)
, discography: [
{ name: 'Sentimental Journey', created: new Date(1970), price: (Math.random()*10)+1 }
, { name: 'Beaucoups of Blues', created: new Date(1970), price: (Math.random()*10)+1 }
, { name: 'Ringo', created: new Date(1973), price: (Math.random()*10)+1 }
, { name: 'Goodnight Vienna', created: new Date(1974), price: (Math.random()*10)+1 }
, { name: 'Ringo\'s Rotogravure', created: new Date(1976), price: (Math.random()*10)+1 }
, { name: 'Ringo the 4th', created: new Date(1977), price: (Math.random()*10)+1 }
]
};
var main = new StringBuilder()
, discography = new StringBuilder();
// extend de String object
StringBuilder.extend('string');
var filename = './{0}.md'.format(data.band);
var stream = fs.createWriteStream( filename );
var namesRegex = /(John|Paul|George|Ringo)\s(Lennon|McCartney|Harrison|Starr)/g
main
.appendLine('{0}', data.band)
.appendLine()
.append('{0} were an English rock band formed in Liverpool in {1:YYYY}.', data.band, data.formed)
.append('They became the most commercially successful and critically ')
.append('acclaimed act in the rock music era. The group\'s best-known ')
.appendLine('lineup consisted of John Lennon, Paul McCartney, George Harrison, and Ringo Starr.')
.replace(namesRegex, '[$1 $2](http://en.wikipedia.org/wiki/$1_$2)') // replace
.appendLine()
.appendLine('### Discography')
.appendLine()
.append(discography) // append an StringBuilder
.appendLine()
.appendLine('### Influences') // then write more text
.appendLine()
.append('Their earliest influences include ')
.appendLine('Elvis Presley, Carl Perkins, Little Richard and Chuck Berry ...')
.appendLine()
.appendLine('### Genres')
.appendLine('')
.append('Originating as a skiffle group, the Beatles quickly embraced 1950s ')
.append('rock and roll, and their repertoire ultimately expanded to include')
.appendLine('a broad variety of pop music ...')
.insert('## ', 0) // Insert text
;
// append into the discography stringbuilder
data.discography.forEach(function(disk){
discography.appendLine(' - {0} in {1:YYYY} *{2:$ 0,0.00 } release price*', disk.name, disk.created, disk.price);
});
var filename = './{0}.md'.format(data.band);
var stream = fs.createWriteStream( filename, 'utf-8' );
main.pipe(stream);
main.flush();
output
The Beatles were an English rock band formed in Liverpool in 1969They became the most commercially successful and critically acclaimed act in the rock music era. The group's best-known lineup consisted of John Lennon, Paul McCartney, George Harrison, and Ringo Starr.
Their earliest influences include Elvis Presley, Carl Perkins, Little Richard and Chuck Berry ...
Originating as a skiffle group, the Beatles quickly embraced 1950s rock and roll, and their repertoire ultimately expanded to include a broad variety of pop music ...
NOTE: The idea of this example is show you how create parts of one output string separately and then combine all of them, if you need makes html can use a template engine like handlebars, this is only an example
var html = new StringBuilder()
, head = new StringBuilder({ newline: '\r\n\t' }) // add a tab at the end
, body = new StringBuilder({ newline: '\r\n\t' }) // add a tab at the end
;
var initTag, endTag, tag, attr, lorem;
initTag = '<{0}>';
endTag = '</{0}>';
tag = '{0}{{1}}{1}'.format(initTag, endTag);
attr = '{0}="{1}"';
lorem = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
// make the body
body.append('\t')
.appendLine(tag.format('h1', 'StringBuilder example'))
.append(tag.format('p', lorem));
// make the head
head.append('\t')
.appendLine('<meta {0}>', attr.format('charset', 'UTF-8'))
.appendLine('<meta {0} {1}>', attr.format('http-equiv', 'X-UA-Compatible'), attr.format('content', 'IE=edge'))
.append(tag.format('title', 'Generate html with StringBuilder'));
// make the html
html.appendLine('<!doctype html>')
.appendLine('<html {0}>', attr.format('lang', 'en'))
// head
.appendLine(initTag.format('head'))
.append(head)
.appendLine()
.appendLine(endTag.format('head'))
// body
.appendLine(initTag.format('body'))
.append(body)
.appendLine()
.appendLine(endTag.format('body'))
.append(endTag.format('html'));
output
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Generate html with StringBuilder</title>
</head>
<body>
<h1>StringBuilder example</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</body>
</html>
To apply formats please see moment and numeral
var sb = new StringBuilder();
sb.append('{0:format}');
dates examples
sb.append('{0:L}', new date()); // 04/29/2013
sb.append('{0:LL}', new date()); // April 29 2013
sb.append('{0:LLL}', new date()); // April 29 2013 9:13 AM
sb.append('{0:LLLL}', new date()); // Monday, April 29 2013 9:13 AM
numbers examples
sb.append('{0:$0,0.00}', 1000.234); // $0,000.23
sb.append('{0:0%}', 1); // 100%
sb.append('{0:0b}', 100); // 100B
sb.append('{0:(0,0.0000)}', -10000); // (10,000.0000)
Please see async
This is the way that the StringBuilder makes the output string.
waterfall
|-parallel
|-sb.append(format, ...args)
|-sb.append(string)
|-sb.appendLine(format, ...args)
|-sb.appendLine(string)
|-sb.insert(...)
|-sb.replace(...)
|-parallel
|-sb.append(format, ...args)
|-sb.append(StringBuilder())
|-sb.appendLine(string)
|-sb.insert(...)
|-parallel
|-sb.append(format, ...args)
(The MIT License)
Copyright (c) 2012-2013 Delmo Carrozzo <dcardev@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
An String Builder for Node.js
We found that stringbuilder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.