admin/lib/smsSegments.ts
alisaza e1eaf5eff5 feat: initial ghabilee-admin backoffice app
Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js
app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
2026-09-05 13:12:59 +03:30

37 lines
985 B
TypeScript

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,
}
}