@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
92 lines (91 loc) • 2.68 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Utilities_1 = __importDefault(require("../core/Utilities"));
const Anchor_1 = __importDefault(require("./Anchor"));
class AnchorSet {
data = {};
anchors = {};
clear(anchorName) {
const exists = this.data[anchorName] !== undefined || this.anchors[anchorName] !== undefined;
if (Utilities_1.default.isUsableAsObjectKey(anchorName)) {
this.data[anchorName] = undefined;
this.anchors[anchorName] = undefined;
}
return exists;
}
clearAll() {
this.data = {};
this.anchors = {};
}
getLength() {
let count = 0;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const strKey in this.data) {
count++;
}
return count;
}
getKeys() {
const keyArr = [];
for (const strKey in this.data) {
keyArr.push(strKey);
}
return keyArr;
}
get(name) {
if (!Utilities_1.default.isUsableAsObjectKey(name)) {
throw new Error();
}
if (this.anchors[name]) {
return this.anchors[name];
}
if (this.data[name]) {
this.anchors[name] = new Anchor_1.default(this.data[name]);
return this.anchors[name];
}
return undefined;
}
ensure(name, from, to) {
let anchor;
if (!Utilities_1.default.isUsableAsObjectKey(name)) {
throw new Error();
}
if (this.anchors[name]) {
anchor = this.anchors[name];
}
if (!anchor && this.data[name]) {
this.anchors[name] = new Anchor_1.default(this.data[name]);
anchor = this.anchors[name];
}
if (!anchor) {
this.data[name] = {
from: from,
to: to,
name: name,
};
this.anchors[name] = new Anchor_1.default(this.data[name]);
anchor = this.anchors[name];
}
if (anchor) {
anchor.from = from;
anchor.to = to;
anchor.name = name;
}
return anchor;
}
getAsString() {
return JSON.stringify(this.data);
}
fromString(incomingStr) {
try {
this.data = JSON.parse(incomingStr);
}
catch (e) { }
}
}
exports.default = AnchorSet;