Fullsize and Halfsize in Mendix

0
I have a textbox and I want to check for fullwidth characters before taking action. How can I check for this and convert halfwidth characters to fullwidth in Mendix?"
asked
1 answers
0

In an OnChange event you can trigger a Nanoflow that calls a JavaScript action. In the JavaScript action you can check and convert the string value with the below snippet. (I have not checked if this snippet exactly works though)

 

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import "mx-global";
import { Big } from "big.js";

// BEGIN EXTRA CODE
// END EXTRA CODE

/**
 * @param {string} valueToConvert
 * @returns {Promise.<string>}
 */
export async function JavaScript_action(valueToConvert) {
	// BEGIN USER CODE
	/**
	 * Checks if a character is a fullwidth character.
	 * Fullwidth characters have Unicode code points between 0xFF01 and 0xFF5E.
	 * 
	 * @param {string} char - The character to check.
	 * @returns {boolean} - True if the character is fullwidth, false otherwise.
	 */
	const isFullWidth = (char) => {
		const fullWidthStart = 0xFF01;
		const fullWidthEnd = 0xFF5E;
		const charCode = char.charCodeAt(0);
		return charCode >= fullWidthStart && charCode <= fullWidthEnd;
	}

	/**
	 * Converts halfwidth characters in a string to fullwidth characters.
	 * 
	 * @param {string} str - The string to convert.
	 * @returns {string} - The converted string with fullwidth characters.
	 */
	const toFullWidth = (str) => {
		return str.replace(/[!-~]/g, function(ch) {
			const halfWidthOffset = 0xFEE0;
			return String.fromCharCode(ch.charCodeAt(0) + halfWidthOffset);
		});
	}

	/**
	 * Checks for fullwidth characters and converts halfwidth characters to fullwidth.
	 * 
	 * @param {string} input - The input string to check and convert.
	 * @returns {object} - An object containing a boolean indicating if fullwidth characters are present and the converted string.
	 */

	const checkAndConvertFullWidth = (input) => {
		let hasFullWidth = false;
		for (let i = 0; i < input.length; i++) {
			if (isFullWidth(input[i])) {
				hasFullWidth = true;
				break;
			}
		}
		return {
			hasFullWidth: hasFullWidth,
			converted: toFullWidth(input)
		};
	}

	const result = checkAndConvertFullWidth(valueToConvert);

	if (result.hasFullWidth) {
		return result.converted;
	} else return null;

	// END USER CODE
}

 

answered