A Node.js library, designed to simplify work with string validation and sanitization.
npm install jstring
You need deal with string validation or sanitization in your Node.js project.
var jstring = require('jstring');
Main sanitization method, that takes string and task object, and return string after processing. (All jstring methods have public access, and You can use them direct. This method is developed for convenience).
Source string.
A object, that contains necessary functions and their parameters. {function: [param], function: [param]} Can be used with: removeTags; cut;
String processed.
jstring.handleString('Foo <Bar>Buzz', { cut: 6,
removeTags:null,
replace: ['Fo', 'FO', true]
});
//return: FOo Bu
Removes all <> and <> elements of the string.
Source string.
A string, or array, that contains exclusion tag names. (Notice that closing tags is added automatically. For example if you pass 'div' as except - tag </div> will not be deleted.)
String processed.
jstring.removeTags('Foo <Bar><Buzz>');
//return: Foo
jstring.removeTags('Foo <Bar><Buzz>', Bar);
//return: Foo <Bar>
jstring.removeTags('Foo <Bar><Buzz>', Bar);
//return: Foo
Cuts the string from start to length.
Source string.
The length by which the source string will be cut including.
String processed.
jstring.cut('Foo <Bar><Buzz>', 9);
//return: Foo <Bar>
Just a wrapper over toLowerCase method, to allow use it in handleString method.
Source string.
String processed.
jstring.toLow('Foo Bar');
//return: foo bar
Just a wrapper over toUpperCase method, to allow use it in handleString method.
Source string.
String processed.
jstring.toUp('Foo Bar');
//return: FOO BAR
Makes first character of the string capitalized.
Source string.
String processed.
jstring.capitalize('foo Bar');
//return: Foo Bar
Removes extra whitespace.
Source string.
String processed.
jstring.minifyWhitespace('Foo Bar Buzz');
//return: Foo Bar Buzz
Replaces substring in main string on new string.
Source string.
String, that need to be replaced with replaceString.
String, that need to be placed instead searchString.
Boolean if replacement should be done strict by case sensitivity (true if case of searchString is matters).
String processed.
jstring.replace('Foo Bar foo', ['foo', 'buzz']);
//return: buzz Bar foo
jstring.replace('Foo Bar foo', ['foo', 'buzz', true]);
//return: Foo Bar buzz
Replaces all occurrences of substring in main string on new string.
Source string.
Strings, that need to be replaced with replaceString.
String, that need to be placed instead searchString.
Boolean if replacement should be done strict by case sensitivity (true if case of searchString is matters).
String processed.
jstring.replace('Foo Bar foo', ['foo', 'buzz']);
//return: buzz Bar buzz
jstring.replace('Foo Bar foo', ['foo', 'buzz', true]);
//return: Foo Bar buzz
Returns left part of string with length.
Source string.
Length of string, that need to be cut from start.
String processed.
jstring.left('Foo Bar foo', 3);
//return: Foo
jstring.left('Foo Bar foo', 5);
//return: Foo B
Returns right part of string with length.
Source string.
Length of string, that need to be cut to end.
String processed.
jstring.right('Foo Bar foo', 3);
//return: foo
jstring.right('Foo Bar foo', 5);
//return: r foo
Returns the string, repeated n times.
Source string.
Count of repeat times.
String processed.
jstring.repeat('Foo', 3);
//return: FooFooFoo
jstring.repeat('Foo Bar', 2);
//return: Foo BarFoo Bar
Iterate through every char of incoming string and call the callback function to it.
Source string.
Callback function, that will be called to each char of string.
Char of string.
Index of value in string.
jstring.forEach('FooBar', function(value, index){
console.log(value, index);
});
//outputs:
F 0
o 1
o 2
B 3
a 4
r 5
Checks if the source string contains only letters.
Source string.
Boolean is the source string contains only letters.
jstring.isAlpha('Foo <Bar><Buzz>');
//return: false
jstring.isAlpha('Foo BarBuzz');
//return: true
Checks if the source string contains only letters and numbers.
Source string.
Boolean is the source string contains only letters and numbers.
jstring.isAlphanumeric('Foo <Bar><Buzz> 67');
//return: false
jstring.isAlphanumeric('Foo BarBuzz 67');
//return: true
Checks if the source string is a valid email.
Source string.
Boolean is the source string is a valid email.
jstring.isEmail('[email protected]');
//return: true
jstring.isEmail('Foo Bar');
//return: false
Checks if the source string is uppercase.
Source string.
Set to true if test should be strict (return true only if all chars of string is uppercase).
Boolean is the source string is uppercase.
jstring.isUpper('Foo Bar');
//return: true
jstring.isUpper('foo bar');
//return: false
jstring.isUpper('Foo Bar', true);
//return: false
Checks if the source string is lowercase.
Source string.
Set to true if test should be strict (return true only if all chars of string is lowercase).
Boolean is the source string is lowercase.
jstring.isLower('Foo Bar');
//return: true
jstring.isLower('FOO BAR');
//return: false
jstring.isLower('Foo Bar', true);
//return: false