types/id.js

'use strict';

/**
 * @author schukai GmbH
 */

import {Monster} from '../namespace.js';
import {random} from '../math/random.js';
import {Base} from  './base.js';

let internalCounter = 0;

/**
 * id class
 *
 * you can call the method via the monster namespace `new Monster.Types.ID()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.5.0/dist/modules/types/id.js';
 * console.log(new Monster.Types.ID())
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {ID} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.5.0/dist/modules/types/id.js';
 * console.log(new ID())
 * </script>
 * ```
 *
 * @since 1.0.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 */
class ID extends Base {

    /**
     * create new object
     */
    constructor() {
        super();

        internalCounter += 1;

        this.id = global.btoa(random(1, 10000))
            .replace(/=/g, '')
            /** No numbers at the beginning of the ID, because of possible problems with DOM */
            .replace(/^[0-9]+/, 'X') + internalCounter;
    }

    toString() {
        return this.id;
    }

}

Monster.assignToNamespace('Monster.Types', ID);
export {Monster, ID}