ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
102 lines (101 loc) • 3.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Es5PropSaver_1 = require("../Es5PropSaver");
function createHashSet() {
if (typeof Set !== "undefined")
return new Set();
return new Es5HashSet();
}
exports.createHashSet = createHashSet;
var Es5HashSet = /** @class */ (function () {
function Es5HashSet() {
this.items = {};
this.propSaver = new Es5PropSaver_1.Es5PropSaver();
this.currentId = 0;
}
Es5HashSet.prototype.has = function (value) {
var key = this.getKey(value);
if (key == null)
return false;
return this.items[key] != null;
};
Es5HashSet.prototype.add = function (value) {
if (this.has(value))
return;
this.items[this.generateNewKey(value)] = value;
};
Es5HashSet.prototype.delete = function (value) {
var key = this.getKey(value);
if (key == null || this.items[key] == null)
return false;
delete this.items[key];
return true;
};
Es5HashSet.prototype.clear = function () {
var e_1, _a;
try {
for (var _b = tslib_1.__values(Object.keys(this.items)), _c = _b.next(); !_c.done; _c = _b.next()) {
var key = _c.value;
delete this.items[key];
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
Es5HashSet.prototype.values = function () {
var e_2, _a, _b, _c, key, e_2_1;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
_d.trys.push([0, 5, 6, 7]);
_b = tslib_1.__values(Object.keys(this.items)), _c = _b.next();
_d.label = 1;
case 1:
if (!!_c.done) return [3 /*break*/, 4];
key = _c.value;
return [4 /*yield*/, this.items[key]];
case 2:
_d.sent();
_d.label = 3;
case 3:
_c = _b.next();
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 7];
case 5:
e_2_1 = _d.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 7];
case 6:
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
}
});
};
Es5HashSet.prototype.generateNewKey = function (value) {
var id;
if (typeof value === "string" || typeof value === "number")
id = value.toString();
else {
id = (this.currentId++).toString();
this.propSaver.set(value, id);
}
return id;
};
Es5HashSet.prototype.getKey = function (value) {
if (typeof value === "string" || typeof value === "number")
return value.toString();
return this.propSaver.get(value);
};
return Es5HashSet;
}());
exports.Es5HashSet = Es5HashSet;