@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
83 lines (82 loc) • 2.22 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Variable
* @description Map
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SandMap = void 0;
const sand_list_1 = require("./sand-list");
const variable_1 = require("./variable");
class SandMap {
static fromScratch() {
return new SandMap({});
}
static fromRawRecord(record) {
const variablesRecord = {};
Object.keys(record).forEach((key) => {
variablesRecord[key] = variable_1.Variable.mutable(record[key]);
});
return new SandMap(variablesRecord);
}
constructor(record) {
const parsedMap = new Map();
Object.keys(record).forEach((key) => {
parsedMap.set(key, record[key]);
});
this._map = parsedMap;
}
get map() {
const map = {};
this._map.forEach((value, key) => map[key] = value.get());
return map;
}
keys() {
return [...this._map.keys()];
}
has(key) {
return this._map.has(key);
}
get(key) {
const variable = this._map.get(key);
return variable
? variable.get()
: undefined;
}
set(key, value) {
const variable = variable_1.Variable.mutable(value);
this._map.set(key, variable);
return this;
}
concat(map) {
map.keys().forEach((key) => {
this.set(key, map.get(key));
});
return this;
}
getRaw(key) {
return this._map.get(key);
}
clone() {
const map = new SandMap({});
this._map.forEach((value, key) => {
const actualValue = value.get();
if (actualValue instanceof SandMap) {
map.set(key, actualValue.clone());
}
else if (actualValue instanceof sand_list_1.SandList) {
map.set(key, actualValue.clone());
}
else {
map.set(key, value.get());
}
});
return map;
}
toString() {
const obj = {};
this._map.forEach((value, key) => obj[key] = value);
return JSON.stringify(obj, null, 2);
}
}
exports.SandMap = SandMap;