Show:
Module: proact-core

Contains a set of utility functions to ease working with arrays and objects. Can be reffered by using ProAct.U too.

This class is part of the proact-core module of ProAct.js.

Methods

bind

(
  • ctx
  • func
)
Function

Binds a function to an object context.

Every time the function is called, this will point to the passed object.


 var context = {a: 3};
 var f = ProAct.Utils.bind(context, function () {
   return this;
 });

 var result = f();
 console.log(result === context); // prints 'true'

Parameters:

  • ctx Object

    The context to bind the this of the function to.

  • func Function

    The function to bind.

Returns:

Function:

The bound function.

clone

(
  • obj
)
Object

Clones the passed object. It creates a deep copy of it. For now it clones only arrays.

TODO It is not fully implemented...

Parameters:

  • obj Object

    The object to clone.

Returns:

Object:

Clone of the passed object.

defValProp

(
  • obj
  • prop
  • enumerable
  • configurable
  • writable
  • val
)

Defines a property to an object that contains a initial value.

The property can be configured using the arguments passed if it is possible in the javascript implementation.

Parameters:

  • obj Object

    The object to define a property in.

  • prop String

    The name of the property to define.

  • enumerable Boolean

    If the property should be enumerable.
    In other words visible when doing

    for (p in obj) {}

  • configurable Boolean

    If the property should be configurable.
    In other words if the parameters of the property for example enumerable or writable can be changed in the future.

  • writable Boolean

    If the property can be changed.

  • val Object

    The initial value of the property.

diff

(
  • array1
  • array2
)
Object

A powerful function that creates a diff object containing the differences between two arrays.

Parameters:

Returns:

Object:

The object returned contains a property for every index there is a difference between the passed arrays.

The object set on the index has two array properties : 'o' and 'n'.

The 'o' property represents the owned elemetns of the first array that are different from the other's.

The 'n' property contains all the elements that are not owned by the first array, but present in the other.

Example:

  var array1 = [1, 3, 4, 5],
      array2 = [1, 2, 7, 5, 6]
      diff;

  diff = ProAct.Utils.diff(array1, array2);

  console.log(diff[0]); // undefined - the arrays are the same at he index 0
  console.log(diff[1]); // {o: [3, 4], n: [2, 7]}
  console.log(diff[2]); // undefined the change began from index 1, so it is stored there
  console.log(diff[3]); // undefined - the arrays are the same at index 3
  console.log(diff[4]); // {o: [], n: [6]}

ex

(
  • destination
  • source
)
Object

Extends the destination object with the properties and methods of the source object.

 var obj1 = {a: 3};
 var obj2 = {b: 4;}
 ProAct.Utils.ex(obj2, obj1);

 console.log(obj2);
 // This prints : {a: 3, b: 4}

Parameters:

  • destination Object

    The object to be extended - it will be modified.

  • source Object

    The source holding the properties and the functions to extend destination with.

Returns:

Object:

The changed destination object.

extendClass

(
  • data
)
Object

Used for extending of classes. Example is:


 var Bar = ProAct.Utils.extendClass.call(Foo, {
   a: 1,
   b: 2,
   c: function () {}
 });

Parameters:

  • data Object

    Data to add new properties to the new class or override old ones.

Returns:

Object:

Child class.

isArray

(
  • value
)
Boolean

Checks if the passed value is a valid JavaScript Array instance or not.

Parameters:

  • value Object

    The value to check.

Returns:

Boolean:

True if the passed value is Array.

isEmptyObject

(
  • value
)
Boolean

Checks if the passed value is {} or not.

Parameters:

  • value Object

    The value to check.

Returns:

Boolean:

True if the value is object that has no own fields.

isError

(
  • value
)
Boolean

Checks if the passed value is a valid JavaScript Error instance or not.

Parameters:

  • value Object

    The value to check.

Returns:

Boolean:

True if the passed value is instance of an Error.

isFunction

(
  • value
)
Boolean

Checks if the passed value is a Function or not.

Parameters:

  • value Object

    The object/value to check.

Returns:

Boolean:

True if the passed value is a Function.

isObject

(
  • value
)
Boolean

Checks if the passed value is a JavaScript object or not.

Parameters:

  • value Object

    The value to check.

Returns:

Boolean:

True if the passed values is not primitive.

isString

(
  • value
)
Boolean

Checks if the passed value is a String instance or not.

Parameters:

  • value Object

    The object/value to check.

Returns:

Boolean:

True if the passed value is a String.

remove

(
  • array
  • value
)

Removes the first appearance of the passed value in the passed array. If the value is not present in the passed array does nothing.


 var array = [1, 2, 3];
 ProAct.Utils.remove(array, 2);

 console.log(array); // prints [1, 3]

Parameters:

  • array Array

    The array to remove from.

  • value Object

    The value to be removed.

uuid

() String

Generates an universally unique identifier.

Returns:

String:

Unique string.