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

110 lines
3.5 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 MixFormat
* @constructor
* @param {!FormatModule} format
* @returns {void}
* @access public
* @static
*/
export const MixFormat = (function(){
/**
* @constructs MixFormat
* @param {!FormatModule} format
* @returns {void}
* @access private
* @static
*/
const MixFormat = function(format){
/** @type {MixFormat} */
const self = this;
/**
* @returns {void}
* @access private
*/
const constructor = () => {
format.modes.mix = self.get;
format.checks.mix = self.check;
format.modes.Mix = (inputs, shared, fragments) => self.get(inputs, {
...shared, capitalized : true
}, fragments);
format.checks.Mix = (string, inputs, shared, fragments) => self.check(string, inputs, {
...shared, capitalized : true
}, fragments);
};
/**
* @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 = []) => {
/** @type {[string, Array.<string>]} */
const [separator, original_items] = (
Check.is_string(inputs) ? ((_, separator, options) => [
separator, options.split("|")
])(...inputs.match(/^([^,]+),(.*)$/)) :
Check.is_array(inputs) ? inputs :
[]),
/** @type {Array.<string>} */
items = [...original_items];
/** @type {number} */
let l = items.length,
/** @type {string} */
results;
Utils.randomize_array(items);
results = format.execute(
l < 2 ? items.join("") :
items.slice(0, l - 1).join(", ") + (
!separator ? " " :
/^[a-z 0-9]+$/i.test(separator.trim()) ? " " + separator.trim() + " " :
separator) + items[l - 1], shared, fragments);
return shared.capitalized ? results.replace(/^./, character => character.toUpperCase()) : results;
};
/**
* @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 {[string, Array.<string>]} */
const [separator, items] = (
Check.is_string(inputs) ? ((_, separator, items) => [
separator, items.split("|")
])(...inputs.match(/^([^,]+),(.*)$/)) :
Check.is_array(inputs) ? inputs :
[]),
/** @type {number} */
l = items.length;
return format.check_select(string, [[l, l], separator, items], shared, fragments);
};
constructor();
};
return MixFormat;
})();