sparkscript
Version:
DiamondFire for Javascript.
64 lines • 2.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.valueMapper = exports.blockMapper = void 0;
const mapperUtils_1 = require("./common/mapperUtils");
function blockMapper(type, serializedData) {
if (typeof serializedData !== "object" || !("args" in serializedData) || !("block" in serializedData))
throw new Error("Cannot map serialized block data because it is invalid.");
const args = serializedData.args.items.map((i) => valueMapper(i.item.id, i));
const constructor = mapperUtils_1.blockMap[type];
if (!constructor)
throw new Error(`Type "${type}" cannot be recongized as a DiamondFire block type. Template may be corrupted or just invalid.`);
return constructor(serializedData, args);
}
exports.blockMapper = blockMapper;
function valueMapper(type, serializedData) {
if (typeof serializedData !== "object" || !("slot" in serializedData) || !("item" in serializedData))
throw new Error("Cannot map serialized value data because it is invalid.");
const constructor = mapperUtils_1.valueMap[type];
if (!constructor)
throw new Error(`Type "${type}" cannot be recongized as a DiamondFire value type. Template may be corrupted or just invalid.`);
return constructor({
v: serializedData.item.data,
s: serializedData.slot
});
}
exports.valueMapper = valueMapper;
/**
* ## Sparkscript global mapper.
* Convert DiamondFire's raw value or codeblock data to the respective sparkscript's class instance.
* @author UserUNP
*
* @param type Codename type to be mapped.
* @param serializedData Respective data for the type.
* @returns Converted sparkscript object instance.
*/
const mapper = (type, serializedData) => {
if (typeof serializedData !== "object")
throw new Error(`Cannot map a variable with type ${typeof serializedData}.`);
if (isOfTypeRawValue(serializedData))
return valueMapper(type, serializedData);
else
return blockMapper(type, serializedData);
};
function isOfTypeRawValue(test) {
if (!test || typeof test !== "object")
return false;
if (!("item" in test) || !("slot" in test))
return false;
return true;
}
/**
* Transform serialized data into readable sparkscript data.
* @description Most type intellisense is lost due to abstraction.
* @param raw Raw action codeblock data.
* @returns New instance of the respective sparkscript class.
*/
mapper.from = (raw) => {
if (isOfTypeRawValue(raw))
return mapper(raw.item.id, raw);
else
return mapper(raw.block, raw);
};
exports.default = mapper;
//# sourceMappingURL=mapper.js.map