halfred
Version:
parses JSON HAL resources (Hypertext Application Language)
44 lines (36 loc) • 1.12 kB
JavaScript
;
/*
* A very naive copy-on-write immutable stack. Since the size of the stack
* is equal to the depth of the embedded resources for one HAL resource, the bad
* performance for the copy-on-write approach is probably not a problem at all.
* Might be replaced by a smarter solution later. Or not. Whatever.
*/
function ImmutableStack() {
if (arguments.length >= 1) {
this._array = arguments[0];
} else {
this._array = [];
}
}
ImmutableStack.prototype.array = function() {
return this._array;
};
ImmutableStack.prototype.isEmpty = function(array) {
return this._array.length === 0;
};
ImmutableStack.prototype.push = function(element) {
var array = this._array.slice(0);
array.push(element);
return new ImmutableStack(array);
};
ImmutableStack.prototype.pop = function() {
var array = this._array.slice(0, this._array.length - 1);
return new ImmutableStack(array);
};
ImmutableStack.prototype.peek = function() {
if (this.isEmpty()) {
throw new Error('can\'t peek on empty stack');
}
return this._array[this._array.length - 1];
};
module.exports = ImmutableStack;