A collection of useful static methods to deal with JavaScript strings.
- Since:
- 1.0.0
Methods
(static) areEqual(str1, str2, lenientopt) → {Boolean}
Returns true
if the passed values are equal, false
otherwise.
-
When the lenient flag is unset or is set to
true
, the comparison will ignore the case and trim the strings before comparing; the two strings are considered equal if,- Both strings are empty, as defined by
mindsmine.String.isEmpty
. -
Trimmed versions of both strings, as defined by
String.prototype.trim()
, are equal.
- Both strings are empty, as defined by
-
When the lenient flag is set to
false
, the two strings are considered equal if,- Both strings are not null
- Both strings represent the same sequence of characters
Example usage:
mindsmine.String.areEqual(null, null, true) // true
mindsmine.String.areEqual(null, "", true) // true
mindsmine.String.areEqual("", null, true) // true
mindsmine.String.areEqual("", "", true) // true
mindsmine.String.areEqual(" ", "", true) // true
mindsmine.String.areEqual(" abc", "abc ", true) // true
mindsmine.String.areEqual("", "abc", true) // false
mindsmine.String.areEqual("ab c", "abc", true) // false
mindsmine.String.areEqual("ABC", "abc", true) // true
mindsmine.String.areEqual("abc", "abc", true) // true
mindsmine.String.areEqual(null, null) // true
mindsmine.String.areEqual(null, "") // true
mindsmine.String.areEqual("", null) // true
mindsmine.String.areEqual("", "") // true
mindsmine.String.areEqual(" ", "") // true
mindsmine.String.areEqual(" abc", "abc ") // true
mindsmine.String.areEqual("", "abc") // false
mindsmine.String.areEqual("ab c", "abc") // false
mindsmine.String.areEqual("ABC", "abc") // true
mindsmine.String.areEqual("abc", "abc") // true
mindsmine.String.areEqual(null, null, false) // false
mindsmine.String.areEqual(null, "", false) // false
mindsmine.String.areEqual("", null, false) // false
mindsmine.String.areEqual("", "", false) // true
mindsmine.String.areEqual(" ", "", false) // false
mindsmine.String.areEqual(" abc", "abc ", false) // false
mindsmine.String.areEqual("", "abc", false) // false
mindsmine.String.areEqual("ab c", "abc", false) // false
mindsmine.String.areEqual("ABC", "abc", false) // false
mindsmine.String.areEqual("abc", "abc", false) // true
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
str1 |
String | to compare |
||
str2 |
String | to compare |
||
lenient |
Boolean |
<optional> |
true | whether to be lenient or not |
- Since:
- 2.1.0
Returns:
whether two strings are equal
- Type
- Boolean
(static) format(string, …values) → {String}
Allows you to define a tokenised string and pass an arbitrary number of arguments to replace the tokens. Each
token must be unique, and must increment in the format {0}
, {1}
, etc.
Example usage:
var str1 = "Hello";
var str2 = "World";
var str3 = mindsmine.String.format("Let us combine {0} and {1} together.", str1, str2);
// str3 now contains the string: "Let us combine Hello and World together."
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
string |
String | The tokenised string to be formatted. |
|
values |
String |
<repeatable> |
The values to replace tokens |
- Since:
- 1.0.0
Returns:
The formatted string
- Type
- String
(static) getNullSafe(str) → {String}
Returns a non-null string, even if the object being passed is a null string.
If the passed-in object is a non-null string, then it is returned as-is.
Example usage:
mindsmine.String.getNullSafe(null) // ""
mindsmine.String.getNullSafe(undefined) // ""
mindsmine.String.getNullSafe(NaN) // ""
mindsmine.String.getNullSafe(100) // ""
mindsmine.String.getNullSafe("") // ""
mindsmine.String.getNullSafe("hello") // hello
mindsmine.String.getNullSafe(true) // ""
Parameters:
Name | Type | Description |
---|---|---|
str |
String | The string to safeguard against |
- Since:
- 1.0.0
Returns:
If str is null
then ""
(empty string).
- Type
- String
(static) htmlDecode(str) → {String}
Convert certain characters (&, <, >, ' and ") from their HTML character equivalents.
Parameters:
Name | Type | Description |
---|---|---|
str |
String | The string to decode. |
- Since:
- 1.0.0
Returns:
The decoded text.
- Type
- String
(static) htmlEncode(str) → {String}
Convert certain characters (&, <, >, ' and ") to their HTML character equivalents for literal display in web pages.
Parameters:
Name | Type | Description |
---|---|---|
str |
String | The string to encode. |
- Since:
- 1.0.0
Returns:
The encoded text.
- Type
- String
(static) isEmpty(str) → {Boolean}
Returns true
if the passed value is an empty string, false otherwise.
The value is deemed to be an empty string if it is either:
null
undefined
NaN
- not a string
- a zero-length string
Parameters:
Name | Type | Description |
---|---|---|
str |
Object | The value to test. |
- Since:
- 1.0.0
Returns:
- Type
- Boolean
(static) isPalindrome(str, lenientopt) → {Boolean}
Returns true
if the passed string is a palindrome, false
otherwise.
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward, such as madam or kayak.
Convenience method equivalent to mindsmine.String.areEqual(string, string.reverse, flag)
Example usage:
mindsmine.String.isPalindrome(null, true) // true
mindsmine.String.isPalindrome("", true) // true
mindsmine.String.isPalindrome(" ", true) // true
mindsmine.String.isPalindrome(" aba", true) // true
mindsmine.String.isPalindrome("aba", true) // true
mindsmine.String.isPalindrome("mAdAm", true) // true
mindsmine.String.isPalindrome("madAm", true) // true
mindsmine.String.isPalindrome("madam", true) // true
mindsmine.String.isPalindrome("Madam", true) // true
mindsmine.String.isPalindrome("hello", true) // false
mindsmine.String.isPalindrome(null) // true
mindsmine.String.isPalindrome("") // true
mindsmine.String.isPalindrome(" ") // true
mindsmine.String.isPalindrome(" aba") // true
mindsmine.String.isPalindrome("aba") // true
mindsmine.String.isPalindrome("mAdAm") // true
mindsmine.String.isPalindrome("madAm") // true
mindsmine.String.isPalindrome("madam") // true
mindsmine.String.isPalindrome("Madam") // true
mindsmine.String.isPalindrome("hello") // false
mindsmine.String.isPalindrome(null, false) // false
mindsmine.String.isPalindrome("", false) // true
mindsmine.String.isPalindrome(" ", false) // true
mindsmine.String.isPalindrome(" aba", false) // false
mindsmine.String.isPalindrome("aba", false) // true
mindsmine.String.isPalindrome("mAdAm", false) // true
mindsmine.String.isPalindrome("madAm", false) // false
mindsmine.String.isPalindrome("madam", false) // true
mindsmine.String.isPalindrome("Madam", false) // false
mindsmine.String.isPalindrome("hello", false) // false
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
str |
String | to check |
||
lenient |
Boolean |
<optional> |
true | whether to be lenient or not |
- Since:
- 2.1.0
- See:
Returns:
if the string is a Palindrome
- Type
- Boolean