tribune
Version:
Holy advocate to the Roman Senate, your Tribune will ensure the Consuls have your interests in mind.
28 lines (21 loc) • 739 B
JavaScript
class TagArray extends Array {
set(key, val) {
const escKey = key.replace(/([$:])/g, '\\$1');
const escVal = val.replace(/([$:])/g, '\\$1');
const index = this.findIndex((tag) =>
tag[0] === '$' && tag.slice(1, escKey.length + 1) === escKey);
if (index > -1) { this.splice(index, 1); }
this.push(encodeURIComponent(`$${escKey}:${escVal}`));
return val;
}
get(key) {
const escKey = key.replace(/([$:])/g, '\\$1');
const tag = this
.map(tag => decodeURIComponent(tag))
.find((tag) =>
tag[0] === '$' && tag.slice(1, escKey.length + 1) === escKey);
if (!tag) { return; }
return tag.slice(escKey.length + 2).replace(/\\([$:])/g, '$1');
}
}
module.exports = TagArray;