Module: UWA/String

UWA/String

String utility functions.

Index

Methods

<static> UWA.String.isEmail(string) → {Boolean}

Validate if a String is a valid email adresse.

Example
UWA.String.isEmail("example.com");
// will return `false`
UWA.String.isEmail("john@example.com");
// will return `true`
Parameters
Name Type Description
string String

the string to test

Returns

True if is a valid email adresse else false.

Type
Boolean

<static> UWA.String.truncate(string, length, truncation) → {String}

Truncates a string to the given length and appends a suffix to it (indicating that it is only an excerpt).

Example
UWA.String.truncate("hello world", 10);
// will return "hello w..."
Parameters
Name Type Description
string String

the string to truncate

length Number

max length of the string

truncation Number

truncation String used for elipsis, "..." by default

Returns

Truncate string.

Type
String

<static> UWA.String.cut(string, length, truncation) → {String}

Cut a string to the given length and appends a suffix to it (indicating that it is only an excerpt). Unlike UWA.String.truncate, this function will try to avoid breaking words.

Example
UWA.String.cut("hello world", 10);
// will return "hello..."
Parameters
Name Type Description
string String

the string to cut

length Number

max length of the string

truncation Number

truncation String used for elipsis, "..." by default

Returns

Cut string.

Type
String

<static> UWA.String.escapeRegExp(string) → {String}

Returns a string with escaped regular expression characters.

Example
UWA.String.escapeRegExp("animals.sheep[1]");
// will return "animals\.sheep\[1\]"
Parameters
Name Type Description
string String

the string to escape

Returns

Escaped RegExp string.

Type
String

<static> UWA.String.stripTags(string) → {String}

Strips a string of any HTML tags.

WARNING: THIS IS NOT XSS PROOF. Use escapeHTML.

Example
UWA.String.stripTags("<h1>hello</h1><a href=''>world</a>");
// will return "hello world"
Parameters
Name Type Description
string String

the string containing tags

Returns

Converted string.

Type
String

<static> UWA.String.stripScripts(string, evaluate) → {String}

Strips a string of any HTML scripts tags.

WARNING: THIS IS NOT XSS PROOF. Use escapeHTML.

Example
UWA.String.stripScripts("<h1>hello</h1><a href=''>world</a><script>alert('hello')</script>");
// will return "hello world"
Parameters
Name Type Argument Description
string String

the string containing script tags

evaluate Boolean | Function <optional>

If true, evaluate the script. If this is a function, run the function with the script as first argument. Default to false. Please use this very carefully.

Returns

Converted string.

Type
String

<static> UWA.String.stripComments(string) → {String}

Strips a string of any comments. Only comments starting at the beginning of a line will be removed.

Example
UWA.String.stripComments("hello world \n" + "// My Comment");
// will return "hello world \n"

UWA.String.stripComments("hello world \n" + "/* My Comment \n *\/");
// will return "hello world \n"
Parameters
Name Type Description
string String

the string containing JS comments

Returns

Converted string.

Type
String

<static> UWA.String.escapeHTMLEntities(string) → {String}

Replace htmlentities to XHTML characters.

Example
UWA.String.escapeHTMLEntities("'");
// will return "'"
Parameters
Name Type Description
string String

the string to escape

Returns

Converted string.

Type
String

<static> UWA.String.globToRegex(string) → {RegExp}

Translates a glob string expression into a JavaScript regular expression.

The following syntax is supported for glob expressions.

  • *: Matches zero or more instances of any character. If the STAR_CANNOT_MATCH_NULL_MASK option is used,
     * matches one or more instances of any character.
  • ? - Matches one instance of any character. If the QUESTION_MATCHES_ZERO_OR_ONE_MASK option is used,
     ? matches zero or one instances of any character.
  • [...] - Matches any of characters enclosed by the brackets. * and ? lose their special meanings within a character class.
     Additionaly if the first character following the opening bracket is a ! or a ^, then any character not in the character
     class is matched. A - between two characters can be used to denote a range. A - at the beginning or end of the character
     class matches itself rather than referring to a range. A ] immediately following the opening [ matches itself rather than
     indicating the end of the character class, otherwise it must be escaped with a backslash to refer to itself.
  • \ - A backslash matches itself in most situations. But when a special character such as a * follows it,
     a backslash escapes the character, indicating that the special chracter should be interpreted as a normal character instead of its special meaning.

All other characters match themselves.

Please remember that the when you construct a JavaScript string in JavaScript code, the backslash character is itself a special JavaScript character, and it must be double backslashed to represent single backslash in a regular expression.

Example
UWA.String.globToRegex("*{www,cdn}.example.com").test("https://www.example.com"); // true
UWA.String.globToRegex("*{www,cdn}.example.com").test("https://example.com"); // false
Parameters
Name Type Description
string String

the glob to convert to regexp

Returns

Generated RegExp from blob string.

Type
RegExp

<static> UWA.String.escapeHTML(string, tagsOnly) → {String}

Escape HTML in a String.

Example
UWA.String.escapeHTML("hello workd");
// will return "<html>hello workd</html>"
Parameters
Name Type Description
string String

the string to escape

tagsOnly Boolean

escape only the tags ( '>' '<' and '/' characters)

Returns

The converted string.

Type
String

<static> UWA.String.unescapeHTML(string) → {String}

Unescape HTML on a String. Warning: it uses a DOM element, so this function may not be available following your environment

Example
UWA.String.unescapeHTML("<html>hello workd</html>");
// will return "hello workd"
Parameters
Name Type Description
string String

the string to unescape

Returns

The converted string.

Type
String

<static> UWA.String.ucfirst(string) → {String}

Convert first String letter to UpperCase.

Example
UWA.String.ucfirst("hello world");
// will return "Hello world"
Parameters
Name Type Description
string String

the source string

Returns

Updated String

Type
String

<static> UWA.String.ucfirst(string) → {String}

Convert first String letter to lowerCase

Example
UWA.String.lcfirst("Hello Patrick !");
// will return "hello Patrick !"
Parameters
Name Type Description
string String

the source string

Returns

Updated String

Type
String

<static> UWA.String.capitalize(string) → {String}

Converts the first letter of each word in a string to uppercase.

Example
UWA.String.capitalize("hello world");
// will return "Hello World"
Parameters
Name Type Description
string String

the source string

Returns

The converted string.

Type
String

<static> UWA.String.camelCase(string) → {String}

Convert current String with space and comma in CamelCase valid string.

Example
UWA.String.camelCase("hello world");
// will return "helloWorld"
UWA.String.camelCase("hello-world");
// will return "helloWorld"
UWA.String.camelCase("hello_world");
// will return "helloWorld"
UWA.String.camelCase("hello_world_");
// will return "helloWorld"
UWA.String.camelCase("Helloworld");
// will return "helloworld"
Parameters
Name Type Description
string String

the source string

Returns

The converted string.

Type
String

<static> UWA.String.unCamelCase(string) → {String}

Convert current String in CamelCase to a non CamelCase valid string.

Example
UWA.String.unCamelCase("helloWorld");
// will return "hello-world"
UWA.String.unCamelCase("helloWorld", ' ');
// will return "hello world"
UWA.String.unCamelCase("helloWorld", '_');
// will return "hello_world"
Parameters
Name Type Description
string String

the source string

Returns

The converted string.

Type
String

<static> UWA.String.rgbToHex(string, array) → {Mixed}

Converts an RGB color value to hexadecimal. Input string must be in one of the following RGB color formats. "rgb(255, 255, 255)", or "rgba(255, 255, 255, 1)".

Example
UWA.String.rgbToHex('rgb(17, 34, 51)'); // returns '#112233'
UWA.String.rgbToHex('rgb(17, 34, 51)', true); // returns ['11', '22', '33']
UWA.String.rgbToHex('rgba(17, 34, 51, 0)'); // returns 'transparent'
Parameters
Name Type Description
string String

a string representing a RGB color

array Boolean

If true is passed, will output an array (e.g. ['ff', '33', '00']) instead of a string (e.g. "#ff3300")

Returns

String or array depend array parameter value.

Type
Mixed

<static> UWA.String.hexToRgb(string, array) → {Mixed}

Converts a hexadecimal color value to RGB. Input string must be in one of the following hexadecimal color formats (with or without the hash) '#ffffff', #fff', 'ffffff', or 'fff'.

Example
UWA.String.hexToRgb('#123'); // returns 'rgb(17, 34, 51)'
UWA.String.hexToRgb('112233'); // returns 'rgb(17, 34, 51)'
UWA.String.hexToRgb('#112233', true); // returns [17, 34, 51]
Parameters
Name Type Description
string String

a string representing a hexadecimal color

array Bool

If true is passed, will output an array (e.g. [255, 51, 0]) instead of a string (e.g. "rgb(255,51,0)")

Returns

String or array depend array parameter value.

Type
Mixed

<static> UWA.String.format(string) → {String}

Replace {d+} into string by the respective argument position.

Example
UWA.String.format('Hello {0} welcome on {1}.', 'John', 'UWA');
// will return 'Hello John welcome on UWA.'
Parameters
Name Type Description
string String

the template string

replacements... String

other arguments are string replacements

Returns

Current String Instance.

Type
String

<static> UWA.String.parseRelativeTime(string, offset) → {String}

Convert date string to a human readable elapsed time, like "2 days ago".

Parameters
Name Type Description
string String

a string representing a date

offset Number

timezone offset

Returns

Relative Time.

Notes: Supported format are:

  • timestamp in milli seconds
  • timestamp in seconds
  • XXX XXX DD YYYY HH:MM:SS GMT+NNNN (XXX)
  • YYYY/MM/DD
  • YYYY-MM-DD
Type
String

<static> UWA.String.makeClickable(string) → {String}

Convert URLs contained in a String as clickable link.

Example
UWA.String.makeClickable("visit http://dev.netvibes.com for more tips");
// will return "visit http://dev.netvibes.com for more tips"
Parameters
Name Type Description
string String

a source string

Returns

The <a> HTML link with String value has href attribute.

Type
String

<static> UWA.String.highlight(string) → {String}

Deprecated: This function does nothing, please don't use it.

Parameters
Name Type Description
string String
Returns

updated String.

Type
String

<static> UWA.String.test(string, regexp, modifier) → {String}

Apply a regexp on a String. Deprecated in favor of RegExp#test.

Example
UWA.String.test("john", /^jack$/);
// will return false
UWA.String.test("jack", /^jack$/);
// will return true
Parameters
Name Type Argument Default Description
string String

the string to test

regexp String

The regexp to apply on the string

modifier String <optional>
''

The modifier to apply on the regexp

Returns

The RegExp results.

Type
String