ngx-string-helper
v2.0.16
Published
Ngx lacks complete string manipulation operations. This is an attempt to fill that gap.
Downloads
17
Maintainers
Readme
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);
// => Outputs: False
NgxStrHelper.isBlank();
isBlank(""); // => Outputs: true
isBlank("\n"); // => Outputs: true
isBlank(" "); // => Outputs: true
isBlank("a"); // => Outputs: false
NgxStrHelper.contains();
//returns true
if (ngxStrHelper.contains('Input is a string', 'is')) {
//Do something awesome here..
}
NgxStrHelper.truncate();
var sampleString = "Input is a string";
var str = ngxStrHelper.truncate(sampleString, 9);
// => Outputs: Input is a ...
NgxStrHelper.toSlug();
var sampleString = "Input is a string";
var slug = ngxStrHelper.toSlug(sampleString);
// => Outputs: input-is-a-string
NgxStrHelper.decapitalize();
Converts first letter of the string to lowercase.
ngxStrHelper.decapitalize("Foo Bar");
// => Outputs: "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");
// => Outputs: "Foo Bar"
ngxStrHelper.capitalize("FOO Bar", true);
// => Outputs: "Foo bar"
NgxStrHelper.clean();
Trim and replace multiple spaces with a single space.
ngxStrHelper.clean(" foo bar ");
// => Outputs: "foo bar"
NgxStrHelper.chars();
ngxStrHelper.chars("Hello");
// => Outputs: ["H", "e", "l", "l", "o"]
NgxStrHelper.swapCase();
Returns a copy of the string in which all the case-based characters have had their case swapped.
ngxStrHelper.swapCase("hELLO");
// => Outputs: "Hello"
NgxStrHelper.include();
Tests if string contains a substring.
ngxStrHelper.include("foobar", "ob");
// => Outputs: true
NgxStrHelper.count();
Returns number of occurrences of substring in string.
ngxStrHelper.count("Hello world", "l");
// => Outputs: 3
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>");
// => Outputs: "<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>");
// => Outputs: "<div>Blah blah blah</div>"
NgxStrHelper.insert();
ngxStrHelper.insert("Hellworld", 4, "o ");
// => Outputs: "Hello world"
#### 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");
// => Outputs: "faa"
NgxStrHelper.reverse();
Return reversed string
ngxStrHelper.reverse("foobar");
// => Outputs: "raboof"
NgxStrHelper.startsWith();
This method checks whether the string begins with starts at position (default: 0)
ngxStrHelper.startsWith("image.gif", "image");
// => Outputs: true
ngxStrHelper.startsWith(".vimrc", "vim", 1);
// => Outputs: true
NgxStrHelper.endsWith();
This method checks whether the string ends with ends at position (default: string.length).
ngxStrHelper.endsWith("image.gif", "gif");
// => Outputs: true
ngxStrHelper.endsWith("image.old.gif", "old", 9);
// => Outputs: true
NgxStrHelper.pred();
Returns the predecessor to str.
ngxStrHelper.pred("b");
// => Outputs: "a"
ngxStrHelper.pred("B");
// => Outputs: "A"
NgxStrHelper.succ();
Returns the successor to str.
ngxStrHelper.succ("a");
// => Outputs: "b"
ngxStrHelper.succ("A");
// => Outputs: "B"
NgxStrHelper.titleize();
ngxStrHelper.titleize("my name is thuyet");
// => Outputs: "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");
// => Outputs: "mozTransform"
ngxStrHelper.camelize("-moz-transform");
// => Outputs: "MozTransform"
ngxStrHelper.camelize("_moz_transform");
// => Outputs: "MozTransform"
ngxStrHelper.camelize("Moz-transform");
// => Outputs: "MozTransform"
ngxStrHelper.camelize("-moz-transform", true);
// => Outputs: "mozTransform"
NgxStrHelper.classify();
Converts string to camelized class name. First letter is always upper case
ngxStrHelper.classify("thuyet_le_van");
// => Outputs: "ThuyetLeVan"
NgxStrHelper.underscored();
Converts a camelized or dasherized string into an underscored one
ngxStrHelper.underscored("ThuyetLeVan");
// => Outputs: "thuyet_le_van"
NgxStrHelper.dasherize();
Converts a underscored or camelized string into an dasherized one
ngxStrHelper.underscored("ThuyetLeVan");
// => Outputs: "thuyet-le-van"
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 ");
// => Outputs: "Capitalize dash camel case underscore trim"
NgxStrHelper.toNumber();
Parse string to number. Returns NaN if string can't be parsed to number.
ngxStrHelper.toNumber("2.556");
// => Outputs: 3
ngxStrHelper.toNumber("2.556", 1);
// => Outputs: 2.6
ngxStrHelper.toNumber("999.999", -1);
// => Outputs: 990
NgxStrHelper.stripTags();
Removes all html tags from string.
ngxStrHelper.stripTags("a <a href=\"#\">link</a>");
// => Outputs: "a link"
ngxStrHelper.stripTags("a <a href=\"#\">link</a><script>alert(\"hello world!\")</script>");
// => Outputs: "a linkalert("hello world!")"
NgxStrHelper.repeat();
Repeats a string count times.
ngxStrHelper.repeat("HEY", 3);
// => Outputs: "HEYHEYHEY";
ngxStrHelper.repeat("HEY", 3, "HI");
// => Outputs: "HEYHEYHEYHI";
NgxStrHelper.surround();
Surround a string with another string.
ngxStrHelper.surround("HEY", "NAME");
// => Outputs: "NAMEHEYNAME";
NgxStrHelper.quote();
Quotes a string. quoteChar defaults to ".
ngxStrHelper.quote("DEMO", '"');
// => Outputs: '"DEMO"';
NgxStrHelper.unquote();
Unquotes a string. quoteChar defaults to ".
ngxStrHelper.unquote('"DEMO"');
// => Outputs: "DEMO"
ngxStrHelper.unquote("'DEMO'", "'");
// => Outputs: "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");
// => Outputs: true
ngxStrHelper.toBoolean("FALSE");
// => Outputs: false
ngxStrHelper.toBoolean("random");
// => Outputs: undefined
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"]);
// => Outputs: true
ngxStrHelper.toBoolean("true only at start", [/^true/]);
// => Outputs: true
NgxStrHelper.chop();
ngxStrHelper.chop("whitespace", 3);
// => Outputs: ["whi", "tes", "pac", "e"]
NgxStrHelper.chompLeft();
Removes prefix from start of string.
ngxStrHelper.chompLeft('foobar', 'foo')
// => Outputs: "bar"
ngxStrHelper.chompLeft('foobar', 'bar')
// => Outputs: "foobar"
NgxStrHelper.chompRight();
Removes suffix from end of string.
ngxStrHelper.chompRight('foobar', 'foo')
// => Outputs: "foobar"
ngxStrHelper.chompRight('foobar', 'bar')
// => Outputs: "foo"
NgxStrHelper.collapseWhitespace();
Converts all adjacent whitespace characters to a single space.
ngxStrHelper.collapseWhitespace(" String \t libraries are \n\n\t fun\n! ");
// => Outputs: "String libraries are fun !"
NgxStrHelper.ensureLeft();
Ensures string starts with prefix.
ngxStrHelper.ensureLeft("subdir", "/");
// => Outputs: "/subdir"
NgxStrHelper.ensureRight();
Ensures string ends with suffix.
ngxStrHelper.ensureRight("dir", "/");
// => Outputs: "dir/"
NgxStrHelper.isAlpha();
Return true if the string contains only letters.
ngxStrHelper.isAlpha("afaf");
// => Outputs: true
ngxStrHelper.isAlpha("fdafaf3");
// => Outputs: false
NgxStrHelper.isAlphaNumeric();
Return true if the string contains only letters and numbers
ngxStrHelper.isAlphaNumeric("afaf35353afaf");
// => Outputs: true
ngxStrHelper.isAlphaNumeric("FFFF99fff");
// => Outputs: true
ngxStrHelper.isAlphaNumeric("aaff..");
// => Outputs: false
NgxStrHelper.isLower();
Return true if the character or string is lowercase
ngxStrHelper.isLower("a");
// => Outputs: true
ngxStrHelper.isLower("z");
// => Outputs: true
ngxStrHelper.isLower("B");
// => Outputs: false
ngxStrHelper.isLower("hithuyet");
// => Outputs: true
ngxStrHelper.isLower("hi thuyet");
// => Outputs: false
NgxStrHelper.isNumeric();
Return true if the string only contains digits
ngxStrHelper.isNumeric("3");
// => Outputs: true
ngxStrHelper.isNumeric("000992424242");
// => Outputs: true
ngxStrHelper.isNumeric("B");
// => Outputs: false
ngxStrHelper.isNumeric("34.22");
// => Outputs: false
ngxStrHelper.isNumeric("NaN");
// => Outputs: false
ngxStrHelper.isNumeric("THUYET");
// => Outputs: false
NgxStrHelper.isUpper();
Returns true if the character or string is uppercase
ngxStrHelper.isUpper("A");
// => Outputs: true
ngxStrHelper.isUpper("Z");
// => Outputs: true
ngxStrHelper.isUpper("b");
// => Outputs: false
ngxStrHelper.isUpper("HITHUYET");
// => Outputs: true
ngxStrHelper.isUpper("hi thuyet");
// => Outputs: false
NgxStrHelper.left();
Return the substring denoted by n positive left-most characters.
ngxStrHelper.left("My name Thuyet", 2)
// => Outputs: "My"
ngxStrHelper.left("My name Thuyet", 0)
// => Outputs: ""
NgxStrHelper.right();
Return the substring denoted by n positive right-most characters.
ngxStrHelper.right("My name Thuyet", 2)
// => Outputs: "et"
ngxStrHelper.right("My name Thuyet", 0)
// => Outputs: ""
NgxStrHelper.times();
Returns a string repeated n times.
ngxStrHelper.times("*", 2)
// => Outputs: "**"