/* * Copyright (c) 2016-2021 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; module.exports = class IdentifierIssuer { /** * Creates a new IdentifierIssuer. A IdentifierIssuer issues unique * identifiers, keeping track of any previously issued identifiers. * * @param prefix the prefix to use (''). * @param existing an existing Map to use. * @param counter the counter to use. */ constructor(prefix, existing = new Map(), counter = 0) { this.prefix = prefix; this._existing = existing; this.counter = counter; } /** * Copies this IdentifierIssuer. * * @return a copy of this IdentifierIssuer. */ clone() { const {prefix, _existing, counter} = this; return new IdentifierIssuer(prefix, new Map(_existing), counter); } /** * Gets the new identifier for the given old identifier, where if no old * identifier is given a new identifier will be generated. * * @param [old] the old identifier to get the new identifier for. * * @return the new identifier. */ getId(old) { // return existing old identifier const existing = old && this._existing.get(old); if(existing) { return existing; } // get next identifier const identifier = this.prefix + this.counter; this.counter++; // save mapping if(old) { this._existing.set(old, identifier); } return identifier; } /** * Returns true if the given old identifer has already been assigned a new * identifier. * * @param old the old identifier to check. * * @return true if the old identifier has been assigned a new identifier, * false if not. */ hasId(old) { return this._existing.has(old); } /** * Returns all of the IDs that have been issued new IDs in the order in * which they were issued new IDs. * * @return the list of old IDs that has been issued new IDs in order. */ getOldIds() { return [...this._existing.keys()]; } };