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

Return values

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..."

Dependencies

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

Return values

A random string consisting of letters

Examples

u.randomString(10);

returns String like mFrHGruzzo

Dependencies

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

Return values

A unique UUID string

Examples

Util.uuid();

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

Dependencies

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".

Return values

A string padded to length using prefix

Examples

Util.prefix(1, 2);

returns String 01

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

returns String ----F

Dependencies

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

Return values

Either either (if not undefined or null) or or

Examples

u.eitherOr(0, "zero");

returns 0

u.eitherOr(, "zero");

returns zero

Dependencies

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

Return values

String string with first letter in uppercase

Examples

u.ucfirst("martin")

Returns "Martin"

Dependencies

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

Return values

String string with first letter in lowercase

Examples

u.lcfirst("ParentNode")

Returns "parentNode"

Dependencies

JavaScript
  • String.toLowerCase
  • String.replace
Manipulator

none

Util.normalize

Definition

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

Description

Normalizes string by replacing known special chars with a-z equivalent, ie. Æ becomes AE.

Parameters

string
String String to normalize

Return values

Normalized string

Examples

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

returns Koebenhavn er saa laekker

Dependencies

JavaScript

RegExp

String.replace

Manipulator

none

Util.superNormalize

Definition

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

Description

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

Parameters

string
String String to superNormalize

Return values

Normalized string

Examples

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

returns koebenhavn-er-saa-laekker

Dependencies

JavaScript
  • String.toLowerCase
  • String.replace
Manipulator
  • u.normalize
  • u.stripTags

Util.stripTags

Definition

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

Description

Remove any HTML tags from string.

Parameters

string
String String to strip tags from

Return values

String without any HTML tags

Examples

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

returns København er så lækker

Dependencies

JavaScript
  • document.createElement
Manipulator
  • u.text

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.

Return values

Quantity specific string, pluralized if needed.

Examples

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

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

Dependencies

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

Return values

Mixed JSON object if found, else false

Examples

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

Returns JSON object.

Util.isStringJSON('manipulator');

Returns false.

Dependencies

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

Return values

Mixed DOM object if found, else false

Examples

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

Returns HTML object.

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

Returns false.

Dependencies

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

Return values

Trimmed string

Examples

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

returns test

Dependencies

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

Return values

Substring of string

Examples

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

returns in

("string").substr(4);

returns ng

Dependencies

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