all files / TypeScript.NET/source/System/Collections/Dictionaries/ DictionaryBase.js

73.6% Statements 92/125
51.02% Branches 25/49
79.31% Functions 23/29
75.42% Lines 89/118
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                      24×     18× 18×   18×   12× 12× 12×     13× 13× 11× 11× 11×                           84×   84× 84×           84×   102× 102× 102× 102× 102× 102×   102× 102×   146×                     18×                           24× 24× 24×                 28× 28× 24× 24× 24×            
/*!
 * @author electricessence / https://github.com/electricessence/
 * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
 */
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
(function (factory) {
    Eif (typeof module === 'object' && typeof module.exports === 'object') {
        var v = factory(require, exports); Iif (v !== undefined) module.exports = v;
    }
    else if (typeof define === 'function' && define.amd) {
        define(["require", "exports", "../../Compare", "../Enumeration/Enumerator", "../CollectionBase", "../Enumeration/EnumeratorBase", "../../Exceptions/ArgumentNullException", "../../Exceptions/InvalidOperationException", "../../KeyValueExtract"], factory);
    }
})(function (require, exports) {
    "use strict";
    var Compare_1 = require("../../Compare");
    var Enumerator_1 = require("../Enumeration/Enumerator");
    var CollectionBase_1 = require("../CollectionBase");
    var EnumeratorBase_1 = require("../Enumeration/EnumeratorBase");
    var ArgumentNullException_1 = require("../../Exceptions/ArgumentNullException");
    var InvalidOperationException_1 = require("../../Exceptions/InvalidOperationException");
    var KeyValueExtract_1 = require("../../KeyValueExtract");
    var VOID0 = void (0);
    var DictionaryBase = (function (_super) {
        __extends(DictionaryBase, _super);
        function DictionaryBase(source) {
            _super.call(this, source);
        }
        DictionaryBase.prototype._onValueModified = function (key, value, old) {
        };
        DictionaryBase.prototype._addInternal = function (item) {
            var _this = this;
            Iif (!item)
                throw new ArgumentNullException_1.ArgumentNullException('item', 'Dictionaries must use a valid key/value pair. \'' + item + '\' is not allowed.');
            return KeyValueExtract_1.extractKeyValue(item, function (key, value) { return _this.addByKeyValue(key, value); });
        };
        DictionaryBase.prototype._clearInternal = function () {
            var _ = this, count = 0;
            for (var _i = 0, _a = _.keys; _i < _a.length; _i++) {
                var key = _a[_i];
                Eif (_.removeByKey(key))
                    count++;
            }
            return count;
        };
        DictionaryBase.prototype.contains = function (item) {
            var _this = this;
            if (!item || !this.getCount())
                return false;
            return KeyValueExtract_1.extractKeyValue(item, function (key, value) {
                var v = _this.getValue(key);
                return Compare_1.areEqual(value, v);
            });
        };
        DictionaryBase.prototype._removeInternal = function (item) {
            var _this = this;
            Iif (!item)
                return 0;
            return KeyValueExtract_1.extractKeyValue(item, function (key, value) {
                var v = _this.getValue(key);
                return (Compare_1.areEqual(value, v) && _this.removeByKey(key))
                    ? 1 : 0;
            });
        };
        Object.defineProperty(DictionaryBase.prototype, "keys", {
            get: function () { return this.getKeys(); },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(DictionaryBase.prototype, "values", {
            get: function () { return this.getValues(); },
            enumerable: true,
            configurable: true
        });
        DictionaryBase.prototype.addByKeyValue = function (key, value) {
            Iif (value === VOID0)
                throw new InvalidOperationException_1.InvalidOperationException("Cannot add 'undefined' as a value.");
            var _ = this;
            Iif (_.containsKey(key)) {
                var ex = new InvalidOperationException_1.InvalidOperationException("Adding a key/value when the key already exists.");
                ex.data['key'] = key;
                ex.data['value'] = value;
                throw ex;
            }
            return _.setValue(key, value);
        };
        DictionaryBase.prototype.setValue = function (key, value) {
            var _ = this;
            _.assertModifiable();
            var changed = false, old = _.getValue(key);
            Eif (!Compare_1.areEqual(value, old) && _._setValueInternal(key, value)) {
                changed = true;
                _._onValueModified(key, value, old);
            }
            _._signalModification(changed);
            return changed;
        };
        DictionaryBase.prototype.containsKey = function (key) {
            return !!this._getEntry(key);
        };
        DictionaryBase.prototype.containsValue = function (value) {
            var e = this.getEnumerator(), equal = Compare_1.areEqual;
            while (e.moveNext()) {
                if (equal(e.current, value, true)) {
                    e.dispose();
                    return true;
                }
            }
            return false;
        };
        DictionaryBase.prototype.removeByKey = function (key) {
            return this.setValue(key, VOID0);
        };
        DictionaryBase.prototype.removeByValue = function (value) {
            var _ = this, count = 0, equal = Compare_1.areEqual;
            for (var _i = 0, _a = _.getKeys(); _i < _a.length; _i++) {
                var key = _a[_i];
                if (equal(_.getValue(key), value, true)) {
                    _.removeByKey(key);
                    count++;
                }
            }
            return count;
        };
        DictionaryBase.prototype.importEntries = function (pairs) {
            return _super.prototype.importEntries.call(this, pairs);
        };
        DictionaryBase.prototype._importEntries = function (pairs) {
            var _ = this;
            Eif (!pairs)
                return 0;
            var changed = 0;
            Enumerator_1.forEach(pairs, function (pair) { return KeyValueExtract_1.extractKeyValue(pair, function (key, value) {
                if (_._setValueInternal(key, value))
                    changed++;
            }); });
            return changed;
        };
        DictionaryBase.prototype.getEnumerator = function () {
            var _ = this;
            var ver, keys, len, i = 0;
            return new EnumeratorBase_1.EnumeratorBase(function () {
                ver = _._version;
                keys = _.getKeys();
                len = keys.length;
            }, function (yielder) {
                _.assertVersion(ver);
                while (i < len) {
                    var key = keys[i++], value = _.getValue(key);
                    Eif (value !== VOID0)
                        return yielder.yieldReturn({ key: key, value: value });
                }
                return yielder.yieldBreak();
            });
        };
        return DictionaryBase;
    }(CollectionBase_1.CollectionBase));
    exports.DictionaryBase = DictionaryBase;
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = DictionaryBase;
});
//# sourceMappingURL=DictionaryBase.js.map