From df606a1f7bcb32747085972b740c6685ffabcb71 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 23 Oct 2024 17:19:31 +0800 Subject: feat: add support for full-width digits --- src/utils.mjs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/utils.mjs') diff --git a/src/utils.mjs b/src/utils.mjs index dbb3881..d2c5d8f 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -235,3 +235,20 @@ export function getCommonAncestor (selector) { // Return the common ancestor return range.commonAncestorContainer } + +/** + * full2Half: full-Width Digits To Half-Width. + * + * @param {String} str + */ +export const full2Half = (str) => { + // Create a regular expression to match full-width digits (U+FF10 to U+FF19) + const fullWidthDigitsRegex = /[\uFF0E\uFF10-\uFF19]/g + + // Replace full-width digits with their half-width equivalents + return str.replace(fullWidthDigitsRegex, (match) => { + const fullWidthDigit = match.charCodeAt(0) + const halfWidthDigit = fullWidthDigit - 65248 // Offset to convert full-width to half-width + return String.fromCharCode(halfWidthDigit) + }) +} -- cgit v1.2.3-70-g09d2