ConstraintJS - v0.10.2
    Preparing search index...

    Class ArrayConstraint<T>

    Note: the preferred way to create an array constraint is with cjs([...]) or cjs.array(options).

    Emulates a standard array, but with constraints: its methods (push, pop, slice, ...) keep track of dependencies, so that a constraint that reads an item is only updated when that item (or, for methods like forEach, the length) changes.

    x[1] = y[2] + z[3]  ===  x.item(1, y.item(2) + z.item(3))
    
    var arr = cjs([1, 2, 3]);
    var sum = cjs(function() {
    return arr.toArray().reduce(function(a, b) { return a + b; }, 0);
    });
    sum.get(); // 6
    arr.push(4);
    sum.get(); // 10

    Type Parameters

    • T = any
    Index
    BREAK: object = BREAK

    Return this from a forEach callback to stop iterating.

    • A new (plain) array of these items followed by values. Array (and array constraint) arguments are spread into the result.

      Parameters

      • ...values: unknown[]

      Returns unknown[]

      var arr1 = cjs(['a','b','c']),
      arr2 = cjs(['x']);
      arr1.concat(arr2); // ['a','b','c','x']
    • Clears this array and cleans up its constraints.

      Parameters

      • Optionalsilent: boolean

        If true, don't invalidate the constraints that depended on it

      Returns void

    • Whether filter returns a truthy value for every item.

      Parameters

      • filter: (this: any, value: T, index: number) => unknown
      • OptionalthisArg: unknown

      Returns boolean

      var arr = cjs([2,4,6]);
      arr.every(function(x) { return x%2===0; }); // true
    • A (plain) array of the items that pass filter.

      Parameters

      • filter: (value: T, index: number, array: T[]) => unknown
      • OptionalthisArg: unknown

      Returns T[]

    • Calls callback once per item. Return cjs.ArrayConstraint.BREAK from it to stop.

      Parameters

      • callback: (this: any, value: T, index: number) => unknown
      • thisArg: unknown = globalThis

        The this for callback (default: the global object)

      Returns this

      var arr = cjs(['a','b','c']);
      arr.forEach(function(val, i) {
      console.log(val);
      if(i === 1) {
      return cjs.ArrayConstraint.BREAK;
      }
      }); // 'a' ... 'b'
    • The index of the first item equal to item, or -1.

      Parameters

      • item: T
      • OptionalequalityCheck: EqualityCheck<T>

        How to compare items (default: this array's equality check)

      Returns number

      var arr = cjs(['a','b','a']);
      arr.indexOf('a'); // 0
    • The index of the first item for which filter returns a truthy value, or -1.

      Parameters

      • filter: (this: any, value: T, index: number) => unknown
      • thisArg: unknown = ...

        The this for filter (default: this array constraint)

      Returns number

      var arr = cjs(['a','b','b']);
      arr.indexWhere(function(val, i) {
      return val === 'b';
      }); // 1
    • With no arguments: a plain array of the items (see toArray). With index: the item at index. With index and value: sets the item at index and returns value.

      Returns T[]

      var arr = cjs(['a','b']);
      arr.item(); // ['a','b']
      arr.item(0); // 'a'
      arr.item(0, 'x');
      arr.toArray(); // ['x','b']
    • With no arguments: a plain array of the items (see toArray). With index: the item at index. With index and value: sets the item at index and returns value.

      Parameters

      • index: number

      Returns T | undefined

      var arr = cjs(['a','b']);
      arr.item(); // ['a','b']
      arr.item(0); // 'a'
      arr.item(0, 'x');
      arr.toArray(); // ['x','b']
    • With no arguments: a plain array of the items (see toArray). With index: the item at index. With index and value: sets the item at index and returns value.

      Parameters

      • index: number
      • value: T

      Returns T

      var arr = cjs(['a','b']);
      arr.item(); // ['a','b']
      arr.item(0); // 'a'
      arr.item(0, 'x');
      arr.toArray(); // ['x','b']
    • A constraint whose value is the item at index (which can itself be a constraint).

      Parameters

      Returns Constraint<T | undefined>

      var arr = cjs(['a','b','c']);
      var first_item = arr.itemConstraint(0);
      first_item.get(); // 'a'
      arr.item(0,'x');
      first_item.get(); // 'x'
    • The items joined into a string, separated by separator (default: ",").

      Parameters

      • Optionalseparator: string

      Returns string

    • The index of the last item equal to item, or -1.

      Parameters

      • item: T
      • OptionalequalityCheck: EqualityCheck<T>

        How to compare items (default: this array's equality check)

      Returns number

      var arr = cjs(['a','b','a']);
      arr.lastIndexOf('a'); // 2
    • The index of the last item for which filter returns a truthy value, or -1.

      Parameters

      • filter: (this: any, value: T, index: number) => unknown
      • thisArg: unknown = ...

        The this for filter (default: this array constraint)

      Returns number

      var arr = cjs(['a','b','a']);
      arr.lastIndexWhere(function(val, i) {
      return val === 'a';
      }); // 2
    • The number of items.

      Returns number

      var arr = cjs(['a','b']);
      arr.length(); // 2
    • A new (plain) array with the results of calling callback on every item.

      Type Parameters

      • R

      Parameters

      • callback: (this: any, value: T, index: number) => R
      • thisArg: unknown = globalThis

      Returns R[]

      var arr = cjs([1,2,3]);
      arr.map(function(x) { return x+1; }); // [2,3,4]
    • Removes the last item and returns it (or undefined if the array is empty).

      Returns T | undefined

      var arr = cjs(['a','b']);
      arr.pop(); // 'b'
      arr.toArray(); // ['a']
    • Appends items to the end and returns the new length.

      Parameters

      • ...values: T[]

      Returns number

      var arr = cjs(['a','b']);
      arr.push('c','d'); // 4
      arr.toArray(); // ['a','b','c','d']
    • A reversed (plain) copy of the items. This array constraint is not modified.

      Returns T[]

    • Replaces every item.

      Parameters

      • values: readonly T[]

      Returns this

      var arr = cjs([1,2,3]);
      arr.setValue(['a','b','c']);
      arr.toArray(); //['a','b','c']
    • Removes the first item and returns it.

      Returns T | undefined

      var arr = cjs(['a','b','c']);
      arr.shift(); // 'a'
      arr.toArray(); //['b','c']
    • A (plain) array of the items from begin up to (but not including) end. Like Array.prototype.slice, negative indices count back from the end.

      Parameters

      • Optionalbegin: number
      • Optionalend: number

      Returns T[]

      var arr = cjs(['a','b','c']);
      arr.slice(1); // ['b','c']
    • Whether filter returns a truthy value for any item.

      Parameters

      • filter: (this: any, value: T, index: number) => unknown
      • OptionalthisArg: unknown

      Returns boolean

      var arr = cjs([1,3,5]);
      arr.some(function(x) { return x%2===0; }); // false
    • A sorted (plain) copy of the items. This array constraint is not modified.

      Parameters

      • OptionalcompareFunction: (a: T, b: T) => number

      Returns T[]

    • Removes howMany items starting at index, inserts items in their place, and returns the removed items. If index is past the end, items are appended.

      Parameters

      • index: number
      • OptionalhowMany: number

        The number of items to remove (default: 0)

      • ...items: T[]

      Returns T[]

      var arr = cjs(['a','b','c']);
      arr.splice(0, 2, 'x', 'y'); // ['a','b']
      arr.toArray(); // ['x','y','c']
    • A plain array of the items.

      Returns T[]

      var arr = cjs(['a','b']);
      arr.toArray(); // ['a', 'b']
    • The items as a string, like Array.prototype.toString.

      Returns string

    • Adds items to the beginning and returns the new length.

      Parameters

      • ...items: T[]

      Returns number

      var arr = cjs(['a','b','c']);
      arr.unshift('x','y'); // 5
      arr.toArray(); //['x','y','a','b','c']