@gabrielrufino/cube
Version:
Data structures made in Typescript
110 lines (109 loc) • 3.46 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var Dictionary = /** @class */ (function () {
function Dictionary(inputs) {
if (inputs === void 0) { inputs = {}; }
this._data = {};
this._data = inputs;
}
Object.defineProperty(Dictionary.prototype, "data", {
get: function () {
return __assign({}, this._data);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Dictionary.prototype, "size", {
get: function () {
return Reflect.ownKeys(this.data).length;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Dictionary.prototype, "isEmpty", {
get: function () {
return this.size === 0;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Dictionary.prototype, "keys", {
get: function () {
return Reflect
.ownKeys(this.data)
.map(function (key) { return String(key); });
},
enumerable: false,
configurable: true
});
Object.defineProperty(Dictionary.prototype, "values", {
get: function () {
return Object.values(this.data);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Dictionary.prototype, "pairs", {
get: function () {
return Object.entries(this.data);
},
enumerable: false,
configurable: true
});
Dictionary.prototype.set = function (key, value) {
Reflect.set(this._data, key, value);
return [key, value];
};
Dictionary.prototype.remove = function (key) {
if (this.hasKey(key)) {
var value = Reflect.get(this.data, key);
Reflect.deleteProperty(this._data, key);
return [key, value];
}
return null;
};
Dictionary.prototype.hasKey = function (key) {
return Reflect.has(this.data, key);
};
Dictionary.prototype.get = function (key) {
if (this.hasKey(key)) {
return Reflect.get(this.data, key);
}
return null;
};
Dictionary.prototype.clear = function () {
var dictionary = __assign({}, this.data);
this._data = {};
return dictionary;
};
Dictionary.prototype.forEach = function (func) {
for (var _i = 0, _a = this.pairs; _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], value = _b[1];
func(key, value);
}
};
Dictionary.prototype[Symbol.toPrimitive] = function (type) {
var primitives = {
default: true,
number: this.size,
string: "{ ".concat(this.pairs.map(function (_a) {
var key = _a[0], value = _a[1];
return "".concat(key, " => ").concat(value);
}).join(', '), " }"),
};
return primitives[type];
};
return Dictionary;
}());
exports.default = Dictionary;