const GSM_BASIC = new Set( Array.from( `@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà` ) ) const GSM_EXTENDED = new Set(['\f', '^', '{', '}', '\\', '[', '~', ']', '|', '€']) export interface SmsSegmentCount { chars: number segments: number unicode: boolean } const isGsmText = (text: string) => Array.from(text).every((character) => GSM_BASIC.has(character) || GSM_EXTENDED.has(character)) export const countSmsSegments = (text: string): SmsSegmentCount => { const chars = Array.from(text).length if (chars === 0) return { chars: 0, segments: 0, unicode: false } const unicode = !isGsmText(text) if (!unicode) { return { chars, segments: chars <= 160 ? 1 : Math.ceil(chars / 153), unicode: false, } } return { chars, segments: chars <= 70 ? 1 : Math.ceil(chars / 67), unicode: true, } }