OpoTests/Public/ecma/Modules/Format/RandomFormat.ecma.js
2025-10-25 17:34:51 +02:00

93 lines
2.6 KiB
JavaScript

"use strict";
import {Utils} from "../../Utils/Utils.ecma.js";
import {Check} from "../../Utils/Check.ecma.js";
import {FormatModule} from "../FormatModule.ecma.js";
/**
* @typedef {import("../FormatModule.ecma.js").FormatModule} FormatModule
*/
/**
* @class RandomFormat
* @constructor
* @param {!FormatModule} format
* @returns {void}
* @access public
* @static
*/
export const RandomFormat = (function(){
/**
* @constructs RandomFormat
* @param {!FormatModule} format
* @returns {void}
* @access private
* @static
*/
const RandomFormat = function(format){
/** @type {RandomFormat} */
const self = this;
/**
* @returns {void}
* @access private
*/
const constructor = () => {
format.modes.rand = format.modes.random = self.get;
format.checks.rand = format.checks.random = self.check;
};
/**
* @param {!(string|Array.<any|null>)} inputs
* @param {!Object.<string, any|null>} [shared = {}]
* @param {!Array.<string>} [fragments = []]
* @returns {string}
* @access public
*/
this.get = (inputs, shared = {}, fragments = []) => format.execute(Utils.get_random(
Check.is_string(inputs) ? inputs.split("|") :
Check.is_array(inputs[0]) ? inputs[0] :
[]), shared, fragments);
/**
* @param {!string} string
* @param {!(string|Array.<string>)} inputs
* @param {!Object.<string, any|null>} [shared = {}]
* @param {!Array.<string>} [fragments = []]
* @returns {number}
* @access public
*/
this.check = (string, inputs, shared = {}, fragments = []) => {
/** @type {boolean} */
let has_empty = false;
for(const option of (
Check.is_string(inputs) ? inputs.split("|") :
Check.is_array(inputs[0]) ? inputs[0] :
[])){
if(!option){
has_empty = true;
continue;
};
if(/\{[^\{\}]+\}/.test(format.set_fragments_level(option, fragments)))
return format.get_check_length(string, [option], shared, fragments, false);
/** @type {number} */
let length = FormatModule.prepare_result(string, option, shared);
if(length != -1)
return length;
};
return has_empty ? 0 : -1;
};
constructor();
};
return RandomFormat;
})();