1
0
mirror of https://github.com/flarum/core.git synced 2025-08-03 06:57:54 +02:00

convert: common/utils/ItemList

This commit is contained in:
David Sevilla Martin
2020-06-21 19:08:34 -04:00
committed by Franz Liedke
parent 24a48310ff
commit 4869baea74

View File

@@ -1,5 +1,9 @@
class Item { class Item {
constructor(content, priority) { content: any;
priority: number;
key?: number;
constructor(content: any, priority?: number) {
this.content = content; this.content = content;
this.priority = priority; this.priority = priority;
} }
@@ -10,23 +14,15 @@ class Item {
* by priority. * by priority.
*/ */
export default class ItemList { export default class ItemList {
constructor() {
/** /**
* The items in the list. * The items in the list
*
* @type {Object}
* @public
*/ */
this.items = {}; items: { [key: string]: Item } = {};
}
/** /**
* Check whether the list is empty. * Check whether the list is empty.
*
* @returns {boolean}
* @public
*/ */
isEmpty() { isEmpty(): boolean {
for (const i in this.items) { for (const i in this.items) {
if (this.items.hasOwnProperty(i)) { if (this.items.hasOwnProperty(i)) {
return false; return false;
@@ -38,36 +34,27 @@ export default class ItemList {
/** /**
* Check whether an item is present in the list. * Check whether an item is present in the list.
*
* @param key
* @returns {boolean}
*/ */
has(key) { has(key: string): boolean {
return !!this.items[key]; return !!this.items[key];
} }
/** /**
* Get the content of an item. * Get the content of an item.
*
* @param {String} key
* @return {*}
* @public
*/ */
get(key) { get(key: string): any {
return this.items[key].content; return this.items[key].content;
} }
/** /**
* Add an item to the list. * Add an item to the list.
* *
* @param {String} key A unique key for the item. * @param key A unique key for the item.
* @param {*} content The item's content. * @param content The item's content.
* @param {Integer} [priority] The priority of the item. Items with a higher * @param [priority] The priority of the item. Items with a higher
* priority will be positioned before items with a lower priority. * priority will be positioned before items with a lower priority.
* @return {ItemList}
* @public
*/ */
add(key, content, priority = 0) { add(key: string, content: any, priority: number = 0): this {
this.items[key] = new Item(content, priority); this.items[key] = new Item(content, priority);
return this; return this;
@@ -75,14 +62,8 @@ export default class ItemList {
/** /**
* Replace an item in the list, only if it is already present. * Replace an item in the list, only if it is already present.
*
* @param {String} key
* @param {*} [content]
* @param {Integer} [priority]
* @return {ItemList}
* @public
*/ */
replace(key, content = null, priority = null) { replace(key: string, content: any = null, priority: number = null): this {
if (this.items[key]) { if (this.items[key]) {
if (content !== null) { if (content !== null) {
this.items[key].content = content; this.items[key].content = content;
@@ -98,12 +79,8 @@ export default class ItemList {
/** /**
* Remove an item from the list. * Remove an item from the list.
*
* @param {String} key
* @return {ItemList}
* @public
*/ */
remove(key) { remove(key: string): this {
delete this.items[key]; delete this.items[key];
return this; return this;
@@ -111,12 +88,8 @@ export default class ItemList {
/** /**
* Merge another list's items into this one. * Merge another list's items into this one.
*
* @param {ItemList} items
* @return {ItemList}
* @public
*/ */
merge(items) { merge(items: this): this {
for (const i in items.items) { for (const i in items.items) {
if (items.items.hasOwnProperty(i) && items.items[i] instanceof Item) { if (items.items.hasOwnProperty(i) && items.items[i] instanceof Item) {
this.items[i] = items.items[i]; this.items[i] = items.items[i];
@@ -130,12 +103,9 @@ export default class ItemList {
* Convert the list into an array of item content arranged by priority. Each * Convert the list into an array of item content arranged by priority. Each
* item's content will be assigned an `itemName` property equal to the item's * item's content will be assigned an `itemName` property equal to the item's
* unique key. * unique key.
*
* @return {Array}
* @public
*/ */
toArray() { toArray(): any[] {
const items = []; const items: Item[] = [];
for (const i in this.items) { for (const i in this.items) {
if (this.items.hasOwnProperty(i) && this.items[i] instanceof Item) { if (this.items.hasOwnProperty(i) && this.items[i] instanceof Item) {