You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.2 KiB
80 lines
1.2 KiB
/**
|
|
* An object that can be used as an array with a key
|
|
* Like PHP's array
|
|
*/
|
|
class ArrayWithKey {
|
|
__stack = [];
|
|
|
|
/**
|
|
*
|
|
*/
|
|
constructor() {
|
|
|
|
}
|
|
|
|
/**
|
|
* @param key
|
|
* @param value
|
|
*/
|
|
push(key, value) {
|
|
this[key] = value;
|
|
this.__stack.push(key);
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
pop() {
|
|
let key = this.__stack.pop();
|
|
let prop = this[key];
|
|
delete this[key];
|
|
return prop;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getLastKey() {
|
|
if (this.__stack.length === 0) {
|
|
return null;
|
|
}
|
|
return this.__stack[this.__stack.length - 1];
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
shift() {
|
|
let key = this.__stack.shift();
|
|
let value = this[key];
|
|
delete this[key];
|
|
return {
|
|
key,
|
|
value,
|
|
};
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
length() {
|
|
return this.__stack.length;
|
|
}
|
|
|
|
/**
|
|
* Get the last element
|
|
* @returns {*|null} The last element, or null if the array is empty
|
|
*/
|
|
last() {
|
|
let key = this.getLastKey();
|
|
if (key === null) {
|
|
return null;
|
|
}
|
|
return this[key];
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
ArrayWithKey
|
|
};
|