cxc-szcx-uniapp/uni_modules/lime-shared/characterLimit/index.ts

63 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @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
};