Angular 2+ String Helper
Ngx lacks complete string manipulation operations. This is an attempt to fill that gap.
Installation
Install via npm
$ npm i ngx-string-helper --save
API Usage
To use it in your Angular 2 app import the module in your root module(app.module.ts) as shown below.
import { NgxStrHelperModule } from 'ngx-string-helper';
...
...
@NgModule({
imports:[
NgxStrHelperModule
],
})
In your components simply import the service.
import { NgxStrHelper } from 'ngx-string-helper';
...
...
export class YOUR_MODULE {
constructor(private ngxStrHelper: NgxStrHelper) {
}
}
NgxStrHelper.isNullOrEmpty();
let sampleString = "Input is a string";
let isNullOrEmpty = ngxStrHelper.isNullOrEmpty(sampleString);
NgxStrHelper.isBlank();
isBlank("");
isBlank("\n");
isBlank(" ");
isBlank("a");
NgxStrHelper.contains();
if (ngxStrHelper.contains('Input is a string', 'is')) {
}
NgxStrHelper.truncate();
var sampleString = "Input is a string";
var str = ngxStrHelper.truncate(sampleString, 9);
NgxStrHelper.toSlug();
var sampleString = "Input is a string";
var slug = ngxStrHelper.toSlug(sampleString);
NgxStrHelper.decapitalize();
Converts first letter of the string to lowercase.
ngxStrHelper.decapitalize("Foo Bar");
NgxStrHelper.capitalize();
Converts first letter of the string to uppercase. If true is passed as second argument the rest of the string will be converted to lower case.
ngxStrHelper.capitalize("foo Bar");
ngxStrHelper.capitalize("FOO Bar", true);
NgxStrHelper.clean();
Trim and replace multiple spaces with a single space.
ngxStrHelper.clean(" foo bar ");
NgxStrHelper.chars();
ngxStrHelper.chars("Hello");
NgxStrHelper.swapCase();
Returns a copy of the string in which all the case-based characters have had their case swapped.
ngxStrHelper.swapCase("hELLO");
NgxStrHelper.include();
Tests if string contains a substring.
ngxStrHelper.include("foobar", "ob");
NgxStrHelper.count();
Returns number of occurrences of substring in string.
ngxStrHelper.count("Hello world", "l");
NgxStrHelper.escapeHTML();
Converts HTML special characters to their entity equivalents. This function supports cent, yen, euro, pound, lt, gt, copy, reg, quote, amp, apos.
ngxStrHelper.escapeHTML("<div>Blah blah blah</div>");
NgxStrHelper.unescapeHTML();
Converts entity characters to HTML equivalents. This function supports cent, yen, euro, pound, lt, gt, copy, reg, quote, amp, apos, nbsp.
ngxStrHelper.unescapeHTML("<div>Blah blah blah</div>");
NgxStrHelper.insert();
ngxStrHelper.insert("Hellworld", 4, "o ");
#### NgxStrHelper.splice();
Like an array splice.
```javascript
ngxStrHelper.splice("https://github.com/test/ngx-string-helper", 19, 4, "thuyetngx");
// => Outputs: "https://github.com/thuyetngx/ngx-string-helper"
NgxStrHelper.replaceAll();
ngxStrHelper.replaceAll("foo", "o", "a");
NgxStrHelper.reverse();
Return reversed string
ngxStrHelper.reverse("foobar");
NgxStrHelper.startsWith();
This method checks whether the string begins with starts at position (default: 0)
ngxStrHelper.startsWith("image.gif", "image");
ngxStrHelper.startsWith(".vimrc", "vim", 1);
NgxStrHelper.endsWith();
This method checks whether the string ends with ends at position (default: string.length).
ngxStrHelper.endsWith("image.gif", "gif");
ngxStrHelper.endsWith("image.old.gif", "old", 9);
NgxStrHelper.pred();
Returns the predecessor to str.
ngxStrHelper.pred("b");
ngxStrHelper.pred("B");
NgxStrHelper.succ();
Returns the successor to str.
ngxStrHelper.succ("a");
ngxStrHelper.succ("A");
NgxStrHelper.titleize();
ngxStrHelper.titleize("my name is thuyet");
NgxStrHelper.camelize();
Converts underscored or dasherized string to a camelized one. Begins with a lower case letter unless it starts with an underscore, dash or an upper case letter.
ngxStrHelper.camelize("moz-transform");
ngxStrHelper.camelize("-moz-transform");
ngxStrHelper.camelize("_moz_transform");
ngxStrHelper.camelize("Moz-transform");
ngxStrHelper.camelize("-moz-transform", true);
NgxStrHelper.classify();
Converts string to camelized class name. First letter is always upper case
ngxStrHelper.classify("thuyet_le_van");
NgxStrHelper.underscored();
Converts a camelized or dasherized string into an underscored one
ngxStrHelper.underscored("ThuyetLeVan");
NgxStrHelper.dasherize();
Converts a underscored or camelized string into an dasherized one
ngxStrHelper.underscored("ThuyetLeVan");
NgxStrHelper.humanize();
Converts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace, and removes the postfix '_id'.
ngxStrHelper.humanize(" capitalize dash-CamelCase_underscore trim ");
NgxStrHelper.toNumber();
Parse string to number. Returns NaN if string can't be parsed to number.
ngxStrHelper.toNumber("2.556");
ngxStrHelper.toNumber("2.556", 1);
ngxStrHelper.toNumber("999.999", -1);
NgxStrHelper.stripTags();
Removes all html tags from string.
ngxStrHelper.stripTags("a <a href=\"#\">link</a>");
ngxStrHelper.stripTags("a <a href=\"#\">link</a><script>alert(\"hello world!\")</script>");
NgxStrHelper.repeat();
Repeats a string count times.
ngxStrHelper.repeat("HEY", 3);
ngxStrHelper.repeat("HEY", 3, "HI");
NgxStrHelper.surround();
Surround a string with another string.
ngxStrHelper.surround("HEY", "NAME");
NgxStrHelper.quote();
Quotes a string. quoteChar defaults to ".
ngxStrHelper.quote("DEMO", '"');
NgxStrHelper.unquote();
Unquotes a string. quoteChar defaults to ".
ngxStrHelper.unquote('"DEMO"');
ngxStrHelper.unquote("'DEMO'", "'");
NgxStrHelper.toBoolean();
Turn strings that can be commonly considered as booleas to real booleans. Such as "true", "false", "1" and "0". This function is case insensitive.
ngxStrHelper.toBoolean("true");
ngxStrHelper.toBoolean("FALSE");
ngxStrHelper.toBoolean("random");
It can be customized by giving arrays of truth and falsy value matcher as parameters. Matchers can be also RegExp objects.
ngxStrHelper.toBoolean("truthy", ["truthy"], ["falsy"]);
ngxStrHelper.toBoolean("true only at start", [/^true/]);
NgxStrHelper.chop();
ngxStrHelper.chop("whitespace", 3);
NgxStrHelper.chompLeft();
Removes prefix from start of string.
ngxStrHelper.chompLeft('foobar', 'foo')
ngxStrHelper.chompLeft('foobar', 'bar')
NgxStrHelper.chompRight();
Removes suffix from end of string.
ngxStrHelper.chompRight('foobar', 'foo')
ngxStrHelper.chompRight('foobar', 'bar')
NgxStrHelper.collapseWhitespace();
Converts all adjacent whitespace characters to a single space.
ngxStrHelper.collapseWhitespace(" String \t libraries are \n\n\t fun\n! ");
NgxStrHelper.ensureLeft();
Ensures string starts with prefix.
ngxStrHelper.ensureLeft("subdir", "/");
NgxStrHelper.ensureRight();
Ensures string ends with suffix.
ngxStrHelper.ensureRight("dir", "/");
NgxStrHelper.isAlpha();
Return true if the string contains only letters.
ngxStrHelper.isAlpha("afaf");
ngxStrHelper.isAlpha("fdafaf3");
NgxStrHelper.isAlphaNumeric();
Return true if the string contains only letters and numbers
ngxStrHelper.isAlphaNumeric("afaf35353afaf");
ngxStrHelper.isAlphaNumeric("FFFF99fff");
ngxStrHelper.isAlphaNumeric("aaff..");
NgxStrHelper.isLower();
Return true if the character or string is lowercase
ngxStrHelper.isLower("a");
ngxStrHelper.isLower("z");
ngxStrHelper.isLower("B");
ngxStrHelper.isLower("hithuyet");
ngxStrHelper.isLower("hi thuyet");
NgxStrHelper.isNumeric();
Return true if the string only contains digits
ngxStrHelper.isNumeric("3");
ngxStrHelper.isNumeric("000992424242");
ngxStrHelper.isNumeric("B");
ngxStrHelper.isNumeric("34.22");
ngxStrHelper.isNumeric("NaN");
ngxStrHelper.isNumeric("THUYET");
NgxStrHelper.isUpper();
Returns true if the character or string is uppercase
ngxStrHelper.isUpper("A");
ngxStrHelper.isUpper("Z");
ngxStrHelper.isUpper("b");
ngxStrHelper.isUpper("HITHUYET");
ngxStrHelper.isUpper("hi thuyet");
NgxStrHelper.left();
Return the substring denoted by n positive left-most characters.
ngxStrHelper.left("My name Thuyet", 2)
ngxStrHelper.left("My name Thuyet", 0)
NgxStrHelper.right();
Return the substring denoted by n positive right-most characters.
ngxStrHelper.right("My name Thuyet", 2)
ngxStrHelper.right("My name Thuyet", 0)
NgxStrHelper.times();
Returns a string repeated n times.
ngxStrHelper.times("*", 2)
Thanks and enjoy!