This commit is contained in:
2025-08-21 11:23:54 +08:00
commit abf7f3c799
530 changed files with 60239 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
// @ts-nocheck
/**
* 计算字符串字符的长度并可以截取字符串。
* @param char 传入字符串maxcharacter条件下一个汉字表示两个字符
* @param max 规定最大字符串长度
* @returns 当没有传入maxCharacter/maxLength 时返回字符串字符长度当传入maxCharacter/maxLength时返回截取之后的字符串和长度。
*/
export type CharacterLengthResult = {
length : number;
characters : string;
}
// #ifdef APP-ANDROID
type ChartType = any
// #endif
// #ifndef APP-ANDROID
type ChartType = string | number
// #endif
export function characterLimit(type : string, char : ChartType, max : number) : CharacterLengthResult {
const str = `${char}`;
if (str.length == 0) {
return {
length: 0,
characters: '',
} as CharacterLengthResult
}
if (type == 'maxcharacter') {
let len = 0;
for (let i = 0; i < str.length; i += 1) {
let currentStringLength : number// = 0;
const code = str.charCodeAt(i)!
if (code > 127 || code == 94) {
currentStringLength = 2;
} else {
currentStringLength = 1;
}
if (len + currentStringLength > max) {
return {
length: len,
characters: str.slice(0, i),
} as CharacterLengthResult
}
len += currentStringLength;
}
return {
length: len,
characters: str,
} as CharacterLengthResult
} else if (type == 'maxlength') {
const length = str.length > max ? max : str.length;
return {
length: length,
characters: str.slice(0, length),
} as CharacterLengthResult
}
return {
length: str.length,
characters: str,
} as CharacterLengthResult
};