@schukai/monster
Version:
Monster is a simple library for creating fast, robust and lightweight websites.
72 lines (61 loc) • 1.92 kB
JavaScript
/**
* Copyright © Volker Schukai and all contributing authors, {{copyRightYear}}. All rights reserved.
* Node module: @schukai/monster
*
* This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
* The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
*
* For those who do not wish to adhere to the AGPLv3, a commercial license is available.
* Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
* For more information about purchasing a commercial license, please contact Volker Schukai.
*
* SPDX-License-Identifier: AGPL-3.0
*/
import { Base } from "./base.mjs";
import { validateString } from "./validate.mjs";
export { ID };
/**
* @private
* @type {Map<string, integer>}
*/
const internalCounter = new Map();
/**
* With the id class, sequences of ids can be created. for this purpose, an internal counter is incremented for each prefix.
* thus, the first id with the prefix `myid` will be `myid1` and the second id `myid2`.
* The ids are the same for every call, for example on a web page.
*
* So the ids can also be used for navigation. you have to take care that the order stays the same.
*
* As of version 1.6.0 there is the new RandomID. this ID class is continuous from now on.
*
* @license AGPLv3
* @since 1.0.0
* @copyright Volker Schukai
* @summary Automatic generation of ids
*/
class ID extends Base {
/**
* create new id with prefix
*
* @param {string} prefix
*/
constructor(prefix) {
super();
if (prefix === undefined) {
prefix = "id";
}
validateString(prefix);
if (!internalCounter.has(prefix)) {
internalCounter.set(prefix, 1);
}
let count = internalCounter.get(prefix);
this.id = prefix + count;
internalCounter.set(prefix, ++count);
}
/**
* @return {string}
*/
toString() {
return this.id;
}
}