di-tory
Version:
Compose applications with dependency injection
30 lines (29 loc) • 731 B
JavaScript
import { StackError, StackErrorType } from './StackError.js';
export class Stack {
constructor(items = [], set = new Set()) {
this.items = items;
this.set = set;
}
push(item) {
if (this.set.has(item))
throw new StackError(StackErrorType.Exists);
this.items.push(item);
this.set.add(item);
}
pop() {
const item = this.items.pop();
if (item == null)
throw new StackError(StackErrorType.Empty);
this.set.delete(item);
return item;
}
peek() {
return this.items.at(-1);
}
toStringArray() {
return this.items.map(String);
}
get length() {
return this.items.length;
}
}