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

ns-string-builder

v1.2.1

Published

**Exercise to create a tool to improve the concatenation of strings.**

Downloads

2

Readme

StringBuilder

Exercise to create a tool to improve the concatenation of strings.

Installation:

In a browser:

    <script src="dist/string-builder.js"></script>

Using npm:

    npm install --save ns-string-builder

In Node.js:

    var StringBuilder = require('ns-string-builder');
    var sb = new StringBuilder();
    var paragraphs = ["Why won't the ineffective paradox cruise? A medicine screams next to a class! The lord fulfills the chairman."];
    sb
        .cat('<html>')
        .cat(
            '<head>',
            ['<title>', 'Demo String Builder', '</title>'],
            '</head>'
        )
        .cat('<body>')
        .wrap('<h1>', '</h1>')
        .cat('Hello World!!')
        .end()
        .wrap('<p>', '</p>')
        .each(paragraphs, function(paragraphs) {
            this.cat(paragraphs)
        })
        .end()
        .cat('</body>')
        .cat('</html>');

    sb.string();

cat(value1[, value2 [,valueN]])

Method to add values to the buffer of the StringBuilder.

  • Syntax

        var sb = stringBuilder();
        sb.cat('hello');
        sb.cat('Javascript', 'crazy', 'world').cat('!!!');
        sb.cat(['nestedValue1', 'nestedValue2']).cat(() => 'Hello my Function');
  • Parameters

    • values
      • Could be strings, arrays and functions.

string()

This method returns a concatenated string of all the parameters that are in the buffer.

  • Syntax

        var sb = stringBuilder();
        sb.cat('hello', '!!');
        sb.string(); // 'hello!!'

rep(args1[, argN] ,howManyTimes)

Method that concatenates the same string a given number of times.

  • Syntax

        var sb = new StringBuilder();
        sb.cat('Can I go,')
            .rep('please ', 2)
            .rep('?',3)
  • Parameters

    • args
      • string values
    • howManyTimes
      • number of times to repeat string

catIf(args1[,argsN], flag)

Method that performs string concatenation only if the flag is true.

  • Syntax

         sb.cat('Hello')
            .catIf('EveryOne', myMood === 'happy');
  • Parameters

    • args
      • string values
    • flag
      • boolean value

wrap(prefix, suffix)

Everything added to StringBuilder after this method is called shall be be surrounded by prefix and suffix arguments.

  • Syntax

        sb.cat('<ul>', '\n')
            .wrap('<li>', ['</li>' ,'\n'])
            .rep('list item', 2)
            .end()
            .cat('</ul>'); // <ul>\n<li>list item</li>\n<li>list item</li>\n</ul>        
  • Parameters

    • prefix
      • strings, functions, arrays
    • suffix
      • strings, functions, arrays

end()

Cancel the current or last effect that was added to the StringBuilder by calling any of the folloowing methods: wrap.

prefix(args)

Everything added after calling this method shall be prefix with the specified arguments.

  • Syntax

         sb.prefix('##')
            .cat('YEI')
            .cat(['!'])
            .string(); // "##YEI##!";
  • Parameters

    • args
      • strings, functions, arrays

suffix(args)

Everything added after calling this method shall be suffix with the specified arguments.

  • Syntax

     sb.suffix('\n')
            .cat('Hello')
            .cat(['World'])
            .string(); // Hello\nWrold\n
  • Parameters

    • args
      • strings, functions, arrays

each(collection, callback)

Allows the iteration over an array of values without breaking the cascasde or chain. It shall to iterate over each value on the array and then call the callback function. The each method will call the callback setting the context(this) reference to the StringBuilder and will send 3 three parameters value , index and args .

  • Syntax

        sb.each(people, function(value, index, people) {
                this
                    .cat('<tr>')
                    .prefix('\t')
                    .cat('<td>' + value.name + '</td>')
                    .cat('<td>' + value.age + '</td>')
                    .end()
                    .cat('</tr>')
            });
  • Parameters

    • collection
      • array
    • callback
      • function
        • with parameters value, index and people

suspend()

This method suspend or pause the applied effects(prefix, suffix and wrap) and it's influence will finish with the call to end() method.

when(expression, thenArgs, otherwiseArgs)

This method evaluate the expression and call the cat() method with the thenArgs or otherwiseArgs depending on the result of evaluation

  • Syntax

        sb.suffix('\n')
            .wrap('<p>', '</p>')
            .each(fixtures.peopleWithGender, function(person) {
                this.when(person.sex == 'm', () => { return person.name + ' is male' }, [ person.name,' is female' ]);
            });
    
        sb.string() // <p>pedro is male</p>\n<p>leticia is female</p>\n<p>pablo is male</p>\n;
  • Parameters

    • expresion
      • function or expressin
    • thenArgs
      • string, array, function
    • otherwiseArgs
      • string, array, function