str

Contains common string utility methods.

Methods


<static> camel( target ) → {string}

Converts the given target to camel case.

Parameters
Name Type Description
target string

The string to camel case.

Returns
Examples

// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str;

console.log( _str.camel( "max-width" ) ); // => "maxWidth"
console.log( _str.camel( "max--width" ) ); // => "maxWidth"
console.log( _str.camel( "max Width" ) ); // => "maxWidth"
console.log( _str.camel( "Max_width" ) ); // => "maxWidth"
console.log( _str.camel( "MaxWidth" ) ); // => "maxWidth"
console.log( _str.camel( "Abbreviations like CSS are left intact" ) ); // => "abbreviationsLikeCSSAreLeftIntact"


<static> contains( target, substr [, ignoreCase ] ) → {boolean}

Checks if the target contains the given substr.

Parameters
Name Type Attributes Default Description
target string

The string to check.

substr string

The string to check for.

ignoreCase boolean <optional>
false

Whether or not to ignore casing when performing the check.

Returns

true if the target contains the given substr.

Examples

// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str,
	// create a string to test
	target = "To be, or not to be, that is the question.";

console.log( _str.contains( target, "To be" ) ); // => true
console.log( _str.contains( target, "question" ) ); // => true
console.log( _str.contains( target, "no" ) ); // => true
console.log( _str.contains( target, "nonexistent" ) ); // => false
console.log( _str.contains( target, "TO BE" ) ); // => false
console.log( _str.contains( target, "TO BE", true ) ); // => true


<static> containsWord( target, word [, ignoreCase ] ) → {boolean}

Checks if the target contains the given word.

Description

This method differs from FooGallery.utils.str.contains in that it searches for whole words by splitting the target string on word boundaries (\b) and then comparing the individual parts.

Parameters
Name Type Attributes Default Description
target string

The string to check.

word string

The word to check for.

ignoreCase boolean <optional>
false

Whether or not to ignore casing when performing the check.

Returns

true if the target contains the given word.

Examples

// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str,
	// create a string to test
	target = "To be, or not to be, that is the question.";

console.log( _str.containsWord( target, "question" ) ); // => true
console.log( _str.containsWord( target, "no" ) ); // => false
console.log( _str.containsWord( target, "NOT" ) ); // => false
console.log( _str.containsWord( target, "NOT", true ) ); // => true
console.log( _str.containsWord( target, "nonexistent" ) ); // => false


<static> endsWith( target, substr ) → {boolean}

Checks if the target ends with the given substr.

Parameters
Name Type Description
target string

The string to check.

substr string

The substr to check for.

Returns

true if the target ends with the given substr.

Examples

// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str;

console.log( _str.endsWith( "something", "g" ) ); // => true
console.log( _str.endsWith( "something", "ing" ) ); // => true
console.log( _str.endsWith( "something", "no" ) ); // => false


<static> escapeRegExp( target ) → {string}

Escapes the target for use in a regular expression.

Parameters
Name Type Description
target string

The string to escape.

Returns

<static> fnv1a( target ) → {number}

Generates a 32 bit FNV-1a hash from the given target.

Parameters
Name Type Description
target string

The string to generate a hash from.

Returns

null if the target is not a string or empty otherwise a 32 bit FNV-1a hash.

Examples

// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str;

console.log( _str.fnv1a( "Some string to generate a hash for." ) ); // => 207568994
console.log( _str.fnv1a( "Some string to generate a hash for" ) ); // => 1350435704


<static> from( target, substr ) → {string}

Returns the remainder of the target split on the first index of the given substr.

Parameters
Name Type Description
target string

The string to split.

substr string

The substring to split on.

Returns

null if the given substr does not exist within the target.

Examples

// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str,
	// create a string to test
	target = "To be, or not to be, that is the question.";

console.log( _str.from( target, "no" ) ); // => "t to be, that is the question."
console.log( _str.from( target, "that" ) ); // => " is the question."
console.log( _str.from( target, "question" ) ); // => "."
console.log( _str.from( target, "nonexistent" ) ); // => null


<static> join( separator, part [, ...partN ] ) → {string}

Joins any number of strings using the given separator.

Description

This method differs from using the standard Array.prototype.join function to join strings in that it ignores empty parts and checks to see if each starts with the supplied separator. If the part starts with the separator it is removed before appending it to the final result.

Parameters
Name Type Attributes Description
separator string

The separator to use to join the strings.

part string

The first string to join.

partN string <optional>
<repeatable>

Any number of additional strings to join.

Returns
Examples

// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str;

console.log( _str.join( "_", "all", "in", "one" ) ); // => "all_in_one"
console.log( _str.join( "_", "all", "_in", "one" ) ); // => "all_in_one"
console.log( _str.join( "/", "http://", "/example.com/", "/path/to/image.png" ) ); // => "http://example.com/path/to/image.png"
console.log( _str.join( "/", "http://", "/example.com", "/path/to/image.png" ) ); // => "http://example.com/path/to/image.png"
console.log( _str.join( "/", "http://", "example.com", "path/to/image.png" ) ); // => "http://example.com/path/to/image.png"


<static> startsWith( target, substr ) → {boolean}

Checks if the target starts with the given substr.

Parameters
Name Type Description
target string

The string to check.

substr string

The substr to check for.

Returns

true if the target starts with the given substr.

Examples

// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str;

console.log( _str.startsWith( "something", "s" ) ); // => true
console.log( _str.startsWith( "something", "some" ) ); // => true
console.log( _str.startsWith( "something", "no" ) ); // => false


<static> until( target, substr ) → {string}

Returns the first part of the target split on the first index of the given substr.

Parameters
Name Type Description
target string

The string to split.

substr string

The substring to split on.

Returns

The target if the substr does not exist.

Examples

// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str,
	// create a string to test
	target = "To be, or not to be, that is the question.";

console.log( _str.until( target, "no" ) ); // => "To be, or "
console.log( _str.until( target, "that" ) ); // => "To be, or not to be, "
console.log( _str.until( target, "question" ) ); // => "To be, or not to be, that is the "
console.log( _str.until( target, "nonexistent" ) ); // => "To be, or not to be, that is the question."


<static> format( target, arg1 [, ...argN ] ) → {string}

A basic string formatter that can use both index and name based placeholders but handles only string or number replacements.

Description

This method allows you to supply the replacements as an object when using named placeholders or as an array or additional arguments when using index placeholders.

This does not perform a simultaneous replacement of placeholders, which is why it's referred to as a basic formatter. This means replacements that contain placeholders within there value could end up being replaced themselves as seen in the last example.

Parameters
Name Type Attributes Description
target string

The format string containing any placeholders to replace.

arg1 string | number | Object | Array

The first value to format the target with. If an object is supplied it's properties are used to match named placeholders. If an array, string or number is supplied it's values are used to match any index placeholders.

argN string | number <optional>
<repeatable>

Any number of additional strings or numbers to format the target with.

Returns

The string formatted with the supplied arguments.

Examples

The following shows how to use index placeholders.


// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str,
	// create a format string using index placeholders
	format = "Hello, {0}, are you feeling {1}?";

console.log( _str.format( format, "Steve", "OK" ) ); // => "Hello, Steve, are you feeling OK?"
// or
console.log( _str.format( format, [ "Steve", "OK" ] ) ); // => "Hello, Steve, are you feeling OK?"

While the above works perfectly fine the downside is that the placeholders provide no clues as to what should be supplied as a replacement value, this is were supplying an object and using named placeholders steps in.


// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str,
	// create a format string using named placeholders
	format = "Hello, {name}, are you feeling {adjective}?";

console.log( _str.format( format, {name: "Steve", adjective: "OK"} ) ); // => "Hello, Steve, are you feeling OK?"

The following demonstrates the issue with not performing a simultaneous replacement of placeholders.


// alias the FooGallery.utils.str namespace
var _str = FooGallery.utils.str;

console.log( _str.format("{0}{1}", "{1}", "{0}") ); // => "{0}{0}"

// If the replacement happened simultaneously the result would be "{1}{0}" but this method executes
// replacements synchronously as seen below:

// "{0}{1}".replace( "{0}", "{1}" )
// => "{1}{1}".replace( "{1}", "{0}" )
// => "{0}{0}"