String

String manipulation and unique key generation.

Functions

Util.cutString

Definition

Name
Util.cutString
Syntax
String = Util.cutString( String string, Number length );

Description

Will cut string to length. Will end the string with "..." if string is longer than length. Compensates for entities (", æ, etc.) when calculating string length.

Parameters

string
String string to cut
length
Number length of the string you want returned

Returns

The cut string.

Examples

var string = "This function will cut the string to the number of letters want"; u.cutString(string, 10);

returns String with text: "This fu..."

Uses

JavaScript
  • String.match
  • String.indexOf
  • String.substring
Manipulator

none

Util.randomString

Definition

Name
Util.randomString
Syntax
String = Util.randomString( [Number length] );

Description

Will generate a random string consisting of uppercase and lowercase letters. If no length is specified, 8 is the default length.

Parameters

length
Number Optional. The length of the random string

Returns

A random string consisting of letters

Examples

u.randomString(10);

returns String like mFrHGruzzo

Uses

JavaScript
  • String.split
Manipulator
  • u.random

Util.uuid

Definition

Name
Util.uuid
Syntax
String = Util.uuid();

Description

Will generate a UUID version 4 string.

Parameters

No parameters

Returns

A unique UUID string

Examples

Util.uuid();

returns String like 75376fed-e97e-44a6-94fe-b9f75e850859

Uses

JavaScript
  • String.split
  • Array.join
  • Math.random
Manipulator

none

Util.prefix

Definition

Name
Util.prefix
Syntax
String = Util.prefix( String string, Number length, [String prefix] );

Description

Pad string to length using prefix.

Parameters

string
String String to be padded
length
Number length to pad string to
prefix
String Optional. String to use as prefix padding. Default "0".

Returns

A string padded to length using prefix

Examples

Util.prefix(1, 2);

returns String 01

Util.prefix("F", 5, "-");

returns String ----F

Uses

JavaScript
  • .toString
Manipulator

none

Util.eitherOr

Definition

Name
Util.eitherOr
Syntax
String = Util.eitherOr( Mixed either, Mixed or );

Description

Checks if either is not undefined or null. If either is valid, return it. If not, returns or instead.

Parameters

either
Mixed Object/String to check
or
Mixed Object/String to return if either is not valid

Returns

Either either (if not undefined or null) or or

Examples

u.eitherOr(0, "zero");

returns 0

u.eitherOr(, "zero");

returns zero

Uses

JavaScript

none

Manipulator

none

Util.upperCaseFirst

Definition

Name
Util.upperCaseFirst
Shorthand
u.ucfirst
Syntax
String = Util.upperCaseFirst( String string );

Description

Make first letter in word uppercase

Parameters

string
String string to perform case conversion on

Returns

String string with first letter in uppercase

Examples

u.ucfirst("martin")

Returns "Martin"

Uses

JavaScript
  • String.toUpperCase
  • String.replace
Manipulator

none

Util.lowerCaseFirst

Definition

Name
Util.lowerCaseFirst
Shorthand
u.lcfirst
Syntax
String = Util.lowerCaseFirst( String string );

Description

Make first letter in word lowercase

Parameters

string
String string to perform case conversion on

Returns

String string with first letter in lowercase

Examples

u.lcfirst("ParentNode")

Returns "parentNode"

Uses

JavaScript
  • String.toLowerCase
  • String.replace
Manipulator

none

Util.normalize

Definition

Name
Util.normalize
Syntax
String = Util.normalize(string);

Description

Normalizes string by lowercasing and replacing any special chars and spaces with - (hyphens). Removes any double or trailing hyphens before returning string.

Parameters

string
String String to normalize

Returns

Normalized string

Examples

u.normalize("København er så lækker");

returns k-benhavn-er-s-l-kker

Uses

JavaScript

String.toLowerCase

String.replace

Manipulator

none

Util.pluralize

Definition

Name
Util.pluralize
Syntax
String = Util.pluralize(count, singular, plural);

Description

Pluralize quantity descriptive string, based on count value.

Parameters

count
Number Quantity used to determine singular/plural.
singular
String String to use in singular case.
plural
String String to use in plural case.

Returns

Quantity specific string, pluralized if needed.

Examples

u.pluralize(houses.length, "house", "houses");

If houses.length is 1, then it returns 1 house. If houses.length is 2, it returns 2 houses.

Uses

JavaScript

none

Manipulator

none

Util.isStringJSON

Definition

Name
Util.isStringJSON
Shorthand
u.isStringJSON
Syntax
Mixed = Util.isStringJSON( String string );

Description

Checks if string contains JSON object. If string contains JSON, JSON-object is returned, and object.isJSON is added to object.

Note: This test is automatically performed on all request responses before being returned to callback function.

Parameters

String
string string to validate for JSON content

Returns

Mixed JSON object if found, else false

Examples

Util.isStringJSON('{"name":"manipulator"}');

Returns JSON object.

Util.isStringJSON('manipulator');

Returns false.

Uses

JavaScript
  • String.substr
  • String.trim
  • String.match
  • try ... catch
  • JSON.parse
Manipulator

none

Util.isStringHTML

Definition

Name
Util.isStringHTML
Shorthand
u.isStringHTML
Syntax
Mixed = Util.isStringHTML( String string );

Description

Checks if string contains DOM object, also knows as HTML. If string contains HTML, DOM-object is returned, and object.isHTML is added to object.

Note: This test is automatically performed on all request responses before being returned to callback function.

Parameters

String
string string to validate for HTML content

Returns

Mixed DOM object if found, else false

Examples

Util.isStringHTML('<div class="scene">manipulator</div>');

Returns HTML object.

Util.isStringHTML('{"name":"manipulator"}');

Returns false.

Uses

JavaScript
  • String.substr
  • String.trim
  • String.match
  • try ... catch
  • document.createElement
  • document.childNodes
Manipulator

none

String.trim

Definition

Name
String.trim
Syntax
String = String.trim();

Description

String trim support for older browsers. Removes whitespace, newlines and tabs from either end of string.

Parameters

No parameters

Returns

Trimmed string

Examples

(" test \n").trim();

returns test

Uses

JavaScript

String.replace

Manipulator

none

String.substr

Definition

Name
String.substr
Syntax
String = String.substr(from, length);

Description

String.substr support in older browsers, primarily correcting faulty implementation in IE8 and older.

Parameters

from
Number Starting point for substring. Negative value to state starting point from end of string.
length
Number Optional length of substring

Returns

Substring of string

Examples

("string").substr(-3, 2);

returns in

("string").substr(4);

returns ng

Uses

JavaScript

String.substring

Manipulator

none

Files

Main file

  • u-string.js

Segment support files

  • u-string-desktop_light.js

Segment dependencies

desktop_edge
u-string.js
desktop_ie11
u-string.js
desktop
u-string.js
desktop_ie10
u-string.js
desktop_ie9
u-string.js
desktop_light
u-string.js + u-string-desktop_light.js
tablet
u-string.js
tablet_light
u-string.js
smartphone
u-string.js
mobile
not tested
mobile_light
not tested
tv
u-string.js + u-string-desktop_light.js
seo
not supported