All Downloads are FREE. Search and download functionalities are using the official Maven repository.

js.prompto.value.BaseValueList.js Maven / Gradle / Ivy

var Value = require("./Value").Value;
var Container = require("./Value").Container;
var Integer = require("./Integer").Integer;
var PromptoError = require("../error/PromptoError").PromptoError;
var InternalError = require("../error/InternalError").InternalError;
var IndexOutOfRangeError = require("../error/IndexOutOfRangeError").IndexOutOfRangeError;

/* an abstract list of values, common to ListValue and TupleValue */
function BaseValueList(type, items, item, mutable) {
    Container.call(this, type);
	this.items = items || [];
    item = item || null;
    if(item!==null) {
        this.add(item);
    }
    this.mutable = mutable || false;
    return this;
}

BaseValueList.prototype = Object.create(Container.prototype);
BaseValueList.prototype.constructor = BaseValueList;

BaseValueList.prototype.toString = function() {
	return "[" + this.items.join(", ") + "]";
};

BaseValueList.prototype.add = function(o) {
	this.items.push(o);
};


BaseValueList.prototype.setItem = function(index, value) {
    this.items[index] = value;
};


BaseValueList.prototype.setItemInContext = function(context, index, value) {
    if (index instanceof Integer) {
        var idx = index.IntegerValue() - 1;
        if (idx > this.items.length) {
            throw new IndexOutOfRangeError();
        }
        this.items[idx] = value;
    } else
        throw new SyntaxError("No such item:" + index.toString())
};


BaseValueList.prototype.get = function(index) {
	return this.items[index];
};

BaseValueList.prototype.size = function() {
	return this.items.length;
};


BaseValueList.prototype.isEmpty = function() {
	return this.items.length===0;
};

BaseValueList.prototype.slice = function(fi, li) {
	var first = this.checkFirst(fi);
	var last = this.checkLast(li);
	var items = this.items.slice(first-1,last);
	return this.newInstance(items);
};

BaseValueList.prototype.checkFirst = function(fi) {
	var value = (fi == null) ? 1 : fi.IntegerValue();
	if (value < 1 || value > this.items.length) {
		throw new IndexOutOfRangeError();
	}
	return value;
};

BaseValueList.prototype.checkLast = function(li) {
	var value = (li == null) ? this.items.length : li.IntegerValue();
	if (value < 0) {
		value = this.items.length + 1 + li.IntegerValue();
	}
	if (value < 1 || value > this.items.length) {
		throw new IndexOutOfRangeError();
	}
	return value;
};

BaseValueList.prototype.hasItem = function(context, lval) {
	for (var i=0;ithis.items.length) {
				throw new IndexOutOfRangeError();
			}
			var value = this.items[idx] || null;
			if(value==null) {
				return null;
			}
			if (value instanceof Value) {
				return value;
			} else {
				throw new InternalError("Item not a value!");
			}
		} catch (e) {
			if(e instanceof PromptoError) {
				throw e;
			} else {
				throw new InternalError(e.toString());
			}
		}
	} else
		throw new SyntaxError("No such item:" + index.toString());
};

BaseValueList.prototype.equals = function(obj) {
	if(obj instanceof BaseValueList) {
		if(this.items.length!=obj.items.length) {
			return false;
		} else {
			for(var i=0;i0) {
        this.items.forEach(function(o) {
            if(o.toDialect)
                o.toDialect(writer);
            else
                writer.append(o.toString());
            writer.append(", ");
        });
        writer.trimLast(2);
    }
};

BaseValueList.prototype.toJson = function(context, json, instanceId, fieldName, withType, binaries) {
    var values = [];
    this.items.map(function(item) {
        item.toJson(context, values, instanceId, fieldName, withType, binaries);
    });
    if(Array.isArray(json))
        json.push(values);
    else
        json[fieldName] = values;

};


exports.BaseValueList = BaseValueList;




© 2015 - 2025 Weber Informatics LLC | Privacy Policy