Skip to content

ds.predicates

metmajer edited this page Mar 16, 2013 · 3 revisions

The ds.predicates module is a collection of functions which indicate whether a particular predicate (property) holds true for a particular object.

isArrayOf(object, predicate, [length=true])

Checks if object is an array and predicate holds true for all its elements.

Examples:

ds.predicates.isArrayOf([0, 1, 2], ds.predicates.isNumber);
// => true

ds.predicates.isArrayOf([0, 1, 2], ds.predicates.isNegativeNumber);
// => false

ds.predicates.isArrayOf([], ds.predicates.isNumber);
// => false

ds.predicates.isArrayOf([], ds.predicates.isNumber, false);
// => true

ds.predicates.isArrayOf([0, 1, 2], ds.predicates.isPositiveNumber, 3);
// => true

ds.predicates.isArrayOf([0, 1, 2], ds.predicates.isPositiveNumber, 4);
// => false

Params:

  • * object The object to check.

  • Function:Boolean predicate A predicate which is evaluated for each array element.

  • Boolean|Number [length=true] Either a boolean indicating whether the array must be non-empty (true), may be empty (false), or a number indicating the number of elements the array must have.

Returns:

  • Boolean Returns true if object is an array and predicate holds true for all its elements, and the length condition holds true, otherwise returns false.

Throws:

  • Error Throws if predicate is not a function.

isDate(object)

Checks if object is a date.

Params:

  • * object The object to check.

Returns:

  • Boolean Returns true if object is a date, otherwise returns false.

isFunction(object, [length=true])

Checks if object is a function.

Params:

  • * object The object to check.

  • Boolean|Number [length=true] Either a boolean indicating whether the function must expect arguments (true), may expect no arguments (false), or a number indicating the number of arguments the function must expect.

Returns:

  • Boolean Returns true if object is a function and the length condition holds true, otherwise returns false.

isInteger(object)

Checks if object is an integer number.

Params:

  • * object The object to check.

Returns:

  • Boolean Returns true if object is an integer number, else returns false.

Checks if object is a negative integer number.

Params:

  • * object The object to check.

Returns:

  • Boolean Returns true if object is an integer number < 0, else returns false.

Checks if object is a negative number.

Params:

  • * object The object to check.

Returns:

  • Boolean Returns true if object is a number < 0, otherwise returns false.

isNumber(object)

Checks if object is a number.

Params:

  • * object The object to check.

Returns:

  • Boolean Returns true if object is a number, otherwise returns false.

isPositiveInteger(object, [withZero=true])

Checks if object is a positive integer number.

Params:

  • * object The object to check.

  • Boolean [withZero=true] Indicates if zero is a valid positive number or not.

Returns:

  • Boolean Returns true if object is an integer number >= 0 or > 0, depending on withZero, otherwise returns false.

Throws:

  • Error Throws if withZero is provided and is not a boolean.

isPositiveNumber(object, [withZero=true])

Checks if object is a positive number.

Params:

  • * object The object to check.

  • Boolean [withZero=true] Indicates if zero is a valid positive number or not.

Returns:

  • Boolean Returns true if object is a number >= 0 or > 0, depending on withZero, otherwise returns false.

Throws:

  • Error Throws if withZero is provided and is not a boolean.