bob.utils Namespace (bob.js JavaScript Framework)

 

bob.utils namespace defines utility/helper functions, which can be used for variety of purposes, such as type compatibility check, validation, etc.

You can download bob.js from CodePlex.

 

Covered Topics

 

equals - Function

bob.utils.equals(value, comparison)

Checks if two provided values equal. It compares argument types and argument values.

Parameters:

value - first value of the equality.

comparison - value to compare with.

Returns:

Boolean

Back to Top

 

equalsAny - Function

bob.utils.equalsAny(value, arrComparisons)

Checks if provided value is equal to any item in the provided array/enumerable collection.

Parameters:

value - any value that will be checked for equality.

arrComparisons - enumerable collection of elements. Each item of the collection will be checked for equality with value. This argument can be an array, string, object, or any other type of enumerable collection. Check bob.collections namespace and bob.collections.Enumerator type/constructor for more details.

Returns:

Boolean

Back to Top

 

extendObject - Function

bob.utils.extendObject(source, extension)

Extends source object by copying all members of extension object to the source object.

Parameters:

source - object to extend.

extension - object containing members that will be copied over to source object.

Returns:

Source object. This is for convenience only. You may as well ignore the returned value and use source object passed as the first argument after calling this method.

Example:

var src = { };
var ext = {
    method1: function() {
           console.log('This is method1.');
    }
};

//copy all members of ext to src.
bob.ns.extendObject(src, ext);

//call new members of src.
src.method1();

//output:
// ================
// This is method1.

Back to Top

 

ifNull - Function

bob.utils.ifNull(obj, altValue)

Null coalescing: returns obj if it's not null, otherwise returns altValue.

Parameters:

obj - any value to check for being null.

altValue - alternate value to be returned if obj is null.

Returns:

First or second argument passed to it.

Back to Top

 

ifNullOrWhiteSpace - Function

bob.utils.ifNullOrWhiteSpace(obj, altValue)

Returns altValue if obj is null or whitespace, otherwise returns obj.

Parameters:

obj - any value to check for being null or whitespace (string containing no characters, or containing only spaces).

altValue - alternate value to be returned if obj is null or whitespace.

Returns:

First or second argument passed to it.

Back to Top

 

ifUndefined - Function

bob.utils.ifUndefined(obj, altValue)

Returns altValue if obj is undefined, otherwise returns obj.

Parameters:

obj - any value to check for being undefined.

altValue - alternate value to be returned if obj is undefined.

Returns:

First or second argument passed to it.

Back to Top

 

ifUndefinedOrNull - Function

bob.utils.ifUndefinedOrNull(obj, altValue)

Returns altValue if obj is undefined or null, otherwise returns obj.

Parameters:

obj - any value to check for being undefined or null.

altValue - alternate value to be returned if obj is undefined or null.

Returns:

First or second argument passed to it.

Back to Top

 

isArray - Function

bob.utils.isArray(obj)

Returns Boolean indicating if obj is array.

Parameters:

obj - any value to check for being array.

Returns:

Boolean.

Back to Top

 

isBoolean - Function

bob.utils.isBoolean(obj)

Returns Boolean indicating if obj is Boolean (true or false).

Parameters:

obj - any value to check for being Boolean.

Returns:

Boolean

Back to Top

 

isDate - Function

bob.utils.isDate(obj)

Returns Boolean indicating if obj is of type Date.

Parameters:

obj - any value to check for being date.

Returns:

Boolean.

Back to Top

 

isFunction - Function

bob.utils.isFunction(obj)

Returns Boolean indicating if obj is function.

Parameters:

obj - any value to check for being function.

Returns:

Boolean.

Back to Top

 

isNull - Function

bob.utils.isNull(obj)

Returns Boolean indicating if obj is null.

Parameters:

obj - any value to check for being null.

Returns:

Boolean.

Back to Top

 

isNullOrWhiteSpace - Function

bob.utils.isNullOrWhiteSpace(str, bThrowOnWrongType)

Returns boolean indicating if str is null or is a whitespace string (containing no characters, or containing only spaces).

Parameters:

str - any value to check for being null or whitespace.

bThrowOnWrongType - (optional) Boolean indicating whether to throw an error if str is not null and is not of type string. If you pass true for this argument, isNullOrWhiteSpace function will accept only nulls and strings, which is sometimes preferred behavior.

Returns:

Boolean.

Back to Top

 

isNumber - Function

bob.utils.isNumber(obj)

Returns Boolean indicating if obj is number.

Parameters:

obj - any value to check for being number.

Returns:

Boolean.

Back to Top

 

isObject - Function

bob.utils.isObject(obj, bAcceptUndefined, bAcceptNull)

Returns Boolean indicating if obj is an object (not of any of the known types, custom type).

Parameters:

obj - any value to check for being object.

bAcceptUndefined - (optional) Boolean indicating whether to treat undefined as object. Pass true if obj can be undefined and that's still an object for your scenario. By default undefined is not object.

bAcceptNull - (optional) Boolean indicating whether to treat null as object. Pass true if obj can be null and that's still an object for your scenario. By default null is not object.

Returns:

Boolean.

Back to Top

 

isString - Function

bob.utils.isString(obj)

Returns Boolean indicating if obj is string.

Parameters:

obj - any value to check for being string. Null is not string.

Returns:

Boolean.

Back to Top

 

isUndefined - Function

bob.utils.isUndefined(obj)

Returns Boolean indicating if obj is undefined.

Parameters:

obj - any value to check for being undefined.

Returns:

Boolean.

Back to Top

 

isUndefinedOrNull - Function

bob.utils.isUndefinedOrNull(obj)

Returns Boolean indicating if obj is undefined or null.

Parameters:

obj - any value to check for being undefined.

Returns:

Boolean.

Back to Top

 

Lazy - Type, Constructor

Defines type that encapsulates lazy loading of the value.

Constructor:

new bob.utils.Lazy(fnCreate)

Initializes new instance of Lazy object using given value loader function.

fnCreate - function returning lazy initialized value. This function will be called internally only once when lazy loading is requested. Normally this must be parameter-less function, because Lazy object does not pass parameters when calling it.

Functions:

obj.get_isValueCreated()

Returns Boolean indicating if value was loaded. This is a Get accessor of isValueCreated read-only property.

obj.getValue()

Returns lazy loaded value. This function internally calls fnCreate when it's called first. Next time it returns already existing value.

Example:

var lazy = new bob.utils.Lazy(function() { console.log('Value created.'); return 1; } );
console.log('Calling getValue #1');
console.log(lazy.getValue());
console.log('Calling getValue #2');
console.log(lazy.getValue());
console.log('Calling getValue #3');
console.log(lazy.getValue());

//output:
//==============
// Calling getValue #1
// Value created.
// 1
// Calling getValue #2
// 1
// Calling getValue #3
// 1

Back to Top

 

lazyFunction - Function

bob.utils.lazyFunction(fnCreate)

Returns function which calls given loader function only once. This can be treated as a conversion from simple to cached function call.

Parameters:

fnCreate - function returning lazy initialized value. This function will be called internally when lazy loading is requested. Normally this must be parameter-less function, because lazy function does not pass parameters when calling it.

Returns:

Lazy loader function.

Example:

var lazyFn = bob.utils.lazyFunction(function() { console.log('Value created.'); return 1; } );
console.log('Calling function #1');
console.log(lazyFn());
console.log('Calling function #2');
console.log(lazyFn());
console.log('Calling function #3');
console.log(lazyFn());

//output:
//==============
// Calling function #1
// Value created.
// 1
// Calling function #2
// 1
// Calling function #3
// 1

Back to Top

 

quote - Function

bob.utils.quote(sValue)

Returns string containing sValue surrounded by double quotes (").

Parameters:

sValue - any value that will be surrounded with double quotes.

Returns:

String.

Back to Top

 

See Also

bob.js project page on CodePlex.

Last edited Oct 11, 2012 at 12:58 PM by ttutisani, version 2

Comments

No comments yet.