associativearray
Version:
102 lines (101 loc) • 3.75 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var existjs_1 = require("existjs");
var pattern_1 = require("./pattern");
var validation_1 = require("./validation");
var AssociativeArray = /** @class */ (function (_super) {
__extends(AssociativeArray, _super);
function AssociativeArray() {
return _super !== null && _super.apply(this, arguments) || this;
}
// Recursive output
AssociativeArray.prototype.stringify = function (tab) {
if (tab === void 0) { tab = ""; }
var trace = "";
for (var i in this.names) {
trace += tab + "[" + this.names[i] + "] ";
if (existjs_1.isObject(this.values[i])) {
if (validation_1.isAssociativeArray(this.values[i])) {
trace += "\n" + this.values[i].stringify(tab + "__ ");
}
else {
trace += JSON.stringify(this.values[i]) + "\n";
}
}
else if (existjs_1.isArray(this.values[i])) {
trace += JSON.stringify(this.values[i]) + "\n";
}
else if (existjs_1.isString(this.values[i])) {
trace += "\"" + this.values[i] + "\"\n";
}
else {
trace += this.values[i] + "\n";
}
}
;
return trace;
};
// Get object (no recursion)
AssociativeArray.prototype.object = function () {
var object = {};
for (var i in this.names) {
object[this.names[i]] = this.values[i];
}
return object;
};
// Import from object (no recursion)
AssociativeArray.prototype.fromObject = function (object) {
for (var name_1 in object) {
this.add(name_1, object[name_1]);
}
};
// Export as array form (recursive)
AssociativeArray.prototype.exportArray = function () {
var complex = [];
for (var i in this.names) {
if (validation_1.isAssociativeArray(this.values[i])) {
var value = this.values[i].exportArray();
var type = "AssociativeArray";
}
else {
var value = this.values[i];
var type = "Object";
}
complex.push({
name: this.names[i],
value: value,
type: type
});
}
return complex;
};
// Import from array form (recursive)
AssociativeArray.prototype.importArray = function (array) {
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
var v = array_1[_i];
if (!existjs_1.exist(v.name) || !existjs_1.exist(v.value) || !existjs_1.exist(v.type)) {
throw "Invalid Array";
}
if (v.type === "AssociativeArray") {
var associativeArray = new AssociativeArray();
associativeArray.importArray(v.value);
this.add(v.name, associativeArray);
}
else {
this.add(v.name, v.value);
}
}
};
return AssociativeArray;
}(pattern_1.Pattern));
exports.AssociativeArray = AssociativeArray;