
js.prompto.value.Dictionary.js Maven / Gradle / Ivy
var Value = require("./Value").Value;
var NullValue = require("./NullValue").NullValue;
var SetValue = require("./SetValue").SetValue;
var ListValue = require("./ListValue").ListValue;
var Text = require("./Text").Text;
var Integer = require("./Integer").Integer;
var InternalError = require("../error/InternalError").InternalError;
var BaseType = require("../type/BaseType").BaseType;
var DictType = require("../type/DictType").DictType;
var TextType = require("../type/TextType").TextType;
function Dictionary(itemType, dict, mutable) {
Value.call(this, new DictType(itemType));
this.dict = dict || {};
this.mutable = mutable || false;
return this;
}
Dictionary.prototype = Object.create(Value.prototype);
Dictionary.prototype.constructor = Dictionary;
Dictionary.prototype.toString = function() {
var names = Object.getOwnPropertyNames(this.dict);
var vals = names.map(function(name) {
return '"' + name + '":' + this.dict[name];
}, this);
return "{" + vals.join(", ") + "}";
};
Dictionary.merge = function(dict1, dict2) {
var dict = {};
for(var p in dict1.dict) {
dict[p] = dict1.dict[p];
}
for(var p in dict2.dict) {
dict[p] = dict2.dict[p];
}
return new Dictionary(dict1.type.itemType, dict);
};
Dictionary.prototype.size = function() {
var n = 0;
for(p in this.dict) {
n += 1;
}
return n;
};
Dictionary.prototype.isEmpty = function() {
for(var p in this.dict) {
return false;
}
return true;
};
Dictionary.prototype.Add = function(context, value) {
if (value instanceof Dictionary) {
return Dictionary.merge(this, value);
} else {
throw new SyntaxError("Illegal: Dict + " + typeof(value));
}
};
Dictionary.prototype.hasItem = function(context, value) {
if (value instanceof Text) {
return value.value in this.dict;
} else {
throw new SyntaxError("Only Text key type supported by Dictionary");
}
};
Dictionary.prototype.getMemberValue = function(context, name) {
if ("count"==name) {
return new Integer(this.size());
} else if ("keys"==name) {
var set_ = new SetValue(TextType.instance);
for(p in this.dict) {
set_.add(new Text(p));
}
return set_;
} else if ("values"==name) {
var list = []
for(p in this.dict) {
list.push(this.dict[p]); // no need to interpret now
}
return new ListValue(this.type.itemType, list);
} else {
return Value.prototype.getMemberValue.call(this, context, name);
}
};
Dictionary.prototype.setItemInContext = function(context, index, value) {
if (index instanceof Text) {
this.dict[index] = value;
} else
throw new SyntaxError("No such item:" + index.toString())
};
Dictionary.prototype.getItemInContext = function(context, index) {
if (index instanceof Text)
{
var value = this.dict[index] || NullValue.instance;
if (value instanceof Value) {
return value;
} else {
throw new InternalError("Item not a value!");
}
} else {
throw new SyntaxError("No such item:" + index.toString());
}
};
Dictionary.prototype.convertToJavaScript = function() {
var dict = {};
for(var key in this.dict) {
dict[key] = this.dict[key].convertToJavaScript();
}
return dict;
};
Dictionary.prototype.equals = function(obj) {
if(obj instanceof Dictionary) {
var keys = Object.getOwnPropertyNames(this.dict);
if(keys.length!=Object.getOwnPropertyNames(obj.dict).length) {
return false;
}
for(var i=0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy