SmartParks_visitore/uni_modules/lime-shared/isByteLength/index.ts

86 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

2025-08-21 11:23:54 +08:00
// @ts-nocheck
// import assertString from './util/assertString';
/**
*
*/
export type IsByteLengthOptions = {
/** 允许的最小字节长度 */
min ?: number;
/** 允许的最大字节长度 */
max ?: number;
}
/**
*
* @function
* @overload 使
* @param str -
* @param options -
* @returns
*
* @overload 使
* @param str -
* @param min -
* @param max -
* @returns
*
* @example
* // 使用配置对象
* isByteLength('🇨🇳', { min: 4, max: 8 }); // trueunicode 国旗符号占 8 字节)
*
* @example
* // 旧式参数调用
* isByteLength('hello', 3, 7); // true实际字节长度 5
*
* @description
* 1. 使 URL
* 2.
* - { min, max }
* - (min, max)
* 3. max
* 4. 0
*/
export function isByteLength(str : string, optionsOrMin ?: IsByteLengthOptions) : boolean;
export function isByteLength(str : string, optionsOrMin ?: number) : boolean;
export function isByteLength(str : string, optionsOrMin : number, maxParam : number | null) : boolean;
export function isByteLength(
str : string,
optionsOrMin ?: IsByteLengthOptions | number,
maxParam : number | null = null
) : boolean {
// assertString(str);
/** 最终计算的最小长度 */
let min: number;
/** 最终计算的最大长度 */
let max : number | null;
// 参数逻辑处理
if (optionsOrMin != null && typeof optionsOrMin == 'object') {
// 使用对象配置的情况
const options = optionsOrMin as IsByteLengthOptions;
min = Math.max(options.min ?? 0, 0); // 确保最小值为正整数
max = options.max;
} else {
// 使用独立参数的情况
min = Math.max(
typeof optionsOrMin == 'number' ? optionsOrMin : 0,
0
);
max = maxParam;
}
// URL 编码后的字节长度计算
const encoded = encodeURI(str);
const len = (encoded?.split(/%..|./).length ?? 0) - 1;
// 执行验证逻辑
// #ifndef APP-ANDROID
return len >= min && (typeof max == 'undefined' || len <= (max ?? 0));
// #endif
// #ifdef APP-ANDROID
return len >= min && (max == null || len <= max);
// #endif
}