ConstraintJS - v0.10.2
    Preparing search index...

    Class MapConstraint<K, V>

    Note: the preferred way to create a map constraint is with cjs({...}) or cjs.map(options).

    Emulates a JavaScript object (or Map), but with constraints. Its entries are ordered, and its keys can be anything (compared with === unless an equals option is given).

    var map = cjs({ x: 1 });
    var x_plus_one = cjs(function() { return map.get("x") + 1; });
    x_plus_one.get(); // 2
    map.put("x", 10);
    x_plus_one.get(); // 11

    Type Parameters

    • K = any
    • V = any
    Index
    BREAK: object = BREAK

    Return this from a forEach callback to stop iterating.

    • Removes every entry.

      Parameters

      • Optionalsilent: boolean

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

      Returns this

      var map = cjs({x: 1, y: 2});
      map.clear();
      map.isEmpty(); // true
    • Clears this map and cleans up its constraints.

      Parameters

      • Optionalsilent: boolean

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

      Returns void

    • Every entry, in order, as {key, value} objects.

      Returns { key: K; value: V }[]

      var map = cjs({x: 1, y: 2});
      map.entries(); // [{key:'x',value:1}, {key:'y',value:2}]
    • Calls callback with the value, key, and index of every entry, in order. Return cjs.MapConstraint.BREAK from it to stop.

      Parameters

      • callback: (this: any, value: V, key: K, index: number) => unknown
      • thisArg: unknown = ...

        The this for callback (default: this map constraint)

      Returns this

      var map = cjs({x:1,y:2,z:3});
      map.forEach(function(val, key) {
      console.log(key+':'+val);
      if(key === 'y') {
      return cjs.MapConstraint.BREAK;
      }
      }); // x:1 ... y:2
    • The value for key (like this[key]), or undefined.

      Parameters

      • key: K

      Returns V | undefined

      var map = cjs({x: 1, y: 2});
      map.get("x"); // 1
    • The value for key; if there isn't one, sets it to create(key) first.

      Parameters

      • key: K
      • create: (this: any, key: K) => V
      • Optionalcontext: unknown

        The this for create (default: this map constraint)

      • Optionalindex: number

        Where to put a new entry (default: at the end)

      • Optionalliteral: boolean

        For a new entry, whether to treat a function value as the value itself

      Returns V

      var map = cjs({x: 1, y: 2});
      map.getOrPut('z', function() {
      console.log("evaluating");
      return 3;
      }); // output: 'evaluating'
      // 3
      map.getOrPut('z', function() {
      console.log("evaluating");
      return 3;
      }); // (no output)
      // 3
    • Whether there is an entry for key.

      Parameters

      • key: K

      Returns boolean

      var map = cjs({x: 1, y: 2});
      map.has('x'); // true
    • The index of the entry for key, or -1.

      Parameters

      • key: K

      Returns number

      var map = cjs({x: 1, y: 2});
      map.indexOf('y'); // 1
      map.indexOf('z'); // -1
    • Whether there are no entries.

      Returns boolean

      var map = cjs({x: 1, y: 2});
      map.isEmpty(); // false
    • With no arguments: a plain object of the entries (see toObject). With key: the value for key. With key and value (and optionally index): sets the value for key (see put).

      Returns Record<string, V>

      var map = cjs({x: 1, y: 2});
      map.item(); // {x:1,y:2}
      map.item('x'); // 1
      map.item('z', 3);
      map.keys(); // ['x','y','z']
    • With no arguments: a plain object of the entries (see toObject). With key: the value for key. With key and value (and optionally index): sets the value for key (see put).

      Parameters

      • key: K

      Returns V | undefined

      var map = cjs({x: 1, y: 2});
      map.item(); // {x:1,y:2}
      map.item('x'); // 1
      map.item('z', 3);
      map.keys(); // ['x','y','z']
    • With no arguments: a plain object of the entries (see toObject). With key: the value for key. With key and value (and optionally index): sets the value for key (see put).

      Parameters

      • key: K
      • value: V
      • Optionalindex: number

      Returns this

      var map = cjs({x: 1, y: 2});
      map.item(); // {x:1,y:2}
      map.item('x'); // 1
      map.item('z', 3);
      map.keys(); // ['x','y','z']
    • A constraint whose value is the value for key (which can itself be a constraint).

      Parameters

      Returns Constraint<V | undefined>

      var map = cjs({x: 1, y: 2});
      var x_val = map.itemConstraint('x');
      x_val.get(); // 1
      map.item('x', 3);
      x_val.get(); // 3
    • The key of an entry whose value is value (or undefined).

      Parameters

      • value: V
      • OptionalequalityCheck: EqualityCheck<V>

        How to compare values (default: this map's value equality check)

      Returns K | undefined

      var map = cjs({x: 1, y: 2, z: 3});
      map.keyForValue(1); // 'x'
    • The keys, in order.

      Returns K[]

      var map = cjs({x: 1, y: 2});
      map.keys(); // ['x','y']
    • Moves the entry for key to index.

      Parameters

      • key: K
      • index: number

      Returns this

      var map = cjs({x: 1, y: 2, z: 3});
      map.move('z', 0);
      map.keys(); // ['z','x','y']
    • Moves the entry at oldIndex to newIndex.

      Parameters

      • oldIndex: number
      • newIndex: number

      Returns this

      var map = cjs({x: 1, y: 2, z: 3});
      map.moveIndex(1, 0);
      map.keys(); // ['y','x','z']
    • Sets the value for key (like this[key] = value).

      Parameters

      • key: K
      • value: V
      • Optionalindex: number

        Where to put the entry (default: at the end for a new key; unchanged for an existing one)

      • Optionalliteral: boolean

        For a new key, whether to treat a function value as the value itself

      Returns this

      var map = cjs({x: 1, y: 2});
      map.put("z", 3, 1);
      map.keys(); // ['x','z','y']
    • Removes the entry for key (like delete this[key]).

      Parameters

      • key: K
      • Optionalsilent: boolean

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

      Returns this

      var map = cjs({x: 1, y: 2});
      map.remove("x");
      map.keys(); // ['y']
    • Change how values are hashed (see the valuehash option); false turns value hashing off.

      Parameters

      Returns this

    • The number of entries.

      Returns number

      var map = cjs({x: 1, y: 2});
      map.size(); // 2
    • A plain object with this map's entries.

      Parameters

      • keyMap: (key: K) => PropertyKey = ...

        Converts each key to a property name (default: the key itself)

      Returns Record<string, V>

      var map = cjs({x: 1, y: 2, z: 3});
      map.toObject(); // {x:1,y:2,z:3}
    • The values, in order.

      Returns V[]

      var map = cjs({x: 1, y: 2});
      map.values(); // [1,2]