node package manager


JavaScript Snippet Collection

JavaScript Snippet Collection


Small collection of useful small snippets for JavaScript and Node.js. Designed to be lightweight, independent and undisruptive as much as possible.


  • is -- Check variable types
  • foreach -- For each element in array or object or the element itself
  • split -- Split variable into smaller chunks by regexp
  • join -- Join array or object as a string with multiple seperators
  • pass -- Pass vector values to a callback as arguments

Installation for Node.js

Simplest way to install is to use npm, just simply npm install snippets.


MIT-style license, see INSTALL.txt.


Every feature is a property of snippets:

var foreach = require('snippets').foreach;
var split = require('snippets').split;
var pass = require('snippets').pass;

See examples/ for full examples.


var is = require('snippets').is;
var a = [1,2,3,4,5];
if(is.array(a)) console.log("a is array");
else if(is.object(a)) console.log("a is object");
else console.log("a is not array nor object");


Write each array element to console:

var a = [1,2,3,4,5];
foreach(a).each(function(v) { console.log(v); });

Write each object key=value pair to console:

a = {'foo':1,'bar':2};
foreach(a).each(function(v, k) { console.log(k+"="+v); });

Write single value to console:

a = 'foobar';
foreach(a).each(function(v) { console.log(v); });


Splits string into smaller chunks using regexp:

var vector = split(/: */, "One: Two:  Three:   End", 2);

Resulting vector will be:

["One", "Two:  Three:   End"]

Please note: Standard JavaScript split does NOT append the rest of the string in the last element.


Joins an array into a string with multiple separators:

var str = join(["a","b","c"], [":","."]);

Resulting str will be "a:b.c".

Default separator is ",".

To change the default, use:

var str = join(["a","b","c"], [":"], ".");


Passes the values from array into the callback as arguments:

pass(["a", "b"]).on(function(key, value) { console.log(key+"="+value); });

To parse a key-value line, we can use pass with split:

pass(split(/ *= */, "a = b", 2)).on(function(key,value) { console.log(key+"="+value); });


To remove white spaces:

a = ltrim('  Hello World '); // a = 'Hello World '
a = rtrim('  Hello World '); // a = '  Hello World'
a = trim('  Hello World ');  // a = 'Hello World'


Pad string to specific length from left:

a = lpad('123', 5, '#'); // a = '##123';
a = rpad('123', 5, '#'); // a = '123##';