Home Manual Reference Source Repository

src/easycurrency/MoneySpanSet.js

/**
 * Represents a unique set of MoneySpan objects.
 */
export default class MoneySpanSet {
    /**
     * Create an empty MoneySpanSet.
     */
    constructor() {
        /** @type {Object<String, MoneySpan>} */
        this._spansByElement = {};
    }

    /**
     * Add a new span to the set.
     * @param {src/easycurrency/MoneySpan.js~MoneySpan}	moneySpan - Span to add
     */
    add(moneySpan) {
        this._spansByElement[moneySpan.getElement()] = moneySpan;
    }

    /**
     * Find a span by the element it renders to.
     * @param {type}	element - The element the span renders to.
     * @return {undefined|src/easycurrency/MoneySpan.js~MoneySpan}
     */
    findByElement(element) {
        return this._spansByElement[element];
    }

    /**
     * Returns a list of all the spans in the set.
     * @return {MoneySpan[]}
     */
    list() {
        return Object.values(this._spansByElement);
    }
}